CoolFace
Apppublic

opusdev/vector-similarity-api

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes
db_ref.ts129 linesDownload Raw Back to src
1import type { Document } from './bson';2import { BSONValue } from './bson_value';3import type { EJSONOptions } from './extended_json';4import type { ObjectId } from './objectid';5import { type InspectFn, defaultInspect } from './parser/utils';6 7/** @public */8export interface DBRefLike {9  $ref: string;10  $id: ObjectId;11  $db?: string;12}13 14/** @internal */15export function isDBRefLike(value: unknown): value is DBRefLike {16  return (17    value != null &&18    typeof value === 'object' &&19    '$id' in value &&20    value.$id != null &&21    '$ref' in value &&22    typeof value.$ref === 'string' &&23    // If '$db' is defined it MUST be a string, otherwise it should be absent24    (!('$db' in value) || ('$db' in value && typeof value.$db === 'string'))25  );26}27 28/**29 * A class representation of the BSON DBRef type.30 * @public31 * @category BSONType32 */33export class DBRef extends BSONValue {34  get _bsontype(): 'DBRef' {35    return 'DBRef';36  }37 38  collection!: string;39  oid!: ObjectId;40  db?: string;41  fields!: Document;42 43  /**44   * @param collection - the collection name.45   * @param oid - the reference ObjectId.46   * @param db - optional db name, if omitted the reference is local to the current db.47   */48  constructor(collection: string, oid: ObjectId, db?: string, fields?: Document) {49    super();50    // check if namespace has been provided51    const parts = collection.split('.');52    if (parts.length === 2) {53      db = parts.shift();54      collection = parts.shift()!;55    }56 57    this.collection = collection;58    this.oid = oid;59    this.db = db;60    this.fields = fields || {};61  }62 63  // Property provided for compatibility with the 1.x parser64  // the 1.x parser used a "namespace" property, while 4.x uses "collection"65 66  /** @internal */67  get namespace(): string {68    return this.collection;69  }70 71  set namespace(value: string) {72    this.collection = value;73  }74 75  toJSON(): DBRefLike & Document {76    const o = Object.assign(77      {78        $ref: this.collection,79        $id: this.oid80      },81      this.fields82    );83 84    if (this.db != null) o.$db = this.db;85    return o;86  }87 88  /** @internal */89  toExtendedJSON(options?: EJSONOptions): DBRefLike {90    options = options || {};91    let o: DBRefLike = {92      $ref: this.collection,93      $id: this.oid94    };95 96    if (options.legacy) {97      return o;98    }99 100    if (this.db) o.$db = this.db;101    o = Object.assign(o, this.fields);102    return o;103  }104 105  /** @internal */106  static fromExtendedJSON(doc: DBRefLike): DBRef {107    const copy = Object.assign({}, doc) as Partial<DBRefLike>;108    delete copy.$ref;109    delete copy.$id;110    delete copy.$db;111    return new DBRef(doc.$ref, doc.$id, doc.$db, copy);112  }113 114  inspect(depth?: number, options?: unknown, inspect?: InspectFn): string {115    inspect ??= defaultInspect;116 117    const args = [118      inspect(this.namespace, options),119      inspect(this.oid, options),120      ...(this.db ? [inspect(this.db, options)] : []),121      ...(Object.keys(this.fields).length > 0 ? [inspect(this.fields, options)] : [])122    ];123 124    args[1] = inspect === defaultInspect ? `new ObjectId(${args[1]})` : args[1];125 126    return `new DBRef(${args.join(', ')})`;127  }128}129