common_functions.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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()
  44. .then(indexes => {
  45. if (!Array.isArray(indexes))
  46. return callback(undefined, []);
  47. if (full)
  48. return callback(undefined, indexes);
  49. callback(undefined, processResults(indexes));
  50. }, error => callback(error));
  51. }
  52. exports.indexInformation = indexInformation;
  53. function prepareDocs(coll, docs, options) {
  54. const forceServerObjectId = typeof options.forceServerObjectId === 'boolean'
  55. ? options.forceServerObjectId
  56. : coll.s.db.options?.forceServerObjectId;
  57. // no need to modify the docs if server sets the ObjectId
  58. if (forceServerObjectId === true) {
  59. return docs;
  60. }
  61. return docs.map(doc => {
  62. if (doc._id == null) {
  63. doc._id = coll.s.pkFactory.createPk();
  64. }
  65. return doc;
  66. });
  67. }
  68. exports.prepareDocs = prepareDocs;
  69. //# sourceMappingURL=common_functions.js.map