123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.FindCursor = exports.FLAGS = void 0;
- const error_1 = require("../error");
- const count_1 = require("../operations/count");
- const execute_operation_1 = require("../operations/execute_operation");
- const find_1 = require("../operations/find");
- const sort_1 = require("../sort");
- const utils_1 = require("../utils");
- const abstract_cursor_1 = require("./abstract_cursor");
- const kFilter = Symbol('filter');
- const kNumReturned = Symbol('numReturned');
- const kBuiltOptions = Symbol('builtOptions');
- exports.FLAGS = [
- 'tailable',
- 'oplogReplay',
- 'noCursorTimeout',
- 'awaitData',
- 'exhaust',
- 'partial'
- ];
- class FindCursor extends abstract_cursor_1.AbstractCursor {
-
- constructor(client, namespace, filter, options = {}) {
- super(client, namespace, options);
- this[kFilter] = filter || {};
- this[kBuiltOptions] = options;
- if (options.sort != null) {
- this[kBuiltOptions].sort = (0, sort_1.formatSort)(options.sort);
- }
- }
- clone() {
- const clonedOptions = (0, utils_1.mergeOptions)({}, this[kBuiltOptions]);
- delete clonedOptions.session;
- return new FindCursor(this.client, this.namespace, this[kFilter], {
- ...clonedOptions
- });
- }
- map(transform) {
- return super.map(transform);
- }
-
- _initialize(session, callback) {
- const findOperation = new find_1.FindOperation(undefined, this.namespace, this[kFilter], {
- ...this[kBuiltOptions],
- ...this.cursorOptions,
- session
- });
- (0, execute_operation_1.executeOperation)(this.client, findOperation, (err, response) => {
- if (err || response == null)
- return callback(err);
-
-
- if (response.cursor) {
- this[kNumReturned] = response.cursor.firstBatch.length;
- }
- else {
- this[kNumReturned] = response.documents ? response.documents.length : 0;
- }
-
- callback(undefined, { server: findOperation.server, session, response });
- });
- }
-
- _getMore(batchSize, callback) {
-
- const numReturned = this[kNumReturned];
- if (numReturned) {
- const limit = this[kBuiltOptions].limit;
- batchSize =
- limit && limit > 0 && numReturned + batchSize > limit ? limit - numReturned : batchSize;
- if (batchSize <= 0) {
- return this.close(callback);
- }
- }
- super._getMore(batchSize, (err, response) => {
- if (err)
- return callback(err);
-
- if (response) {
- this[kNumReturned] = this[kNumReturned] + response.cursor.nextBatch.length;
- }
- callback(undefined, response);
- });
- }
- count(options, callback) {
- (0, utils_1.emitWarningOnce)('cursor.count is deprecated and will be removed in the next major version, please use `collection.estimatedDocumentCount` or `collection.countDocuments` instead ');
- if (typeof options === 'boolean') {
- throw new error_1.MongoInvalidArgumentError('Invalid first parameter to count');
- }
- if (typeof options === 'function')
- (callback = options), (options = {});
- options = options !== null && options !== void 0 ? options : {};
- return (0, execute_operation_1.executeOperation)(this.client, new count_1.CountOperation(this.namespace, this[kFilter], {
- ...this[kBuiltOptions],
- ...this.cursorOptions,
- ...options
- }), callback);
- }
- explain(verbosity, callback) {
- if (typeof verbosity === 'function')
- (callback = verbosity), (verbosity = true);
- if (verbosity == null)
- verbosity = true;
- return (0, execute_operation_1.executeOperation)(this.client, new find_1.FindOperation(undefined, this.namespace, this[kFilter], {
- ...this[kBuiltOptions],
- ...this.cursorOptions,
- explain: verbosity
- }), callback);
- }
-
- filter(filter) {
- (0, abstract_cursor_1.assertUninitialized)(this);
- this[kFilter] = filter;
- return this;
- }
-
- hint(hint) {
- (0, abstract_cursor_1.assertUninitialized)(this);
- this[kBuiltOptions].hint = hint;
- return this;
- }
-
- min(min) {
- (0, abstract_cursor_1.assertUninitialized)(this);
- this[kBuiltOptions].min = min;
- return this;
- }
-
- max(max) {
- (0, abstract_cursor_1.assertUninitialized)(this);
- this[kBuiltOptions].max = max;
- return this;
- }
-
- returnKey(value) {
- (0, abstract_cursor_1.assertUninitialized)(this);
- this[kBuiltOptions].returnKey = value;
- return this;
- }
-
- showRecordId(value) {
- (0, abstract_cursor_1.assertUninitialized)(this);
- this[kBuiltOptions].showRecordId = value;
- return this;
- }
-
- addQueryModifier(name, value) {
- (0, abstract_cursor_1.assertUninitialized)(this);
- if (name[0] !== '$') {
- throw new error_1.MongoInvalidArgumentError(`${name} is not a valid query modifier`);
- }
-
- const field = name.substr(1);
-
- switch (field) {
- case 'comment':
- this[kBuiltOptions].comment = value;
- break;
- case 'explain':
- this[kBuiltOptions].explain = value;
- break;
- case 'hint':
- this[kBuiltOptions].hint = value;
- break;
- case 'max':
- this[kBuiltOptions].max = value;
- break;
- case 'maxTimeMS':
- this[kBuiltOptions].maxTimeMS = value;
- break;
- case 'min':
- this[kBuiltOptions].min = value;
- break;
- case 'orderby':
- this[kBuiltOptions].sort = (0, sort_1.formatSort)(value);
- break;
- case 'query':
- this[kFilter] = value;
- break;
- case 'returnKey':
- this[kBuiltOptions].returnKey = value;
- break;
- case 'showDiskLoc':
- this[kBuiltOptions].showRecordId = value;
- break;
- default:
- throw new error_1.MongoInvalidArgumentError(`Invalid query modifier: ${name}`);
- }
- return this;
- }
-
- comment(value) {
- (0, abstract_cursor_1.assertUninitialized)(this);
- this[kBuiltOptions].comment = value;
- return this;
- }
-
- maxAwaitTimeMS(value) {
- (0, abstract_cursor_1.assertUninitialized)(this);
- if (typeof value !== 'number') {
- throw new error_1.MongoInvalidArgumentError('Argument for maxAwaitTimeMS must be a number');
- }
- this[kBuiltOptions].maxAwaitTimeMS = value;
- return this;
- }
-
- maxTimeMS(value) {
- (0, abstract_cursor_1.assertUninitialized)(this);
- if (typeof value !== 'number') {
- throw new error_1.MongoInvalidArgumentError('Argument for maxTimeMS must be a number');
- }
- this[kBuiltOptions].maxTimeMS = value;
- return this;
- }
-
- project(value) {
- (0, abstract_cursor_1.assertUninitialized)(this);
- this[kBuiltOptions].projection = value;
- return this;
- }
-
- sort(sort, direction) {
- (0, abstract_cursor_1.assertUninitialized)(this);
- if (this[kBuiltOptions].tailable) {
- throw new error_1.MongoTailableCursorError('Tailable cursor does not support sorting');
- }
- this[kBuiltOptions].sort = (0, sort_1.formatSort)(sort, direction);
- return this;
- }
-
- allowDiskUse(allow = true) {
- (0, abstract_cursor_1.assertUninitialized)(this);
- if (!this[kBuiltOptions].sort) {
- throw new error_1.MongoInvalidArgumentError('Option "allowDiskUse" requires a sort specification');
- }
-
- if (!allow) {
- this[kBuiltOptions].allowDiskUse = false;
- return this;
- }
- this[kBuiltOptions].allowDiskUse = true;
- return this;
- }
-
- collation(value) {
- (0, abstract_cursor_1.assertUninitialized)(this);
- this[kBuiltOptions].collation = value;
- return this;
- }
-
- limit(value) {
- (0, abstract_cursor_1.assertUninitialized)(this);
- if (this[kBuiltOptions].tailable) {
- throw new error_1.MongoTailableCursorError('Tailable cursor does not support limit');
- }
- if (typeof value !== 'number') {
- throw new error_1.MongoInvalidArgumentError('Operation "limit" requires an integer');
- }
- this[kBuiltOptions].limit = value;
- return this;
- }
-
- skip(value) {
- (0, abstract_cursor_1.assertUninitialized)(this);
- if (this[kBuiltOptions].tailable) {
- throw new error_1.MongoTailableCursorError('Tailable cursor does not support skip');
- }
- if (typeof value !== 'number') {
- throw new error_1.MongoInvalidArgumentError('Operation "skip" requires an integer');
- }
- this[kBuiltOptions].skip = value;
- return this;
- }
- }
- exports.FindCursor = FindCursor;
|