opusdev/vector-similarity-api
1
1import type { Document } from './bson';2 3/** @public */4export const ReadConcernLevel = Object.freeze({5 local: 'local',6 majority: 'majority',7 linearizable: 'linearizable',8 available: 'available',9 snapshot: 'snapshot'10} as const);11 12/** @public */13export type ReadConcernLevel = (typeof ReadConcernLevel)[keyof typeof ReadConcernLevel];14 15/** @public */16export type ReadConcernLike = ReadConcern | { level: ReadConcernLevel } | ReadConcernLevel;17 18/**19 * The MongoDB ReadConcern, which allows for control of the consistency and isolation properties20 * of the data read from replica sets and replica set shards.21 * @public22 *23 * @see https://www.mongodb.com/docs/manual/reference/read-concern/index.html24 */25export class ReadConcern {26 level: ReadConcernLevel | string;27 28 /** Constructs a ReadConcern from the read concern level.*/29 constructor(level: ReadConcernLevel) {30 /**31 * A spec test exists that allows level to be any string.32 * "invalid readConcern with out stage"33 * @see ./test/spec/crud/v2/aggregate-out-readConcern.json34 * @see https://github.com/mongodb/specifications/blob/master/source/read-write-concern/read-write-concern.md#unknown-levels-and-additional-options-for-string-based-readconcerns35 */36 this.level = ReadConcernLevel[level] ?? level;37 }38 39 /**40 * Construct a ReadConcern given an options object.41 *42 * @param options - The options object from which to extract the write concern.43 */44 static fromOptions(options?: {45 readConcern?: ReadConcernLike;46 level?: ReadConcernLevel;47 }): ReadConcern | undefined {48 if (options == null) {49 return;50 }51 52 if (options.readConcern) {53 const { readConcern } = options;54 if (readConcern instanceof ReadConcern) {55 return readConcern;56 } else if (typeof readConcern === 'string') {57 return new ReadConcern(readConcern);58 } else if ('level' in readConcern && readConcern.level) {59 return new ReadConcern(readConcern.level);60 }61 }62 63 if (options.level) {64 return new ReadConcern(options.level);65 }66 return;67 }68 69 static get MAJORITY(): 'majority' {70 return ReadConcernLevel.majority;71 }72 73 static get AVAILABLE(): 'available' {74 return ReadConcernLevel.available;75 }76 77 static get LINEARIZABLE(): 'linearizable' {78 return ReadConcernLevel.linearizable;79 }80 81 static get SNAPSHOT(): 'snapshot' {82 return ReadConcernLevel.snapshot;83 }84 85 toJSON(): Document {86 return { level: this.level };87 }88}89 