1
1
import type { Document } from '../bson' ;
2
2
import type { Collection } from '../collection' ;
3
3
import type { Db } from '../db' ;
4
- import { MongoTopologyClosedError } from '../error' ;
5
4
import type { ReadPreference } from '../read_preference' ;
6
5
import type { ClientSession } from '../sessions' ;
7
- import { type Callback , getTopology } from '../utils' ;
8
6
9
7
/** @public */
10
8
export interface IndexInformationOptions {
@@ -18,66 +16,31 @@ export interface IndexInformationOptions {
18
16
* @param db - The Db instance on which to retrieve the index info.
19
17
* @param name - The name of the collection.
20
18
*/
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 (
23
21
db : Db ,
24
22
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 (
29
26
db : Db ,
30
27
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 ) {
38
31
options = { } ;
39
32
}
40
33
// If we specified full information
41
34
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 ;
42
38
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 ) ;
67
42
}
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 ;
81
44
}
82
45
83
46
export function prepareDocs (
0 commit comments