Skip to content

Commit

Permalink
refactor(NODE-5480): search index operations to use async syntax (#3780)
Browse files Browse the repository at this point in the history
  • Loading branch information
malikj2000 authored Jul 21, 2023
1 parent 6a5c492 commit c7c5dae
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 52 deletions.
26 changes: 6 additions & 20 deletions src/operations/search_indexes/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,37 +17,24 @@ export interface SearchIndexDescription {
}

/** @internal */
export class CreateSearchIndexesOperation extends AbstractCallbackOperation<string[]> {
export class CreateSearchIndexesOperation extends AbstractOperation<string[]> {
constructor(
private readonly collection: Collection,
private readonly descriptions: ReadonlyArray<SearchIndexDescription>
) {
super();
}

executeCallback(
server: Server,
session: ClientSession | undefined,
callback: Callback<string[]>
): void {
override async execute(server: Server, session: ClientSession | undefined): Promise<string[]> {
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);
}
}
21 changes: 5 additions & 16 deletions src/operations/search_indexes/drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
export class DropSearchIndexOperation extends AbstractOperation<void> {
constructor(private readonly collection: Collection, private readonly name: string) {
super();
}

executeCallback(
server: Server,
session: ClientSession | undefined,
callback: Callback<void>
): void {
override async execute(server: Server, session: ClientSession | undefined): Promise<void> {
const namespace = this.collection.fullNamespace;

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

server.command(namespace, command, { session }, err => {
if (err) {
callback(err);
return;
}

callback();
});
await server.commandAsync(namespace, command, { session });
return;
}
}
21 changes: 5 additions & 16 deletions src/operations/search_indexes/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
export class UpdateSearchIndexOperation extends AbstractOperation<void> {
constructor(
private readonly collection: Collection,
private readonly name: string,
Expand All @@ -16,25 +15,15 @@ export class UpdateSearchIndexOperation extends AbstractCallbackOperation<void>
super();
}

executeCallback(
server: Server,
session: ClientSession | undefined,
callback: Callback<void>
): void {
override async execute(server: Server, session: ClientSession | undefined): Promise<void> {
const namespace = this.collection.fullNamespace;
const command = {
updateSearchIndex: namespace.collection,
name: this.name,
definition: this.definition
};

server.command(namespace, command, { session }, err => {
if (err) {
callback(err);
return;
}

callback();
});
await server.commandAsync(namespace, command, { session });
return;
}
}

0 comments on commit c7c5dae

Please sign in to comment.