Skip to content

Commit ada1f75

Browse files
refactor(NODE-5458): refactoring index operations to use async syntax (#3774)
Co-authored-by: Bailey Pearson <bailey.pearson@mongodb.com>
1 parent 65aa288 commit ada1f75

File tree

4 files changed

+201
-215
lines changed

4 files changed

+201
-215
lines changed

src/collection.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ import {
5353
CreateIndexesOperation,
5454
type CreateIndexesOptions,
5555
CreateIndexOperation,
56-
DropIndexesOperation,
5756
type DropIndexesOptions,
5857
DropIndexOperation,
5958
type IndexDescription,
@@ -646,11 +645,16 @@ export class Collection<TSchema extends Document = Document> {
646645
*
647646
* @param options - Optional settings for the command
648647
*/
649-
async dropIndexes(options?: DropIndexesOptions): Promise<Document> {
650-
return executeOperation(
651-
this.client,
652-
new DropIndexesOperation(this as TODO_NODE_3286, resolveOptions(this, options))
653-
);
648+
async dropIndexes(options?: DropIndexesOptions): Promise<boolean> {
649+
try {
650+
await executeOperation(
651+
this.client,
652+
new DropIndexOperation(this as TODO_NODE_3286, '*', resolveOptions(this, options))
653+
);
654+
return true;
655+
} catch {
656+
return false;
657+
}
654658
}
655659

656660
/**

src/operations/common_functions.ts

Lines changed: 15 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import type { Document } from '../bson';
22
import type { Collection } from '../collection';
33
import type { Db } from '../db';
4-
import { MongoTopologyClosedError } from '../error';
54
import type { ReadPreference } from '../read_preference';
65
import type { ClientSession } from '../sessions';
7-
import { type Callback, getTopology } from '../utils';
86

97
/** @public */
108
export interface IndexInformationOptions {
@@ -18,66 +16,31 @@ export interface IndexInformationOptions {
1816
* @param db - The Db instance on which to retrieve the index info.
1917
* @param name - The name of the collection.
2018
*/
21-
export function indexInformation(db: Db, name: string, callback: Callback): void;
22-
export function indexInformation(
19+
export async function indexInformation(db: Db, name: string): Promise<any>;
20+
export async function indexInformation(
2321
db: Db,
2422
name: string,
25-
options: IndexInformationOptions,
26-
callback?: Callback
27-
): void;
28-
export function indexInformation(
23+
options?: IndexInformationOptions
24+
): Promise<any>;
25+
export async function indexInformation(
2926
db: Db,
3027
name: string,
31-
_optionsOrCallback: IndexInformationOptions | Callback,
32-
_callback?: Callback
33-
): void {
34-
let options = _optionsOrCallback as IndexInformationOptions;
35-
let callback = _callback as Callback;
36-
if ('function' === typeof _optionsOrCallback) {
37-
callback = _optionsOrCallback;
28+
options?: IndexInformationOptions
29+
): Promise<any> {
30+
if (options == null) {
3831
options = {};
3932
}
4033
// If we specified full information
4134
const full = options.full == null ? false : options.full;
35+
// Get the list of indexes of the specified collection
36+
const indexes = await db.collection(name).listIndexes(options).toArray();
37+
if (full) return indexes;
4238

43-
let topology;
44-
try {
45-
topology = getTopology(db);
46-
} catch (error) {
47-
return callback(error);
48-
}
49-
50-
// Did the user destroy the topology
51-
if (topology.isDestroyed()) return callback(new MongoTopologyClosedError());
52-
// Process all the results from the index command and collection
53-
function processResults(indexes: any) {
54-
// Contains all the information
55-
const info: any = {};
56-
// Process all the indexes
57-
for (let i = 0; i < indexes.length; i++) {
58-
const index = indexes[i];
59-
// Let's unpack the object
60-
info[index.name] = [];
61-
for (const name in index.key) {
62-
info[index.name].push([name, index.key[name]]);
63-
}
64-
}
65-
66-
return info;
39+
const info: Record<string, Array<[string, unknown]>> = {};
40+
for (const index of indexes) {
41+
info[index.name] = Object.entries(index.key);
6742
}
68-
69-
// Get the list of indexes of the specified collection
70-
db.collection(name)
71-
.listIndexes(options)
72-
.toArray()
73-
.then(
74-
indexes => {
75-
if (!Array.isArray(indexes)) return callback(undefined, []);
76-
if (full) return callback(undefined, indexes);
77-
callback(undefined, processResults(indexes));
78-
},
79-
error => callback(error)
80-
);
43+
return info;
8144
}
8245

8346
export function prepareDocs(

0 commit comments

Comments
 (0)