mongodb 6.5.0 → 6.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (207) hide show
  1. package/README.md +9 -9
  2. package/lib/admin.js +9 -9
  3. package/lib/admin.js.map +1 -1
  4. package/lib/bson.js +33 -22
  5. package/lib/bson.js.map +1 -1
  6. package/lib/bulk/common.js +13 -12
  7. package/lib/bulk/common.js.map +1 -1
  8. package/lib/change_stream.js +28 -18
  9. package/lib/change_stream.js.map +1 -1
  10. package/lib/client-side-encryption/auto_encrypter.js +2 -2
  11. package/lib/client-side-encryption/auto_encrypter.js.map +1 -1
  12. package/lib/client-side-encryption/client_encryption.js +6 -6
  13. package/lib/client-side-encryption/client_encryption.js.map +1 -1
  14. package/lib/client-side-encryption/providers/aws.js +13 -10
  15. package/lib/client-side-encryption/providers/aws.js.map +1 -1
  16. package/lib/client-side-encryption/providers/azure.js +6 -3
  17. package/lib/client-side-encryption/providers/azure.js.map +1 -1
  18. package/lib/cmap/auth/aws_temporary_credentials.js +140 -0
  19. package/lib/cmap/auth/aws_temporary_credentials.js.map +1 -0
  20. package/lib/cmap/auth/gssapi.js +7 -6
  21. package/lib/cmap/auth/gssapi.js.map +1 -1
  22. package/lib/cmap/auth/mongodb_aws.js +8 -101
  23. package/lib/cmap/auth/mongodb_aws.js.map +1 -1
  24. package/lib/cmap/auth/mongodb_oidc/aws_service_workflow.js +1 -1
  25. package/lib/cmap/auth/mongodb_oidc/aws_service_workflow.js.map +1 -1
  26. package/lib/cmap/auth/mongodb_oidc/callback_lock_cache.js +2 -1
  27. package/lib/cmap/auth/mongodb_oidc/callback_lock_cache.js.map +1 -1
  28. package/lib/cmap/auth/mongodb_oidc/service_workflow.js +1 -1
  29. package/lib/cmap/auth/mongodb_oidc/service_workflow.js.map +1 -1
  30. package/lib/cmap/auth/scram.js +2 -2
  31. package/lib/cmap/auth/scram.js.map +1 -1
  32. package/lib/cmap/commands.js +24 -111
  33. package/lib/cmap/commands.js.map +1 -1
  34. package/lib/cmap/connect.js +4 -4
  35. package/lib/cmap/connect.js.map +1 -1
  36. package/lib/cmap/connection.js +61 -36
  37. package/lib/cmap/connection.js.map +1 -1
  38. package/lib/cmap/connection_pool.js +22 -13
  39. package/lib/cmap/connection_pool.js.map +1 -1
  40. package/lib/cmap/handshake/client_metadata.js +2 -2
  41. package/lib/cmap/handshake/client_metadata.js.map +1 -1
  42. package/lib/cmap/wire_protocol/compression.js +8 -8
  43. package/lib/cmap/wire_protocol/compression.js.map +1 -1
  44. package/lib/cmap/wire_protocol/on_demand/document.js +218 -0
  45. package/lib/cmap/wire_protocol/on_demand/document.js.map +1 -0
  46. package/lib/cmap/wire_protocol/responses.js +184 -0
  47. package/lib/cmap/wire_protocol/responses.js.map +1 -0
  48. package/lib/collection.js +42 -38
  49. package/lib/collection.js.map +1 -1
  50. package/lib/connection_string.js +4 -6
  51. package/lib/connection_string.js.map +1 -1
  52. package/lib/cursor/abstract_cursor.js +76 -43
  53. package/lib/cursor/abstract_cursor.js.map +1 -1
  54. package/lib/cursor/aggregation_cursor.js +16 -33
  55. package/lib/cursor/aggregation_cursor.js.map +1 -1
  56. package/lib/cursor/find_cursor.js +36 -18
  57. package/lib/cursor/find_cursor.js.map +1 -1
  58. package/lib/cursor/run_command_cursor.js +3 -2
  59. package/lib/cursor/run_command_cursor.js.map +1 -1
  60. package/lib/db.js +15 -19
  61. package/lib/db.js.map +1 -1
  62. package/lib/deps.js +31 -26
  63. package/lib/deps.js.map +1 -1
  64. package/lib/encrypter.js +14 -5
  65. package/lib/encrypter.js.map +1 -1
  66. package/lib/error.js +4 -3
  67. package/lib/error.js.map +1 -1
  68. package/lib/gridfs/download.js +19 -14
  69. package/lib/gridfs/download.js.map +1 -1
  70. package/lib/gridfs/index.js.map +1 -1
  71. package/lib/gridfs/upload.js +6 -1
  72. package/lib/gridfs/upload.js.map +1 -1
  73. package/lib/index.js.map +1 -1
  74. package/lib/mongo_client.js +11 -7
  75. package/lib/mongo_client.js.map +1 -1
  76. package/lib/mongo_logger.js +3 -0
  77. package/lib/mongo_logger.js.map +1 -1
  78. package/lib/operations/aggregate.js +2 -1
  79. package/lib/operations/aggregate.js.map +1 -1
  80. package/lib/operations/command.js +1 -1
  81. package/lib/operations/command.js.map +1 -1
  82. package/lib/operations/create_collection.js +1 -1
  83. package/lib/operations/create_collection.js.map +1 -1
  84. package/lib/operations/delete.js +4 -3
  85. package/lib/operations/delete.js.map +1 -1
  86. package/lib/operations/drop.js +1 -1
  87. package/lib/operations/drop.js.map +1 -1
  88. package/lib/operations/execute_operation.js +23 -8
  89. package/lib/operations/execute_operation.js.map +1 -1
  90. package/lib/operations/find.js +4 -4
  91. package/lib/operations/find.js.map +1 -1
  92. package/lib/operations/get_more.js +2 -1
  93. package/lib/operations/get_more.js.map +1 -1
  94. package/lib/operations/indexes.js +29 -121
  95. package/lib/operations/indexes.js.map +1 -1
  96. package/lib/operations/insert.js +3 -3
  97. package/lib/operations/insert.js.map +1 -1
  98. package/lib/operations/kill_cursors.js +3 -1
  99. package/lib/operations/kill_cursors.js.map +1 -1
  100. package/lib/operations/list_collections.js +1 -1
  101. package/lib/operations/list_collections.js.map +1 -1
  102. package/lib/operations/list_databases.js +1 -1
  103. package/lib/operations/list_databases.js.map +1 -1
  104. package/lib/operations/operation.js.map +1 -1
  105. package/lib/operations/run_command.js +4 -2
  106. package/lib/operations/run_command.js.map +1 -1
  107. package/lib/operations/search_indexes/create.js.map +1 -1
  108. package/lib/operations/stats.js +1 -1
  109. package/lib/operations/stats.js.map +1 -1
  110. package/lib/operations/update.js +1 -1
  111. package/lib/operations/update.js.map +1 -1
  112. package/lib/sdam/common.js.map +1 -1
  113. package/lib/sdam/monitor.js +139 -42
  114. package/lib/sdam/monitor.js.map +1 -1
  115. package/lib/sdam/server.js +5 -15
  116. package/lib/sdam/server.js.map +1 -1
  117. package/lib/sdam/server_description.js +1 -0
  118. package/lib/sdam/server_description.js.map +1 -1
  119. package/lib/sdam/server_selection.js +1 -1
  120. package/lib/sdam/server_selection.js.map +1 -1
  121. package/lib/sdam/srv_polling.js +2 -1
  122. package/lib/sdam/srv_polling.js.map +1 -1
  123. package/lib/sdam/topology.js +67 -54
  124. package/lib/sdam/topology.js.map +1 -1
  125. package/lib/sdam/topology_description.js +10 -0
  126. package/lib/sdam/topology_description.js.map +1 -1
  127. package/lib/sessions.js +133 -93
  128. package/lib/sessions.js.map +1 -1
  129. package/lib/timeout.js +77 -0
  130. package/lib/timeout.js.map +1 -0
  131. package/lib/utils.js +61 -28
  132. package/lib/utils.js.map +1 -1
  133. package/mongodb.d.ts +150 -38
  134. package/package.json +16 -13
  135. package/src/admin.ts +9 -9
  136. package/src/bson.ts +14 -0
  137. package/src/bulk/common.ts +3 -2
  138. package/src/change_stream.ts +39 -30
  139. package/src/client-side-encryption/auto_encrypter.ts +2 -2
  140. package/src/client-side-encryption/client_encryption.ts +6 -6
  141. package/src/client-side-encryption/providers/aws.ts +17 -10
  142. package/src/client-side-encryption/providers/azure.ts +5 -3
  143. package/src/cmap/auth/aws_temporary_credentials.ts +169 -0
  144. package/src/cmap/auth/gssapi.ts +9 -11
  145. package/src/cmap/auth/mongodb_aws.ts +19 -126
  146. package/src/cmap/auth/mongodb_oidc/aws_service_workflow.ts +1 -1
  147. package/src/cmap/auth/mongodb_oidc/callback_lock_cache.ts +2 -1
  148. package/src/cmap/auth/mongodb_oidc/service_workflow.ts +1 -1
  149. package/src/cmap/auth/scram.ts +2 -2
  150. package/src/cmap/commands.ts +28 -132
  151. package/src/cmap/connect.ts +4 -4
  152. package/src/cmap/connection.ts +107 -43
  153. package/src/cmap/connection_pool.ts +32 -29
  154. package/src/cmap/handshake/client_metadata.ts +2 -5
  155. package/src/cmap/wire_protocol/compression.ts +11 -13
  156. package/src/cmap/wire_protocol/on_demand/document.ts +338 -0
  157. package/src/cmap/wire_protocol/responses.ts +237 -0
  158. package/src/collection.ts +87 -58
  159. package/src/connection_string.ts +9 -7
  160. package/src/cursor/abstract_cursor.ts +102 -38
  161. package/src/cursor/aggregation_cursor.ts +32 -34
  162. package/src/cursor/find_cursor.ts +33 -21
  163. package/src/cursor/list_search_indexes_cursor.ts +1 -1
  164. package/src/cursor/run_command_cursor.ts +3 -2
  165. package/src/db.ts +42 -21
  166. package/src/deps.ts +52 -40
  167. package/src/encrypter.ts +14 -5
  168. package/src/error.ts +9 -3
  169. package/src/gridfs/download.ts +19 -31
  170. package/src/gridfs/index.ts +2 -0
  171. package/src/gridfs/upload.ts +11 -8
  172. package/src/index.ts +13 -5
  173. package/src/mongo_client.ts +21 -15
  174. package/src/mongo_logger.ts +3 -0
  175. package/src/mongo_types.ts +1 -1
  176. package/src/operations/aggregate.ts +2 -1
  177. package/src/operations/command.ts +1 -1
  178. package/src/operations/create_collection.ts +7 -2
  179. package/src/operations/delete.ts +4 -3
  180. package/src/operations/drop.ts +1 -1
  181. package/src/operations/execute_operation.ts +29 -10
  182. package/src/operations/find.ts +13 -14
  183. package/src/operations/get_more.ts +9 -1
  184. package/src/operations/indexes.ts +103 -176
  185. package/src/operations/insert.ts +2 -2
  186. package/src/operations/kill_cursors.ts +3 -2
  187. package/src/operations/list_collections.ts +5 -1
  188. package/src/operations/list_databases.ts +1 -1
  189. package/src/operations/operation.ts +3 -0
  190. package/src/operations/run_command.ts +6 -4
  191. package/src/operations/search_indexes/create.ts +4 -1
  192. package/src/operations/stats.ts +1 -1
  193. package/src/operations/update.ts +7 -7
  194. package/src/sdam/common.ts +8 -2
  195. package/src/sdam/monitor.ts +178 -61
  196. package/src/sdam/server.ts +27 -20
  197. package/src/sdam/server_description.ts +8 -3
  198. package/src/sdam/server_selection.ts +2 -3
  199. package/src/sdam/srv_polling.ts +3 -2
  200. package/src/sdam/topology.ts +114 -117
  201. package/src/sdam/topology_description.ts +14 -4
  202. package/src/sessions.ts +168 -148
  203. package/src/timeout.ts +96 -0
  204. package/src/utils.ts +85 -32
  205. package/lib/operations/common_functions.js +0 -38
  206. package/lib/operations/common_functions.js.map +0 -1
  207. package/src/operations/common_functions.ts +0 -79
package/src/utils.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import * as crypto from 'crypto';
2
2
  import type { SrvRecord } from 'dns';
3
3
  import { type EventEmitter } from 'events';
4
+ import { promises as fs } from 'fs';
4
5
  import * as http from 'http';
5
- import { clearTimeout, setTimeout } from 'timers';
6
6
  import * as url from 'url';
7
7
  import { URL } from 'url';
8
8
  import { promisify } from 'util';
@@ -64,6 +64,19 @@ export const ByteUtils = {
64
64
  }
65
65
  };
66
66
 
67
+ /**
68
+ * Returns true if value is a Uint8Array or a Buffer
69
+ * @param value - any value that may be a Uint8Array
70
+ */
71
+ export function isUint8Array(value: unknown): value is Uint8Array {
72
+ return (
73
+ value != null &&
74
+ typeof value === 'object' &&
75
+ Symbol.toStringTag in value &&
76
+ value[Symbol.toStringTag] === 'Uint8Array'
77
+ );
78
+ }
79
+
67
80
  /**
68
81
  * Determines if a connection's address matches a user provided list
69
82
  * of domain wildcards.
@@ -178,6 +191,7 @@ export function isPromiseLike<T = unknown>(value?: unknown): value is PromiseLik
178
191
  value != null &&
179
192
  typeof value === 'object' &&
180
193
  'then' in value &&
194
+ // eslint-disable-next-line github/no-then
181
195
  typeof value.then === 'function'
182
196
  );
183
197
  }
@@ -1156,7 +1170,7 @@ export async function request(
1156
1170
  uri: string,
1157
1171
  options: RequestOptions = {}
1158
1172
  ): Promise<string | Record<string, any>> {
1159
- return new Promise<string | Record<string, any>>((resolve, reject) => {
1173
+ return await new Promise<string | Record<string, any>>((resolve, reject) => {
1160
1174
  const requestOptions = {
1161
1175
  method: 'GET',
1162
1176
  timeout: 10000,
@@ -1201,33 +1215,6 @@ export async function request(
1201
1215
  });
1202
1216
  }
1203
1217
 
1204
- /**
1205
- * A custom AbortController that aborts after a specified timeout.
1206
- *
1207
- * If `timeout` is undefined or \<=0, the abort controller never aborts.
1208
- *
1209
- * This class provides two benefits over the built-in AbortSignal.timeout() method.
1210
- * - This class provides a mechanism for cancelling the timeout
1211
- * - This class supports infinite timeouts by interpreting a timeout of 0 as infinite. This is
1212
- * consistent with existing timeout options in the Node driver (serverSelectionTimeoutMS, for example).
1213
- * @internal
1214
- */
1215
- export class TimeoutController extends AbortController {
1216
- constructor(
1217
- timeout = 0,
1218
- private timeoutId = timeout > 0 ? setTimeout(() => this.abort(), timeout) : null
1219
- ) {
1220
- super();
1221
- }
1222
-
1223
- clear() {
1224
- if (this.timeoutId != null) {
1225
- clearTimeout(this.timeoutId);
1226
- }
1227
- this.timeoutId = null;
1228
- }
1229
- }
1230
-
1231
1218
  /** @internal */
1232
1219
  export const DOCUMENT_DB_CHECK = /(\.docdb\.amazonaws\.com$)|(\.docdb-elastic\.amazonaws\.com$)/;
1233
1220
  /** @internal */
@@ -1245,9 +1232,13 @@ export function isHostMatch(match: RegExp, host?: string): boolean {
1245
1232
  return host && match.test(host.toLowerCase()) ? true : false;
1246
1233
  }
1247
1234
 
1248
- export function promiseWithResolvers<T>() {
1249
- let resolve!: Parameters<ConstructorParameters<typeof Promise<T>>[0]>[0];
1250
- let reject!: Parameters<ConstructorParameters<typeof Promise<T>>[0]>[1];
1235
+ export function promiseWithResolvers<T>(): {
1236
+ promise: Promise<T>;
1237
+ resolve: (value: T) => void;
1238
+ reject: (error: Error) => void;
1239
+ } {
1240
+ let resolve!: (value: T) => void;
1241
+ let reject!: (error: Error) => void;
1251
1242
  const promise = new Promise<T>(function withResolversExecutor(promiseResolve, promiseReject) {
1252
1243
  resolve = promiseResolve;
1253
1244
  reject = promiseReject;
@@ -1255,6 +1246,20 @@ export function promiseWithResolvers<T>() {
1255
1246
  return { promise, resolve, reject } as const;
1256
1247
  }
1257
1248
 
1249
+ /**
1250
+ * A noop function intended for use in preventing unhandled rejections.
1251
+ *
1252
+ * @example
1253
+ * ```js
1254
+ * const promise = myAsyncTask();
1255
+ * // eslint-disable-next-line github/no-then
1256
+ * promise.then(undefined, squashError);
1257
+ * ```
1258
+ */
1259
+ export function squashError(_error: unknown) {
1260
+ return;
1261
+ }
1262
+
1258
1263
  export const randomBytes = promisify(crypto.randomBytes);
1259
1264
 
1260
1265
  /**
@@ -1280,3 +1285,51 @@ export async function once<T>(ee: EventEmitter, name: string): Promise<T> {
1280
1285
  throw error;
1281
1286
  }
1282
1287
  }
1288
+
1289
+ export function maybeAddIdToDocuments(
1290
+ coll: Collection,
1291
+ docs: Document[],
1292
+ options: { forceServerObjectId?: boolean }
1293
+ ): Document[];
1294
+ export function maybeAddIdToDocuments(
1295
+ coll: Collection,
1296
+ docs: Document,
1297
+ options: { forceServerObjectId?: boolean }
1298
+ ): Document;
1299
+ export function maybeAddIdToDocuments(
1300
+ coll: Collection,
1301
+ docOrDocs: Document[] | Document,
1302
+ options: { forceServerObjectId?: boolean }
1303
+ ): Document[] | Document {
1304
+ const forceServerObjectId =
1305
+ typeof options.forceServerObjectId === 'boolean'
1306
+ ? options.forceServerObjectId
1307
+ : coll.s.db.options?.forceServerObjectId;
1308
+
1309
+ // no need to modify the docs if server sets the ObjectId
1310
+ if (forceServerObjectId === true) {
1311
+ return docOrDocs;
1312
+ }
1313
+
1314
+ const transform = (doc: Document): Document => {
1315
+ if (doc._id == null) {
1316
+ doc._id = coll.s.pkFactory.createPk();
1317
+ }
1318
+
1319
+ return doc;
1320
+ };
1321
+ return Array.isArray(docOrDocs) ? docOrDocs.map(transform) : transform(docOrDocs);
1322
+ }
1323
+
1324
+ export async function fileIsAccessible(fileName: string, mode?: number) {
1325
+ try {
1326
+ await fs.access(fileName, mode);
1327
+ return true;
1328
+ } catch {
1329
+ return false;
1330
+ }
1331
+ }
1332
+
1333
+ export function noop() {
1334
+ return;
1335
+ }
@@ -1,38 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.maybeAddIdToDocuments = exports.indexInformation = void 0;
4
- async function indexInformation(db, name, options) {
5
- if (options == null) {
6
- options = {};
7
- }
8
- // If we specified full information
9
- const full = options.full == null ? false : options.full;
10
- // Get the list of indexes of the specified collection
11
- const indexes = await db.collection(name).listIndexes(options).toArray();
12
- if (full)
13
- return indexes;
14
- const info = {};
15
- for (const index of indexes) {
16
- info[index.name] = Object.entries(index.key);
17
- }
18
- return info;
19
- }
20
- exports.indexInformation = indexInformation;
21
- function maybeAddIdToDocuments(coll, docOrDocs, options) {
22
- const forceServerObjectId = typeof options.forceServerObjectId === 'boolean'
23
- ? options.forceServerObjectId
24
- : coll.s.db.options?.forceServerObjectId;
25
- // no need to modify the docs if server sets the ObjectId
26
- if (forceServerObjectId === true) {
27
- return docOrDocs;
28
- }
29
- const transform = (doc) => {
30
- if (doc._id == null) {
31
- doc._id = coll.s.pkFactory.createPk();
32
- }
33
- return doc;
34
- };
35
- return Array.isArray(docOrDocs) ? docOrDocs.map(transform) : transform(docOrDocs);
36
- }
37
- exports.maybeAddIdToDocuments = maybeAddIdToDocuments;
38
- //# sourceMappingURL=common_functions.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"common_functions.js","sourceRoot":"","sources":["../../src/operations/common_functions.ts"],"names":[],"mappings":";;;AAwBO,KAAK,UAAU,gBAAgB,CACpC,EAAM,EACN,IAAY,EACZ,OAAiC;IAEjC,IAAI,OAAO,IAAI,IAAI,EAAE;QACnB,OAAO,GAAG,EAAE,CAAC;KACd;IACD,mCAAmC;IACnC,MAAM,IAAI,GAAG,OAAO,CAAC,IAAI,IAAI,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC;IACzD,sDAAsD;IACtD,MAAM,OAAO,GAAG,MAAM,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,WAAW,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,CAAC;IACzE,IAAI,IAAI;QAAE,OAAO,OAAO,CAAC;IAEzB,MAAM,IAAI,GAA6C,EAAE,CAAC;IAC1D,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE;QAC3B,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;KAC9C;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAnBD,4CAmBC;AAYD,SAAgB,qBAAqB,CACnC,IAAgB,EAChB,SAAgC,EAChC,OAA0C;IAE1C,MAAM,mBAAmB,GACvB,OAAO,OAAO,CAAC,mBAAmB,KAAK,SAAS;QAC9C,CAAC,CAAC,OAAO,CAAC,mBAAmB;QAC7B,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,OAAO,EAAE,mBAAmB,CAAC;IAE7C,yDAAyD;IACzD,IAAI,mBAAmB,KAAK,IAAI,EAAE;QAChC,OAAO,SAAS,CAAC;KAClB;IAED,MAAM,SAAS,GAAG,CAAC,GAAa,EAAY,EAAE;QAC5C,IAAI,GAAG,CAAC,GAAG,IAAI,IAAI,EAAE;YACnB,GAAG,CAAC,GAAG,GAAG,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,QAAQ,EAAE,CAAC;SACvC;QAED,OAAO,GAAG,CAAC;IACb,CAAC,CAAC;IACF,OAAO,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC;AACpF,CAAC;AAvBD,sDAuBC"}
@@ -1,79 +0,0 @@
1
- import type { Document } from '../bson';
2
- import type { Collection } from '../collection';
3
- import type { Db } from '../db';
4
- import type { ReadPreference } from '../read_preference';
5
- import type { ClientSession } from '../sessions';
6
-
7
- /** @public */
8
- export interface IndexInformationOptions {
9
- full?: boolean;
10
- readPreference?: ReadPreference;
11
- session?: ClientSession;
12
- }
13
- /**
14
- * Retrieves this collections index info.
15
- *
16
- * @param db - The Db instance on which to retrieve the index info.
17
- * @param name - The name of the collection.
18
- */
19
- export async function indexInformation(db: Db, name: string): Promise<any>;
20
- export async function indexInformation(
21
- db: Db,
22
- name: string,
23
- options?: IndexInformationOptions
24
- ): Promise<any>;
25
- export async function indexInformation(
26
- db: Db,
27
- name: string,
28
- options?: IndexInformationOptions
29
- ): Promise<any> {
30
- if (options == null) {
31
- options = {};
32
- }
33
- // If we specified full information
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;
38
-
39
- const info: Record<string, Array<[string, unknown]>> = {};
40
- for (const index of indexes) {
41
- info[index.name] = Object.entries(index.key);
42
- }
43
- return info;
44
- }
45
-
46
- export function maybeAddIdToDocuments(
47
- coll: Collection,
48
- docs: Document[],
49
- options: { forceServerObjectId?: boolean }
50
- ): Document[];
51
- export function maybeAddIdToDocuments(
52
- coll: Collection,
53
- docs: Document,
54
- options: { forceServerObjectId?: boolean }
55
- ): Document;
56
- export function maybeAddIdToDocuments(
57
- coll: Collection,
58
- docOrDocs: Document[] | Document,
59
- options: { forceServerObjectId?: boolean }
60
- ): Document[] | Document {
61
- const forceServerObjectId =
62
- typeof options.forceServerObjectId === 'boolean'
63
- ? options.forceServerObjectId
64
- : coll.s.db.options?.forceServerObjectId;
65
-
66
- // no need to modify the docs if server sets the ObjectId
67
- if (forceServerObjectId === true) {
68
- return docOrDocs;
69
- }
70
-
71
- const transform = (doc: Document): Document => {
72
- if (doc._id == null) {
73
- doc._id = coll.s.pkFactory.createPk();
74
- }
75
-
76
- return doc;
77
- };
78
- return Array.isArray(docOrDocs) ? docOrDocs.map(transform) : transform(docOrDocs);
79
- }