-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathcommon_functions.ts
105 lines (96 loc) · 2.87 KB
/
common_functions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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 { Callback, getTopology } from '../utils';
/** @public */
export interface IndexInformationOptions {
full?: boolean;
readPreference?: ReadPreference;
session?: ClientSession;
}
/**
* Retrieves this collections index info.
*
* @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(
db: Db,
name: string,
options: IndexInformationOptions,
callback?: Callback
): void;
export 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 = {};
}
// If we specified full information
const full = options.full == null ? false : options.full;
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;
}
// 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)
);
}
export function prepareDocs(
coll: Collection,
docs: Document[],
options: { forceServerObjectId?: boolean }
): Document[] {
const forceServerObjectId =
typeof options.forceServerObjectId === 'boolean'
? options.forceServerObjectId
: coll.s.db.options?.forceServerObjectId;
// no need to modify the docs if server sets the ObjectId
if (forceServerObjectId === true) {
return docs;
}
return docs.map(doc => {
if (doc._id == null) {
doc._id = coll.s.pkFactory.createPk();
}
return doc;
});
}