common_functions.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.prepareDocs = exports.indexInformation = void 0;
  4. const error_1 = require("../error");
  5. const utils_1 = require("../utils");
  6. function indexInformation(db, name, _optionsOrCallback, _callback) {
  7. let options = _optionsOrCallback;
  8. let callback = _callback;
  9. if ('function' === typeof _optionsOrCallback) {
  10. callback = _optionsOrCallback;
  11. options = {};
  12. }
  13. // If we specified full information
  14. const full = options.full == null ? false : options.full;
  15. let topology;
  16. try {
  17. topology = (0, utils_1.getTopology)(db);
  18. }
  19. catch (error) {
  20. return callback(error);
  21. }
  22. // Did the user destroy the topology
  23. if (topology.isDestroyed())
  24. return callback(new error_1.MongoTopologyClosedError());
  25. // Process all the results from the index command and collection
  26. function processResults(indexes) {
  27. // Contains all the information
  28. const info = {};
  29. // Process all the indexes
  30. for (let i = 0; i < indexes.length; i++) {
  31. const index = indexes[i];
  32. // Let's unpack the object
  33. info[index.name] = [];
  34. for (const name in index.key) {
  35. info[index.name].push([name, index.key[name]]);
  36. }
  37. }
  38. return info;
  39. }
  40. // Get the list of indexes of the specified collection
  41. db.collection(name)
  42. .listIndexes(options)
  43. .toArray((err, indexes) => {
  44. if (err)
  45. return callback(err);
  46. if (!Array.isArray(indexes))
  47. return callback(undefined, []);
  48. if (full)
  49. return callback(undefined, indexes);
  50. callback(undefined, processResults(indexes));
  51. });
  52. }
  53. exports.indexInformation = indexInformation;
  54. function prepareDocs(coll, docs, options) {
  55. var _a;
  56. const forceServerObjectId = typeof options.forceServerObjectId === 'boolean'
  57. ? options.forceServerObjectId
  58. : (_a = coll.s.db.options) === null || _a === void 0 ? void 0 : _a.forceServerObjectId;
  59. // no need to modify the docs if server sets the ObjectId
  60. if (forceServerObjectId === true) {
  61. return docs;
  62. }
  63. return docs.map(doc => {
  64. if (doc._id == null) {
  65. doc._id = coll.s.pkFactory.createPk();
  66. }
  67. return doc;
  68. });
  69. }
  70. exports.prepareDocs = prepareDocs;
  71. //# sourceMappingURL=common_functions.js.map