-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathrename.ts
More file actions
60 lines (52 loc) · 1.99 KB
/
rename.ts
File metadata and controls
60 lines (52 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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;
/**
* @deprecated
*
* This option has been dead code since at least Node driver version 4.x. It will
* be removed in a future major release.
*/
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]);