diff --git a/src/operations/indexes.ts b/src/operations/indexes.ts index 3dc1db8ba2..15252e53b7 100644 --- a/src/operations/indexes.ts +++ b/src/operations/indexes.ts @@ -1,17 +1,13 @@ import type { Document } from '../bson'; import { type Connection } from '../cmap/connection'; -import { CursorResponse } from '../cmap/wire_protocol/responses'; +import { CursorResponse, MongoDBResponse } from '../cmap/wire_protocol/responses'; import type { Collection } from '../collection'; import { type AbstractCursorOptions } from '../cursor/abstract_cursor'; import { MongoCompatibilityError } from '../error'; import { type OneOrMore } from '../mongo_types'; -import type { Server } from '../sdam/server'; -import type { ClientSession } from '../sessions'; -import { type TimeoutContext } from '../timeout'; import { isObject, maxWireVersion, type MongoDBNamespace } from '../utils'; import { type CollationOptions, - CommandOperation, type CommandOperationOptions, ModernizedCommandOperation, type OperationParent @@ -246,7 +242,8 @@ type ResolvedIndexDescription = Omit & { }; /** @internal */ -export class CreateIndexesOperation extends CommandOperation { +export class CreateIndexesOperation extends ModernizedCommandOperation { + override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse; override options: CreateIndexesOptions; collectionName: string; indexes: ReadonlyArray; @@ -260,6 +257,8 @@ export class CreateIndexesOperation extends CommandOperation { super(parent, options); this.options = options ?? {}; + // collation is set on each index, it should not be defined at the root + this.options.collation = undefined; this.collectionName = collectionName; this.indexes = indexes.map((userIndex: IndexDescription): ResolvedIndexDescription => { // Ensure the key is a Map to preserve index key ordering @@ -273,6 +272,7 @@ export class CreateIndexesOperation extends CommandOperation { key }; }); + this.ns = parent.s.namespace; } static fromIndexDescriptionArray( @@ -299,15 +299,11 @@ export class CreateIndexesOperation extends CommandOperation { return 'createIndexes'; } - override async execute( - server: Server, - session: ClientSession | undefined, - timeoutContext: TimeoutContext - ): Promise { + override buildCommandDocument(connection: Connection): Document { const options = this.options; const indexes = this.indexes; - const serverWireVersion = maxWireVersion(server); + const serverWireVersion = maxWireVersion(connection); const cmd: Document = { createIndexes: this.collectionName, indexes }; @@ -319,13 +315,11 @@ export class CreateIndexesOperation extends CommandOperation { } cmd.commitQuorum = options.commitQuorum; } + return cmd; + } - // collation is set on each index, it should not be defined at the root - this.options.collation = undefined; - - await super.executeCommand(server, session, cmd, timeoutContext); - - const indexNames = indexes.map(index => index.name || ''); + override handleOk(_response: InstanceType): string[] { + const indexNames = this.indexes.map(index => index.name || ''); return indexNames; } } @@ -334,7 +328,8 @@ export class CreateIndexesOperation extends CommandOperation { export type DropIndexesOptions = CommandOperationOptions; /** @internal */ -export class DropIndexOperation extends CommandOperation { +export class DropIndexOperation extends ModernizedCommandOperation { + override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse; override options: DropIndexesOptions; collection: Collection; indexName: string; @@ -345,19 +340,15 @@ export class DropIndexOperation extends CommandOperation { this.options = options ?? {}; this.collection = collection; this.indexName = indexName; + this.ns = collection.fullNamespace; } override get commandName() { return 'dropIndexes' as const; } - override async execute( - server: Server, - session: ClientSession | undefined, - timeoutContext: TimeoutContext - ): Promise { - const cmd = { dropIndexes: this.collection.collectionName, index: this.indexName }; - return await super.executeCommand(server, session, cmd, timeoutContext); + override buildCommandDocument(_connection: Connection): Document { + return { dropIndexes: this.collection.collectionName, index: this.indexName }; } } diff --git a/src/operations/search_indexes/create.ts b/src/operations/search_indexes/create.ts index 95f91c2d19..4fde15b8ff 100644 --- a/src/operations/search_indexes/create.ts +++ b/src/operations/search_indexes/create.ts @@ -1,9 +1,11 @@ -import type { Document } from '../../bson'; +import { type Document } from '../../bson'; +import { type Connection } from '../../cmap/connection'; +import { MongoDBResponse } from '../../cmap/wire_protocol/responses'; import type { Collection } from '../../collection'; -import type { Server } from '../../sdam/server'; +import type { ServerCommandOptions } from '../../sdam/server'; import type { ClientSession } from '../../sessions'; import { type TimeoutContext } from '../../timeout'; -import { AbstractOperation } from '../operation'; +import { ModernizedOperation } from '../operation'; /** * @public @@ -20,7 +22,8 @@ export interface SearchIndexDescription extends Document { } /** @internal */ -export class CreateSearchIndexesOperation extends AbstractOperation { +export class CreateSearchIndexesOperation extends ModernizedOperation { + override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse; private readonly collection: Collection; private readonly descriptions: ReadonlyArray; @@ -28,29 +31,26 @@ export class CreateSearchIndexesOperation extends AbstractOperation { super(); this.collection = collection; this.descriptions = descriptions; + this.ns = collection.fullNamespace; } override get commandName() { return 'createSearchIndexes' as const; } - override async execute( - server: Server, - session: ClientSession | undefined, - timeoutContext: TimeoutContext - ): Promise { + override buildCommand(_connection: Connection, _session?: ClientSession): Document { const namespace = this.collection.fullNamespace; - const command = { + return { createSearchIndexes: namespace.collection, indexes: this.descriptions }; + } - const res = await server.command(namespace, command, { - session, - timeoutContext - }); + override handleOk(response: InstanceType): string[] { + return super.handleOk(response).indexesCreated.map((val: { name: string }) => val.name); + } - const indexesCreated: Array<{ name: string }> = res?.indexesCreated ?? []; - return indexesCreated.map(({ name }) => name); + override buildOptions(timeoutContext: TimeoutContext): ServerCommandOptions { + return { session: this.session, timeoutContext }; } } diff --git a/src/operations/search_indexes/update.ts b/src/operations/search_indexes/update.ts index dfc2c848db..b4d5738ffd 100644 --- a/src/operations/search_indexes/update.ts +++ b/src/operations/search_indexes/update.ts @@ -1,12 +1,15 @@ import type { Document } from '../../bson'; +import { type Connection } from '../../cmap/connection'; +import { MongoDBResponse } from '../../cmap/wire_protocol/responses'; import type { Collection } from '../../collection'; -import type { Server } from '../../sdam/server'; +import type { ServerCommandOptions } from '../../sdam/server'; import type { ClientSession } from '../../sessions'; import { type TimeoutContext } from '../../timeout'; -import { AbstractOperation } from '../operation'; +import { ModernizedOperation } from '../operation'; /** @internal */ -export class UpdateSearchIndexOperation extends AbstractOperation { +export class UpdateSearchIndexOperation extends ModernizedOperation { + override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse; private readonly collection: Collection; private readonly name: string; private readonly definition: Document; @@ -16,25 +19,27 @@ export class UpdateSearchIndexOperation extends AbstractOperation { this.collection = collection; this.name = name; this.definition = definition; + this.ns = collection.fullNamespace; } override get commandName() { return 'updateSearchIndex' as const; } - override async execute( - server: Server, - session: ClientSession | undefined, - timeoutContext: TimeoutContext - ): Promise { + override buildCommand(_connection: Connection, _session?: ClientSession): Document { const namespace = this.collection.fullNamespace; - const command = { + return { updateSearchIndex: namespace.collection, name: this.name, definition: this.definition }; + } + + override handleOk(_response: MongoDBResponse): void { + // no response. + } - await server.command(namespace, command, { session, timeoutContext }); - return; + override buildOptions(timeoutContext: TimeoutContext): ServerCommandOptions { + return { session: this.session, timeoutContext }; } }