CoolFace
Apppublic

opusdev/vector-similarity-api

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes
write_concern.ts184 linesDownload Raw Back to src
1import { type Document } from './bson';2import { MongoDBResponse } from './cmap/wire_protocol/responses';3import { MongoWriteConcernError } from './error';4 5/** @public */6export type W = number | 'majority';7 8/** @public */9export interface WriteConcernOptions {10  /** Write Concern as an object */11  writeConcern?: WriteConcern | WriteConcernSettings;12}13 14/** @public */15export interface WriteConcernSettings {16  /** The write concern */17  w?: W;18  /**19   * The write concern timeout.20   */21  wtimeoutMS?: number;22  /** The journal write concern */23  journal?: boolean;24 25  // legacy options26  /**27   * The journal write concern.28   * @deprecated Will be removed in the next major version. Please use the journal option.29   */30  j?: boolean;31  /**32   * The write concern timeout.33   */34  wtimeout?: number;35  /**36   * The file sync write concern.37   * @deprecated Will be removed in the next major version. Please use the journal option.38   */39  fsync?: boolean | 1;40}41 42export const WRITE_CONCERN_KEYS = ['w', 'wtimeout', 'j', 'journal', 'fsync'];43 44/** The write concern options that decorate the server command. */45interface CommandWriteConcernOptions {46  /** The write concern */47  w?: W;48  /** The journal write concern. */49  j?: boolean;50  /** The write concern timeout. */51  wtimeout?: number;52}53 54/**55 * A MongoDB WriteConcern, which describes the level of acknowledgement56 * requested from MongoDB for write operations.57 * @public58 *59 * @see https://www.mongodb.com/docs/manual/reference/write-concern/60 */61export class WriteConcern {62  /**63   * Request acknowledgment that the write operation has propagated to a specified number of mongod instances or to mongod instances with specified tags.64   * If w is 0 and is set on a write operation, the server will not send a response.65   */66  readonly w?: W;67  /** Request acknowledgment that the write operation has been written to the on-disk journal */68  readonly journal?: boolean;69  /**70   * Specify a time limit to prevent write operations from blocking indefinitely.71   */72  readonly wtimeoutMS?: number;73  /**74   * Specify a time limit to prevent write operations from blocking indefinitely.75   * @deprecated Will be removed in the next major version. Please use wtimeoutMS.76   */77  wtimeout?: number;78  /**79   * Request acknowledgment that the write operation has been written to the on-disk journal.80   * @deprecated Will be removed in the next major version. Please use journal.81   */82  j?: boolean;83  /**84   * Equivalent to the j option.85   * @deprecated Will be removed in the next major version. Please use journal.86   */87  fsync?: boolean | 1;88 89  /**90   * Constructs a WriteConcern from the write concern properties.91   * @param w - request acknowledgment that the write operation has propagated to a specified number of mongod instances or to mongod instances with specified tags.92   * @param wtimeoutMS - specify a time limit to prevent write operations from blocking indefinitely93   * @param journal - request acknowledgment that the write operation has been written to the on-disk journal94   * @param fsync - equivalent to the j option. Is deprecated and will be removed in the next major version.95   */96  constructor(w?: W, wtimeoutMS?: number, journal?: boolean, fsync?: boolean | 1) {97    if (w != null) {98      if (!Number.isNaN(Number(w))) {99        this.w = Number(w);100      } else {101        this.w = w;102      }103    }104    if (wtimeoutMS != null) {105      this.wtimeoutMS = this.wtimeout = wtimeoutMS;106    }107    if (journal != null) {108      this.journal = this.j = journal;109    }110    if (fsync != null) {111      this.journal = this.j = fsync ? true : false;112    }113  }114 115  /**116   * Apply a write concern to a command document. Will modify and return the command.117   */118  static apply(command: Document, writeConcern: WriteConcern): Document {119    const wc: CommandWriteConcernOptions = {};120    // The write concern document sent to the server has w/wtimeout/j fields.121    if (writeConcern.w != null) wc.w = writeConcern.w;122    if (writeConcern.wtimeoutMS != null) wc.wtimeout = writeConcern.wtimeoutMS;123    if (writeConcern.journal != null) wc.j = writeConcern.j;124    command.writeConcern = wc;125    return command;126  }127 128  /** Construct a WriteConcern given an options object. */129  static fromOptions(130    options?: WriteConcernOptions | WriteConcern | W,131    inherit?: WriteConcernOptions | WriteConcern132  ): WriteConcern | undefined {133    if (options == null) return undefined;134    inherit = inherit ?? {};135    let opts: WriteConcernSettings | WriteConcern | undefined;136    if (typeof options === 'string' || typeof options === 'number') {137      opts = { w: options };138    } else if (options instanceof WriteConcern) {139      opts = options;140    } else {141      opts = options.writeConcern;142    }143    const parentOpts: WriteConcern | WriteConcernSettings | undefined =144      inherit instanceof WriteConcern ? inherit : inherit.writeConcern;145 146    const mergedOpts = { ...parentOpts, ...opts } as WriteConcernSettings;147    const {148      w = undefined,149      wtimeout = undefined,150      j = undefined,151      fsync = undefined,152      journal = undefined,153      wtimeoutMS = undefined154    } = mergedOpts;155    if (156      w != null ||157      wtimeout != null ||158      wtimeoutMS != null ||159      j != null ||160      journal != null ||161      fsync != null162    ) {163      return new WriteConcern(w, wtimeout ?? wtimeoutMS, j ?? journal, fsync);164    }165    return undefined;166  }167}168 169/** Called with either a plain object or MongoDBResponse */170export function throwIfWriteConcernError(response: unknown): void {171  if (typeof response === 'object' && response != null) {172    const writeConcernError: object | null =173      MongoDBResponse.is(response) && response.has('writeConcernError')174        ? response.toObject()175        : !MongoDBResponse.is(response) && 'writeConcernError' in response176          ? response177          : null;178 179    if (writeConcernError != null) {180      throw new MongoWriteConcernError(writeConcernError as any);181    }182  }183}184