git-subtree-dir: main-site git-subtree-mainline: 4d1daa39101c0a85ca6d916f1c31139faf39632a git-subtree-split: 5cefb4d1618bc54ae0e86830421a8c911900302c
56 lines
1.8 KiB
TypeScript
56 lines
1.8 KiB
TypeScript
import { type Connection } from '..';
|
|
import type { Document } from '../bson';
|
|
import { MongoDBResponse } from '../cmap/wire_protocol/responses';
|
|
import { Collection } from '../collection';
|
|
import type { ClientSession } from '../sessions';
|
|
import { MongoDBNamespace } from '../utils';
|
|
import { CommandOperation, type CommandOperationOptions } from './command';
|
|
import { Aspect, defineAspects } from './operation';
|
|
|
|
/** @public */
|
|
export interface RenameOptions extends Omit<CommandOperationOptions, 'rawData'> {
|
|
/** Drop the target name collection if it previously exists. */
|
|
dropTarget?: boolean;
|
|
/** Unclear */
|
|
new_collection?: boolean;
|
|
}
|
|
|
|
/** @internal */
|
|
export class RenameOperation extends CommandOperation<Document> {
|
|
override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;
|
|
collection: Collection;
|
|
newName: string;
|
|
override options: RenameOptions;
|
|
|
|
constructor(collection: Collection, newName: string, options: RenameOptions) {
|
|
super(collection, options);
|
|
this.collection = collection;
|
|
this.newName = newName;
|
|
this.options = options;
|
|
this.ns = new MongoDBNamespace('admin', '$cmd');
|
|
}
|
|
|
|
override get commandName(): string {
|
|
return 'renameCollection' as const;
|
|
}
|
|
|
|
override buildCommandDocument(_connection: Connection, _session?: ClientSession): Document {
|
|
const renameCollection = this.collection.namespace;
|
|
const to = this.collection.s.namespace.withCollection(this.newName).toString();
|
|
const dropTarget =
|
|
typeof this.options.dropTarget === 'boolean' ? this.options.dropTarget : false;
|
|
|
|
return {
|
|
renameCollection,
|
|
to,
|
|
dropTarget
|
|
};
|
|
}
|
|
|
|
override handleOk(_response: InstanceType<typeof this.SERVER_COMMAND_RESPONSE_TYPE>): Document {
|
|
return new Collection(this.collection.db, this.newName, this.collection.s.options);
|
|
}
|
|
}
|
|
|
|
defineAspects(RenameOperation, [Aspect.WRITE_OPERATION]);
|