opusdev/vector-similarity-api
1
1import { type Connection } from '..';2import type { Document } from '../bson';3import type { BulkWriteOptions } from '../bulk/common';4import { MongoDBResponse } from '../cmap/wire_protocol/responses';5import type { Collection } from '../collection';6import { MongoServerError } from '../error';7import type { InferIdType } from '../mongo_types';8import type { ClientSession } from '../sessions';9import { maybeAddIdToDocuments, type MongoDBNamespace } from '../utils';10import { CommandOperation, type CommandOperationOptions } from './command';11import { Aspect, defineAspects } from './operation';12/** @internal */13export class InsertOperation extends CommandOperation<Document> {14 override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;15 override options: BulkWriteOptions;16 17 documents: Document[];18 19 constructor(ns: MongoDBNamespace, documents: Document[], options: BulkWriteOptions) {20 super(undefined, options);21 this.options = { ...options, checkKeys: options.checkKeys ?? false };22 this.ns = ns;23 this.documents = documents;24 }25 26 override get commandName() {27 return 'insert' as const;28 }29 30 override buildCommandDocument(_connection: Connection, _session?: ClientSession): Document {31 const options = this.options ?? {};32 const ordered = typeof options.ordered === 'boolean' ? options.ordered : true;33 const command: Document = {34 insert: this.ns.collection,35 documents: this.documents,36 ordered37 };38 39 if (typeof options.bypassDocumentValidation === 'boolean') {40 command.bypassDocumentValidation = options.bypassDocumentValidation;41 }42 43 // we check for undefined specifically here to allow falsy values44 // eslint-disable-next-line no-restricted-syntax45 if (options.comment !== undefined) {46 command.comment = options.comment;47 }48 49 return command;50 }51}52 53/** @public */54export interface InsertOneOptions extends CommandOperationOptions {55 /** Allow driver to bypass schema validation. */56 bypassDocumentValidation?: boolean;57 /** Force server to assign _id values instead of driver. */58 forceServerObjectId?: boolean;59}60 61/** @public */62export interface InsertOneResult<TSchema = Document> {63 /** Indicates whether this write result was acknowledged. If not, then all other members of this result will be undefined */64 acknowledged: boolean;65 /** The identifier that was inserted. If the server generated the identifier, this value will be null as the driver does not have access to that data */66 insertedId: InferIdType<TSchema>;67}68 69export class InsertOneOperation extends InsertOperation {70 constructor(collection: Collection, doc: Document, options: InsertOneOptions) {71 super(collection.s.namespace, [maybeAddIdToDocuments(collection, doc, options)], options);72 }73 74 override handleOk(response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): Document {75 const res = super.handleOk(response);76 if (res.code) throw new MongoServerError(res);77 if (res.writeErrors) {78 // This should be a WriteError but we can't change it now because of error hierarchy79 throw new MongoServerError(res.writeErrors[0]);80 }81 82 return {83 acknowledged: this.writeConcern?.w !== 0,84 insertedId: this.documents[0]._id85 };86 }87}88 89/** @public */90export interface InsertManyResult<TSchema = Document> {91 /** Indicates whether this write result was acknowledged. If not, then all other members of this result will be undefined */92 acknowledged: boolean;93 /** The number of inserted documents for this operations */94 insertedCount: number;95 /** Map of the index of the inserted document to the id of the inserted document */96 insertedIds: { [key: number]: InferIdType<TSchema> };97}98 99defineAspects(InsertOperation, [100 Aspect.RETRYABLE,101 Aspect.WRITE_OPERATION,102 Aspect.SUPPORTS_RAW_DATA103]);104defineAspects(InsertOneOperation, [105 Aspect.RETRYABLE,106 Aspect.WRITE_OPERATION,107 Aspect.SUPPORTS_RAW_DATA108]);109 