11import type { Document } from '../bson' ;
22import type { Collection } from '../collection' ;
33import type { Db } from '../db' ;
4- import { MongoTopologyClosedError } from '../error' ;
54import type { ReadPreference } from '../read_preference' ;
65import type { ClientSession } from '../sessions' ;
7- import { type Callback , getTopology } from '../utils' ;
86
97/** @public */
108export 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
8346export function prepareDocs (
0 commit comments