find.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.FindOperation = void 0;
  4. const responses_1 = require("../cmap/wire_protocol/responses");
  5. const error_1 = require("../error");
  6. const sort_1 = require("../sort");
  7. const utils_1 = require("../utils");
  8. const command_1 = require("./command");
  9. const operation_1 = require("./operation");
  10. /** @internal */
  11. class FindOperation extends command_1.CommandOperation {
  12. constructor(ns, filter = {}, options = {}) {
  13. super(undefined, options);
  14. this.SERVER_COMMAND_RESPONSE_TYPE = responses_1.CursorResponse;
  15. this.options = { ...options };
  16. delete this.options.writeConcern;
  17. this.ns = ns;
  18. if (typeof filter !== 'object' || Array.isArray(filter)) {
  19. throw new error_1.MongoInvalidArgumentError('Query filter must be a plain object or ObjectId');
  20. }
  21. // special case passing in an ObjectId as a filter
  22. this.filter = filter != null && filter._bsontype === 'ObjectId' ? { _id: filter } : filter;
  23. this.SERVER_COMMAND_RESPONSE_TYPE = this.explain ? responses_1.ExplainedCursorResponse : responses_1.CursorResponse;
  24. }
  25. get commandName() {
  26. return 'find';
  27. }
  28. buildOptions(timeoutContext) {
  29. return {
  30. ...this.options,
  31. ...this.bsonOptions,
  32. documentsReturnedIn: 'firstBatch',
  33. session: this.session,
  34. timeoutContext
  35. };
  36. }
  37. handleOk(response) {
  38. return response;
  39. }
  40. buildCommandDocument() {
  41. return makeFindCommand(this.ns, this.filter, this.options);
  42. }
  43. }
  44. exports.FindOperation = FindOperation;
  45. function makeFindCommand(ns, filter, options) {
  46. const findCommand = {
  47. find: ns.collection,
  48. filter
  49. };
  50. if (options.sort) {
  51. findCommand.sort = (0, sort_1.formatSort)(options.sort);
  52. }
  53. if (options.projection) {
  54. let projection = options.projection;
  55. if (projection && Array.isArray(projection)) {
  56. projection = projection.length
  57. ? projection.reduce((result, field) => {
  58. result[field] = 1;
  59. return result;
  60. }, {})
  61. : { _id: 1 };
  62. }
  63. findCommand.projection = projection;
  64. }
  65. if (options.hint) {
  66. findCommand.hint = (0, utils_1.normalizeHintField)(options.hint);
  67. }
  68. if (typeof options.skip === 'number') {
  69. findCommand.skip = options.skip;
  70. }
  71. if (typeof options.limit === 'number') {
  72. if (options.limit < 0) {
  73. findCommand.limit = -options.limit;
  74. findCommand.singleBatch = true;
  75. }
  76. else {
  77. findCommand.limit = options.limit;
  78. }
  79. }
  80. if (typeof options.batchSize === 'number') {
  81. if (options.batchSize < 0) {
  82. findCommand.limit = -options.batchSize;
  83. }
  84. else {
  85. if (options.batchSize === options.limit) {
  86. // Spec dictates that if these are equal the batchSize should be one more than the
  87. // limit to avoid leaving the cursor open.
  88. findCommand.batchSize = options.batchSize + 1;
  89. }
  90. else {
  91. findCommand.batchSize = options.batchSize;
  92. }
  93. }
  94. }
  95. if (typeof options.singleBatch === 'boolean') {
  96. findCommand.singleBatch = options.singleBatch;
  97. }
  98. // we check for undefined specifically here to allow falsy values
  99. // eslint-disable-next-line no-restricted-syntax
  100. if (options.comment !== undefined) {
  101. findCommand.comment = options.comment;
  102. }
  103. if (options.max) {
  104. findCommand.max = options.max;
  105. }
  106. if (options.min) {
  107. findCommand.min = options.min;
  108. }
  109. if (typeof options.returnKey === 'boolean') {
  110. findCommand.returnKey = options.returnKey;
  111. }
  112. if (typeof options.showRecordId === 'boolean') {
  113. findCommand.showRecordId = options.showRecordId;
  114. }
  115. if (typeof options.tailable === 'boolean') {
  116. findCommand.tailable = options.tailable;
  117. }
  118. if (typeof options.oplogReplay === 'boolean') {
  119. findCommand.oplogReplay = options.oplogReplay;
  120. }
  121. if (typeof options.timeout === 'boolean') {
  122. findCommand.noCursorTimeout = !options.timeout;
  123. }
  124. else if (typeof options.noCursorTimeout === 'boolean') {
  125. findCommand.noCursorTimeout = options.noCursorTimeout;
  126. }
  127. if (typeof options.awaitData === 'boolean') {
  128. findCommand.awaitData = options.awaitData;
  129. }
  130. if (typeof options.allowPartialResults === 'boolean') {
  131. findCommand.allowPartialResults = options.allowPartialResults;
  132. }
  133. if (typeof options.allowDiskUse === 'boolean') {
  134. findCommand.allowDiskUse = options.allowDiskUse;
  135. }
  136. if (options.let) {
  137. findCommand.let = options.let;
  138. }
  139. return findCommand;
  140. }
  141. (0, operation_1.defineAspects)(FindOperation, [
  142. operation_1.Aspect.READ_OPERATION,
  143. operation_1.Aspect.RETRYABLE,
  144. operation_1.Aspect.EXPLAINABLE,
  145. operation_1.Aspect.CURSOR_CREATING,
  146. operation_1.Aspect.SUPPORTS_RAW_DATA
  147. ]);
  148. //# sourceMappingURL=find.js.map