Skip to content

Commit d3c725e

Browse files
committed
refactor(NODE-7083): index operations to modernized operation
1 parent a8338ad commit d3c725e

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed
Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import type { Document } from '../../bson';
1+
import { BSONType, type Document } from '../../bson';
2+
import { type Connection } from '../../cmap/connection';
3+
import { MongoDBResponse } from '../../cmap/wire_protocol/responses';
24
import type { Collection } from '../../collection';
3-
import type { Server } from '../../sdam/server';
5+
import type { ServerCommandOptions } from '../../sdam/server';
46
import type { ClientSession } from '../../sessions';
57
import { type TimeoutContext } from '../../timeout';
6-
import { AbstractOperation } from '../operation';
8+
import { ModernizedOperation } from '../operation';
79

810
/**
911
* @public
@@ -20,37 +22,36 @@ export interface SearchIndexDescription extends Document {
2022
}
2123

2224
/** @internal */
23-
export class CreateSearchIndexesOperation extends AbstractOperation<string[]> {
25+
export class CreateSearchIndexesOperation extends ModernizedOperation<string[]> {
26+
override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;
2427
private readonly collection: Collection;
2528
private readonly descriptions: ReadonlyArray<SearchIndexDescription>;
2629

2730
constructor(collection: Collection, descriptions: ReadonlyArray<SearchIndexDescription>) {
2831
super();
2932
this.collection = collection;
3033
this.descriptions = descriptions;
34+
this.ns = collection.fullNamespace;
3135
}
3236

3337
override get commandName() {
3438
return 'createSearchIndexes' as const;
3539
}
3640

37-
override async execute(
38-
server: Server,
39-
session: ClientSession | undefined,
40-
timeoutContext: TimeoutContext
41-
): Promise<string[]> {
41+
override buildCommand(_connection: Connection, _session?: ClientSession): Document {
4242
const namespace = this.collection.fullNamespace;
43-
const command = {
43+
return {
4444
createSearchIndexes: namespace.collection,
4545
indexes: this.descriptions
4646
};
47+
}
4748

48-
const res = await server.command(namespace, command, {
49-
session,
50-
timeoutContext
51-
});
49+
override handleOk(response: MongoDBResponse): string[] {
50+
const indexesCreated = response.get('indexesCreated', BSONType.array, true);
51+
return indexesCreated.toObject().map(({ name }: { name: string }) => name);
52+
}
5253

53-
const indexesCreated: Array<{ name: string }> = res?.indexesCreated ?? [];
54-
return indexesCreated.map(({ name }) => name);
54+
override buildOptions(timeoutContext: TimeoutContext): ServerCommandOptions {
55+
return { session: this.session, timeoutContext };
5556
}
5657
}

0 commit comments

Comments
 (0)