Skip to content

Commit c7c5dae

Browse files
authored
refactor(NODE-5480): search index operations to use async syntax (#3780)
1 parent 6a5c492 commit c7c5dae

File tree

3 files changed

+16
-52
lines changed

3 files changed

+16
-52
lines changed

src/operations/search_indexes/create.ts

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import type { Document } from 'bson';
33
import type { Collection } from '../../collection';
44
import type { Server } from '../../sdam/server';
55
import type { ClientSession } from '../../sessions';
6-
import type { Callback } from '../../utils';
7-
import { AbstractCallbackOperation } from '../operation';
6+
import { AbstractOperation } from '../operation';
87

98
/**
109
* @public
@@ -18,37 +17,24 @@ export interface SearchIndexDescription {
1817
}
1918

2019
/** @internal */
21-
export class CreateSearchIndexesOperation extends AbstractCallbackOperation<string[]> {
20+
export class CreateSearchIndexesOperation extends AbstractOperation<string[]> {
2221
constructor(
2322
private readonly collection: Collection,
2423
private readonly descriptions: ReadonlyArray<SearchIndexDescription>
2524
) {
2625
super();
2726
}
2827

29-
executeCallback(
30-
server: Server,
31-
session: ClientSession | undefined,
32-
callback: Callback<string[]>
33-
): void {
28+
override async execute(server: Server, session: ClientSession | undefined): Promise<string[]> {
3429
const namespace = this.collection.fullNamespace;
3530
const command = {
3631
createSearchIndexes: namespace.collection,
3732
indexes: this.descriptions
3833
};
3934

40-
server.command(namespace, command, { session }, (err, res) => {
41-
if (err || !res) {
42-
callback(err);
43-
return;
44-
}
35+
const res = await server.commandAsync(namespace, command, { session });
4536

46-
const indexesCreated: Array<{ name: string }> = res?.indexesCreated ?? [];
47-
48-
callback(
49-
undefined,
50-
indexesCreated.map(({ name }) => name)
51-
);
52-
});
37+
const indexesCreated: Array<{ name: string }> = res?.indexesCreated ?? [];
38+
return indexesCreated.map(({ name }) => name);
5339
}
5440
}

src/operations/search_indexes/drop.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,15 @@ import type { Document } from 'bson';
33
import type { Collection } from '../../collection';
44
import type { Server } from '../../sdam/server';
55
import type { ClientSession } from '../../sessions';
6-
import type { Callback } from '../../utils';
7-
import { AbstractCallbackOperation } from '../operation';
6+
import { AbstractOperation } from '../operation';
87

98
/** @internal */
10-
export class DropSearchIndexOperation extends AbstractCallbackOperation<void> {
9+
export class DropSearchIndexOperation extends AbstractOperation<void> {
1110
constructor(private readonly collection: Collection, private readonly name: string) {
1211
super();
1312
}
1413

15-
executeCallback(
16-
server: Server,
17-
session: ClientSession | undefined,
18-
callback: Callback<void>
19-
): void {
14+
override async execute(server: Server, session: ClientSession | undefined): Promise<void> {
2015
const namespace = this.collection.fullNamespace;
2116

2217
const command: Document = {
@@ -27,13 +22,7 @@ export class DropSearchIndexOperation extends AbstractCallbackOperation<void> {
2722
command.name = this.name;
2823
}
2924

30-
server.command(namespace, command, { session }, err => {
31-
if (err) {
32-
callback(err);
33-
return;
34-
}
35-
36-
callback();
37-
});
25+
await server.commandAsync(namespace, command, { session });
26+
return;
3827
}
3928
}

src/operations/search_indexes/update.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ import type { Document } from 'bson';
33
import type { Collection } from '../../collection';
44
import type { Server } from '../../sdam/server';
55
import type { ClientSession } from '../../sessions';
6-
import type { Callback } from '../../utils';
7-
import { AbstractCallbackOperation } from '../operation';
6+
import { AbstractOperation } from '../operation';
87

98
/** @internal */
10-
export class UpdateSearchIndexOperation extends AbstractCallbackOperation<void> {
9+
export class UpdateSearchIndexOperation extends AbstractOperation<void> {
1110
constructor(
1211
private readonly collection: Collection,
1312
private readonly name: string,
@@ -16,25 +15,15 @@ export class UpdateSearchIndexOperation extends AbstractCallbackOperation<void>
1615
super();
1716
}
1817

19-
executeCallback(
20-
server: Server,
21-
session: ClientSession | undefined,
22-
callback: Callback<void>
23-
): void {
18+
override async execute(server: Server, session: ClientSession | undefined): Promise<void> {
2419
const namespace = this.collection.fullNamespace;
2520
const command = {
2621
updateSearchIndex: namespace.collection,
2722
name: this.name,
2823
definition: this.definition
2924
};
3025

31-
server.command(namespace, command, { session }, err => {
32-
if (err) {
33-
callback(err);
34-
return;
35-
}
36-
37-
callback();
38-
});
26+
await server.commandAsync(namespace, command, { session });
27+
return;
3928
}
4029
}

0 commit comments

Comments
 (0)