get_more.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.GetMoreOperation = void 0;
  4. const error_1 = require("../error");
  5. const utils_1 = require("../utils");
  6. const operation_1 = require("./operation");
  7. /** @internal */
  8. class GetMoreOperation extends operation_1.AbstractOperation {
  9. constructor(ns, cursorId, server, options) {
  10. super(options);
  11. this.options = options;
  12. this.ns = ns;
  13. this.cursorId = cursorId;
  14. this.server = server;
  15. }
  16. /**
  17. * Although there is a server already associated with the get more operation, the signature
  18. * for execute passes a server so we will just use that one.
  19. */
  20. async execute(server, _session) {
  21. if (server !== this.server) {
  22. throw new error_1.MongoRuntimeError('Getmore must run on the same server operation began on');
  23. }
  24. if (this.cursorId == null || this.cursorId.isZero()) {
  25. throw new error_1.MongoRuntimeError('Unable to iterate cursor with no id');
  26. }
  27. const collection = this.ns.collection;
  28. if (collection == null) {
  29. // Cursors should have adopted the namespace returned by MongoDB
  30. // which should always defined a collection name (even a pseudo one, ex. db.aggregate())
  31. throw new error_1.MongoRuntimeError('A collection name must be determined before getMore');
  32. }
  33. const getMoreCmd = {
  34. getMore: this.cursorId,
  35. collection
  36. };
  37. if (typeof this.options.batchSize === 'number') {
  38. getMoreCmd.batchSize = Math.abs(this.options.batchSize);
  39. }
  40. if (typeof this.options.maxAwaitTimeMS === 'number') {
  41. getMoreCmd.maxTimeMS = this.options.maxAwaitTimeMS;
  42. }
  43. // we check for undefined specifically here to allow falsy values
  44. // eslint-disable-next-line no-restricted-syntax
  45. if (this.options.comment !== undefined && (0, utils_1.maxWireVersion)(server) >= 9) {
  46. getMoreCmd.comment = this.options.comment;
  47. }
  48. const commandOptions = {
  49. returnFieldSelector: null,
  50. documentsReturnedIn: 'nextBatch',
  51. ...this.options
  52. };
  53. return server.commandAsync(this.ns, getMoreCmd, commandOptions);
  54. }
  55. }
  56. exports.GetMoreOperation = GetMoreOperation;
  57. (0, operation_1.defineAspects)(GetMoreOperation, [operation_1.Aspect.READ_OPERATION, operation_1.Aspect.MUST_SELECT_SAME_SERVER]);
  58. //# sourceMappingURL=get_more.js.map