list_collections.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ListCollectionsCursor = exports.ListCollectionsOperation = void 0;
  4. const CONSTANTS = require("../constants");
  5. const abstract_cursor_1 = require("../cursor/abstract_cursor");
  6. const utils_1 = require("../utils");
  7. const command_1 = require("./command");
  8. const execute_operation_1 = require("./execute_operation");
  9. const operation_1 = require("./operation");
  10. const LIST_COLLECTIONS_WIRE_VERSION = 3;
  11. /** @internal */
  12. class ListCollectionsOperation extends command_1.CommandOperation {
  13. constructor(db, filter, options) {
  14. super(db, options);
  15. this.options = options !== null && options !== void 0 ? options : {};
  16. this.db = db;
  17. this.filter = filter;
  18. this.nameOnly = !!this.options.nameOnly;
  19. this.authorizedCollections = !!this.options.authorizedCollections;
  20. if (typeof this.options.batchSize === 'number') {
  21. this.batchSize = this.options.batchSize;
  22. }
  23. }
  24. execute(server, session, callback) {
  25. if ((0, utils_1.maxWireVersion)(server) < LIST_COLLECTIONS_WIRE_VERSION) {
  26. let filter = this.filter;
  27. const databaseName = this.db.s.namespace.db;
  28. // If we have legacy mode and have not provided a full db name filter it
  29. if (typeof filter.name === 'string' && !new RegExp(`^${databaseName}\\.`).test(filter.name)) {
  30. filter = Object.assign({}, filter);
  31. filter.name = this.db.s.namespace.withCollection(filter.name).toString();
  32. }
  33. // No filter, filter by current database
  34. if (filter == null) {
  35. filter = { name: `/${databaseName}/` };
  36. }
  37. // Rewrite the filter to use $and to filter out indexes
  38. if (filter.name) {
  39. filter = { $and: [{ name: filter.name }, { name: /^((?!\$).)*$/ }] };
  40. }
  41. else {
  42. filter = { name: /^((?!\$).)*$/ };
  43. }
  44. const documentTransform = (doc) => {
  45. const matching = `${databaseName}.`;
  46. const index = doc.name.indexOf(matching);
  47. // Remove database name if available
  48. if (doc.name && index === 0) {
  49. doc.name = doc.name.substr(index + matching.length);
  50. }
  51. return doc;
  52. };
  53. server.query(new utils_1.MongoDBNamespace(databaseName, CONSTANTS.SYSTEM_NAMESPACE_COLLECTION), { query: filter }, { batchSize: this.batchSize || 1000, readPreference: this.readPreference }, (err, result) => {
  54. if (result && result.documents && Array.isArray(result.documents)) {
  55. result.documents = result.documents.map(documentTransform);
  56. }
  57. callback(err, result);
  58. });
  59. return;
  60. }
  61. return super.executeCommand(server, session, this.generateCommand(), callback);
  62. }
  63. /* This is here for the purpose of unit testing the final command that gets sent. */
  64. generateCommand() {
  65. return {
  66. listCollections: 1,
  67. filter: this.filter,
  68. cursor: this.batchSize ? { batchSize: this.batchSize } : {},
  69. nameOnly: this.nameOnly,
  70. authorizedCollections: this.authorizedCollections
  71. };
  72. }
  73. }
  74. exports.ListCollectionsOperation = ListCollectionsOperation;
  75. /** @public */
  76. class ListCollectionsCursor extends abstract_cursor_1.AbstractCursor {
  77. constructor(db, filter, options) {
  78. super((0, utils_1.getTopology)(db), db.s.namespace, options);
  79. this.parent = db;
  80. this.filter = filter;
  81. this.options = options;
  82. }
  83. clone() {
  84. return new ListCollectionsCursor(this.parent, this.filter, {
  85. ...this.options,
  86. ...this.cursorOptions
  87. });
  88. }
  89. /** @internal */
  90. _initialize(session, callback) {
  91. const operation = new ListCollectionsOperation(this.parent, this.filter, {
  92. ...this.cursorOptions,
  93. ...this.options,
  94. session
  95. });
  96. (0, execute_operation_1.executeOperation)((0, utils_1.getTopology)(this.parent), operation, (err, response) => {
  97. if (err || response == null)
  98. return callback(err);
  99. // TODO: NODE-2882
  100. callback(undefined, { server: operation.server, session, response });
  101. });
  102. }
  103. }
  104. exports.ListCollectionsCursor = ListCollectionsCursor;
  105. (0, operation_1.defineAspects)(ListCollectionsOperation, [
  106. operation_1.Aspect.READ_OPERATION,
  107. operation_1.Aspect.RETRYABLE,
  108. operation_1.Aspect.CURSOR_CREATING
  109. ]);
  110. //# sourceMappingURL=list_collections.js.map