opusdev/vector-similarity-api
1
1import { type Connection } from '..';2import type { Document } from '../bson';3import { MongoDBResponse } from '../cmap/wire_protocol/responses';4import { Collection } from '../collection';5import type { ClientSession } from '../sessions';6import { MongoDBNamespace } from '../utils';7import { CommandOperation, type CommandOperationOptions } from './command';8import { Aspect, defineAspects } from './operation';9 10/** @public */11export interface RenameOptions extends Omit<CommandOperationOptions, 'rawData'> {12 /** Drop the target name collection if it previously exists. */13 dropTarget?: boolean;14 /** Unclear */15 new_collection?: boolean;16}17 18/** @internal */19export class RenameOperation extends CommandOperation<Document> {20 override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;21 collection: Collection;22 newName: string;23 override options: RenameOptions;24 25 constructor(collection: Collection, newName: string, options: RenameOptions) {26 super(collection, options);27 this.collection = collection;28 this.newName = newName;29 this.options = options;30 this.ns = new MongoDBNamespace('admin', '$cmd');31 }32 33 override get commandName(): string {34 return 'renameCollection' as const;35 }36 37 override buildCommandDocument(_connection: Connection, _session?: ClientSession): Document {38 const renameCollection = this.collection.namespace;39 const to = this.collection.s.namespace.withCollection(this.newName).toString();40 const dropTarget =41 typeof this.options.dropTarget === 'boolean' ? this.options.dropTarget : false;42 43 return {44 renameCollection,45 to,46 dropTarget47 };48 }49 50 override handleOk(_response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): Document {51 return new Collection(this.collection.db, this.newName, this.collection.s.options);52 }53}54 55defineAspects(RenameOperation, [Aspect.WRITE_OPERATION]);56 