From c7c5dae58475244c560484c34d0d575811709e39 Mon Sep 17 00:00:00 2001 From: Malik Javaid <67246038+malikj2000@users.noreply.github.com> Date: Fri, 21 Jul 2023 13:43:53 -0400 Subject: [PATCH] refactor(NODE-5480): search index operations to use async syntax (#3780) --- src/operations/search_indexes/create.ts | 26 ++++++------------------- src/operations/search_indexes/drop.ts | 21 +++++--------------- src/operations/search_indexes/update.ts | 21 +++++--------------- 3 files changed, 16 insertions(+), 52 deletions(-) diff --git a/src/operations/search_indexes/create.ts b/src/operations/search_indexes/create.ts index 11120e2de0..1dedfac5cb 100644 --- a/src/operations/search_indexes/create.ts +++ b/src/operations/search_indexes/create.ts @@ -3,8 +3,7 @@ import type { Document } from 'bson'; import type { Collection } from '../../collection'; import type { Server } from '../../sdam/server'; import type { ClientSession } from '../../sessions'; -import type { Callback } from '../../utils'; -import { AbstractCallbackOperation } from '../operation'; +import { AbstractOperation } from '../operation'; /** * @public @@ -18,7 +17,7 @@ export interface SearchIndexDescription { } /** @internal */ -export class CreateSearchIndexesOperation extends AbstractCallbackOperation { +export class CreateSearchIndexesOperation extends AbstractOperation { constructor( private readonly collection: Collection, private readonly descriptions: ReadonlyArray @@ -26,29 +25,16 @@ export class CreateSearchIndexesOperation extends AbstractCallbackOperation - ): void { + override async execute(server: Server, session: ClientSession | undefined): Promise { const namespace = this.collection.fullNamespace; const command = { createSearchIndexes: namespace.collection, indexes: this.descriptions }; - server.command(namespace, command, { session }, (err, res) => { - if (err || !res) { - callback(err); - return; - } + const res = await server.commandAsync(namespace, command, { session }); - const indexesCreated: Array<{ name: string }> = res?.indexesCreated ?? []; - - callback( - undefined, - indexesCreated.map(({ name }) => name) - ); - }); + const indexesCreated: Array<{ name: string }> = res?.indexesCreated ?? []; + return indexesCreated.map(({ name }) => name); } } diff --git a/src/operations/search_indexes/drop.ts b/src/operations/search_indexes/drop.ts index e98f522650..e9ba3cfa48 100644 --- a/src/operations/search_indexes/drop.ts +++ b/src/operations/search_indexes/drop.ts @@ -3,20 +3,15 @@ import type { Document } from 'bson'; import type { Collection } from '../../collection'; import type { Server } from '../../sdam/server'; import type { ClientSession } from '../../sessions'; -import type { Callback } from '../../utils'; -import { AbstractCallbackOperation } from '../operation'; +import { AbstractOperation } from '../operation'; /** @internal */ -export class DropSearchIndexOperation extends AbstractCallbackOperation { +export class DropSearchIndexOperation extends AbstractOperation { constructor(private readonly collection: Collection, private readonly name: string) { super(); } - executeCallback( - server: Server, - session: ClientSession | undefined, - callback: Callback - ): void { + override async execute(server: Server, session: ClientSession | undefined): Promise { const namespace = this.collection.fullNamespace; const command: Document = { @@ -27,13 +22,7 @@ export class DropSearchIndexOperation extends AbstractCallbackOperation { command.name = this.name; } - server.command(namespace, command, { session }, err => { - if (err) { - callback(err); - return; - } - - callback(); - }); + await server.commandAsync(namespace, command, { session }); + return; } } diff --git a/src/operations/search_indexes/update.ts b/src/operations/search_indexes/update.ts index de7c0f055e..a490740ee4 100644 --- a/src/operations/search_indexes/update.ts +++ b/src/operations/search_indexes/update.ts @@ -3,11 +3,10 @@ import type { Document } from 'bson'; import type { Collection } from '../../collection'; import type { Server } from '../../sdam/server'; import type { ClientSession } from '../../sessions'; -import type { Callback } from '../../utils'; -import { AbstractCallbackOperation } from '../operation'; +import { AbstractOperation } from '../operation'; /** @internal */ -export class UpdateSearchIndexOperation extends AbstractCallbackOperation { +export class UpdateSearchIndexOperation extends AbstractOperation { constructor( private readonly collection: Collection, private readonly name: string, @@ -16,11 +15,7 @@ export class UpdateSearchIndexOperation extends AbstractCallbackOperation super(); } - executeCallback( - server: Server, - session: ClientSession | undefined, - callback: Callback - ): void { + override async execute(server: Server, session: ClientSession | undefined): Promise { const namespace = this.collection.fullNamespace; const command = { updateSearchIndex: namespace.collection, @@ -28,13 +23,7 @@ export class UpdateSearchIndexOperation extends AbstractCallbackOperation definition: this.definition }; - server.command(namespace, command, { session }, err => { - if (err) { - callback(err); - return; - } - - callback(); - }); + await server.commandAsync(namespace, command, { session }); + return; } }