common_functions.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. // Did the user destroy the topology
  16. if ((0, utils_1.getTopology)(db).isDestroyed())
  17. return callback(new error_1.MongoTopologyClosedError());
  18. // Process all the results from the index command and collection
  19. function processResults(indexes) {
  20. // Contains all the information
  21. const info = {};
  22. // Process all the indexes
  23. for (let i = 0; i < indexes.length; i++) {
  24. const index = indexes[i];
  25. // Let's unpack the object
  26. info[index.name] = [];
  27. for (const name in index.key) {
  28. info[index.name].push([name, index.key[name]]);
  29. }
  30. }
  31. return info;
  32. }
  33. // Get the list of indexes of the specified collection
  34. db.collection(name)
  35. .listIndexes(options)
  36. .toArray((err, indexes) => {
  37. if (err)
  38. return callback(err);
  39. if (!Array.isArray(indexes))
  40. return callback(undefined, []);
  41. if (full)
  42. return callback(undefined, indexes);
  43. callback(undefined, processResults(indexes));
  44. });
  45. }
  46. exports.indexInformation = indexInformation;
  47. function prepareDocs(coll, docs, options) {
  48. var _a;
  49. const forceServerObjectId = typeof options.forceServerObjectId === 'boolean'
  50. ? options.forceServerObjectId
  51. : (_a = coll.s.db.options) === null || _a === void 0 ? void 0 : _a.forceServerObjectId;
  52. // no need to modify the docs if server sets the ObjectId
  53. if (forceServerObjectId === true) {
  54. return docs;
  55. }
  56. return docs.map(doc => {
  57. if (doc._id == null) {
  58. doc._id = coll.s.pkFactory.createPk();
  59. }
  60. return doc;
  61. });
  62. }
  63. exports.prepareDocs = prepareDocs;
  64. //# sourceMappingURL=common_functions.js.map