Skip to content

Commit

Permalink
refactor(NODE-5458): refactoring index operations to use async syntax (
Browse files Browse the repository at this point in the history
…#3774)

Co-authored-by: Bailey Pearson <bailey.pearson@mongodb.com>
  • Loading branch information
malikj2000 and baileympearson authored Jul 27, 2023
1 parent 65aa288 commit ada1f75
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 215 deletions.
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

0 comments on commit ada1f75

Please sign in to comment.