list_collections.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ListCollectionsCursor = exports.ListCollectionsOperation = void 0;
  4. const abstract_cursor_1 = require("../cursor/abstract_cursor");
  5. const utils_1 = require("../utils");
  6. const command_1 = require("./command");
  7. const execute_operation_1 = require("./execute_operation");
  8. const operation_1 = require("./operation");
  9. /** @internal */
  10. class ListCollectionsOperation extends command_1.CommandOperation {
  11. constructor(db, filter, options) {
  12. super(db, options);
  13. this.options = options !== null && options !== void 0 ? options : {};
  14. this.db = db;
  15. this.filter = filter;
  16. this.nameOnly = !!this.options.nameOnly;
  17. this.authorizedCollections = !!this.options.authorizedCollections;
  18. if (typeof this.options.batchSize === 'number') {
  19. this.batchSize = this.options.batchSize;
  20. }
  21. }
  22. execute(server, session, callback) {
  23. return super.executeCommand(server, session, this.generateCommand((0, utils_1.maxWireVersion)(server)), callback);
  24. }
  25. /* This is here for the purpose of unit testing the final command that gets sent. */
  26. generateCommand(wireVersion) {
  27. const command = {
  28. listCollections: 1,
  29. filter: this.filter,
  30. cursor: this.batchSize ? { batchSize: this.batchSize } : {},
  31. nameOnly: this.nameOnly,
  32. authorizedCollections: this.authorizedCollections
  33. };
  34. // we check for undefined specifically here to allow falsy values
  35. // eslint-disable-next-line no-restricted-syntax
  36. if (wireVersion >= 9 && this.options.comment !== undefined) {
  37. command.comment = this.options.comment;
  38. }
  39. return command;
  40. }
  41. }
  42. exports.ListCollectionsOperation = ListCollectionsOperation;
  43. /** @public */
  44. class ListCollectionsCursor extends abstract_cursor_1.AbstractCursor {
  45. constructor(db, filter, options) {
  46. super(db.s.client, db.s.namespace, options);
  47. this.parent = db;
  48. this.filter = filter;
  49. this.options = options;
  50. }
  51. clone() {
  52. return new ListCollectionsCursor(this.parent, this.filter, {
  53. ...this.options,
  54. ...this.cursorOptions
  55. });
  56. }
  57. /** @internal */
  58. _initialize(session, callback) {
  59. const operation = new ListCollectionsOperation(this.parent, this.filter, {
  60. ...this.cursorOptions,
  61. ...this.options,
  62. session
  63. });
  64. (0, execute_operation_1.executeOperation)(this.parent.s.client, operation, (err, response) => {
  65. if (err || response == null)
  66. return callback(err);
  67. // TODO: NODE-2882
  68. callback(undefined, { server: operation.server, session, response });
  69. });
  70. }
  71. }
  72. exports.ListCollectionsCursor = ListCollectionsCursor;
  73. (0, operation_1.defineAspects)(ListCollectionsOperation, [
  74. operation_1.Aspect.READ_OPERATION,
  75. operation_1.Aspect.RETRYABLE,
  76. operation_1.Aspect.CURSOR_CREATING
  77. ]);
  78. //# sourceMappingURL=list_collections.js.map