All files / src/operations rename.ts

100% Statements 18/18
100% Branches 2/2
100% Functions 3/3
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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  392x       392x 392x 392x                     392x   1634x 1634x 1634x   1634x 1634x       1863x                 1405x 1405x   1405x   1405x           1405x 947x       392x  
import type { Document } from '../bson';
import { Collection } from '../collection';
import type { Server } from '../sdam/server';
import type { ClientSession } from '../sessions';
import { type TimeoutContext } from '../timeout';
import { MongoDBNamespace } from '../utils';
import { CommandOperation, type CommandOperationOptions } from './command';
import { Aspect, defineAspects } from './operation';
 
/** @public */
export interface RenameOptions extends CommandOperationOptions {
  /** Drop the target name collection if it previously exists. */
  dropTarget?: boolean;
  /** Unclear */
  new_collection?: boolean;
}
 
/** @internal */
export class RenameOperation extends CommandOperation<Document> {
  constructor(
    public collection: Collection,
    public newName: string,
    public override options: RenameOptions
  ) {
    super(collection, options);
    this.ns = new MongoDBNamespace('admin', '$cmd');
  }
 
  override get commandName(): string {
    return 'renameCollection' as const;
  }
 
  override async execute(
    server: Server,
    session: ClientSession | undefined,
    timeoutContext: TimeoutContext
  ): Promise<Collection> {
    // Build the command
    const renameCollection = this.collection.namespace;
    const toCollection = this.collection.s.namespace.withCollection(this.newName).toString();
    const dropTarget =
      typeof this.options.dropTarget === 'boolean' ? this.options.dropTarget : false;
 
    const command = {
      renameCollection: renameCollection,
      to: toCollection,
      dropTarget: dropTarget
    };
 
    await super.executeCommand(server, session, command, timeoutContext);
    return new Collection(this.collection.s.db, this.newName, this.collection.s.options);
  }
}
 
defineAspects(RenameOperation, [Aspect.WRITE_OPERATION]);