Skip to content

refactor(NODE-5458): refactoring index operations to use async syntax #3774

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jul 27, 2023
Merged
16 changes: 10 additions & 6 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ import {
CreateIndexesOperation,
type CreateIndexesOptions,
CreateIndexOperation,
DropIndexesOperation,
type DropIndexesOptions,
DropIndexOperation,
type IndexDescription,
Expand Down Expand Up @@ -646,11 +645,16 @@ export class Collection<TSchema extends Document = Document> {
*
* @param options - Optional settings for the command
*/
async dropIndexes(options?: DropIndexesOptions): Promise<Document> {
return executeOperation(
this.client,
new DropIndexesOperation(this as TODO_NODE_3286, resolveOptions(this, options))
);
async dropIndexes(options?: DropIndexesOptions): Promise<boolean> {
try {
await executeOperation(
this.client,
new DropIndexOperation(this as TODO_NODE_3286, '*', resolveOptions(this, options))
);
return true;
} catch {
return false;
}
}

/**
Expand Down
67 changes: 15 additions & 52 deletions src/operations/common_functions.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type { Document } from '../bson';
import type { Collection } from '../collection';
import type { Db } from '../db';
import { MongoTopologyClosedError } from '../error';
import type { ReadPreference } from '../read_preference';
import type { ClientSession } from '../sessions';
import { type Callback, getTopology } from '../utils';

/** @public */
export interface IndexInformationOptions {
Expand All @@ -18,66 +16,31 @@ export interface IndexInformationOptions {
* @param db - The Db instance on which to retrieve the index info.
* @param name - The name of the collection.
*/
export function indexInformation(db: Db, name: string, callback: Callback): void;
export function indexInformation(
export async function indexInformation(db: Db, name: string): Promise<any>;
export async function indexInformation(
db: Db,
name: string,
options: IndexInformationOptions,
callback?: Callback
): void;
export function indexInformation(
options?: IndexInformationOptions
): Promise<any>;
export async function indexInformation(
db: Db,
name: string,
_optionsOrCallback: IndexInformationOptions | Callback,
_callback?: Callback
): void {
let options = _optionsOrCallback as IndexInformationOptions;
let callback = _callback as Callback;
if ('function' === typeof _optionsOrCallback) {
callback = _optionsOrCallback;
options?: IndexInformationOptions
): Promise<any> {
if (options == null) {
options = {};
}
// If we specified full information
const full = options.full == null ? false : options.full;
// Get the list of indexes of the specified collection
const indexes = await db.collection(name).listIndexes(options).toArray();
if (full) return indexes;

let topology;
try {
topology = getTopology(db);
} catch (error) {
return callback(error);
}

// Did the user destroy the topology
if (topology.isDestroyed()) return callback(new MongoTopologyClosedError());
// Process all the results from the index command and collection
function processResults(indexes: any) {
// Contains all the information
const info: any = {};
// Process all the indexes
for (let i = 0; i < indexes.length; i++) {
const index = indexes[i];
// Let's unpack the object
info[index.name] = [];
for (const name in index.key) {
info[index.name].push([name, index.key[name]]);
}
}

return info;
const info: Record<string, Array<[string, unknown]>> = {};
for (const index of indexes) {
info[index.name] = Object.entries(index.key);
}

// Get the list of indexes of the specified collection
db.collection(name)
.listIndexes(options)
.toArray()
.then(
indexes => {
if (!Array.isArray(indexes)) return callback(undefined, []);
if (full) return callback(undefined, indexes);
callback(undefined, processResults(indexes));
},
error => callback(error)
);
return info;
}

export function prepareDocs(
Expand Down
Loading