mongoose 9.11.4 → 9.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cursor/aggregationCursor.js +9 -5
- package/lib/cursor/queryCursor.js +8 -5
- package/lib/document.js +186 -287
- package/lib/helpers/cursor/waitForInitialization.js +17 -0
- package/lib/helpers/document/applyDefaults.js +6 -11
- package/lib/helpers/model/discriminator.js +3 -0
- package/lib/helpers/pathTrie.js +2 -2
- package/lib/helpers/populate/setInheritedReadOptions.js +21 -0
- package/lib/helpers/query/cloneMongooseOptions.js +44 -0
- package/lib/helpers/schema/cleanPositionalOperators.js +5 -3
- package/lib/helpers/update/applyTimestampsToChildren.js +1 -1
- package/lib/helpers/update/applyTimestampsToUpdate.js +1 -1
- package/lib/helpers/updateValidators.js +24 -16
- package/lib/model.js +24 -6
- package/lib/query.js +5 -52
- package/lib/queryHelpers.js +8 -1
- package/lib/schema/documentArray.js +14 -3
- package/lib/schema/number.js +3 -1
- package/lib/schema/string.js +3 -1
- package/lib/schema/subdocument.js +9 -1
- package/lib/schema.js +114 -4
- package/lib/stateMachine.js +5 -2
- package/lib/types/array/methods/index.js +22 -4
- package/lib/types/arraySubdocument.js +11 -9
- package/lib/types/map.js +17 -1
- package/lib/types/subdocument.js +5 -1
- package/package.json +15 -15
- package/types/types.d.ts +12 -0
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
const MongooseError = require('../error/mongooseError');
|
|
8
8
|
const Readable = require('stream').Readable;
|
|
9
9
|
const eachAsync = require('../helpers/cursor/eachAsync');
|
|
10
|
+
const waitForInitialization = require('../helpers/cursor/waitForInitialization');
|
|
10
11
|
const immediate = require('../helpers/immediate');
|
|
11
12
|
const kareem = require('kareem');
|
|
12
13
|
const util = require('util');
|
|
@@ -42,6 +43,7 @@ function AggregationCursor(agg) {
|
|
|
42
43
|
Readable.call(this, { autoDestroy: true, objectMode: true });
|
|
43
44
|
|
|
44
45
|
this.cursor = null;
|
|
46
|
+
this._initializationComplete = false;
|
|
45
47
|
this.agg = agg;
|
|
46
48
|
this._transforms = [];
|
|
47
49
|
const connection = agg._connection;
|
|
@@ -58,6 +60,7 @@ function AggregationCursor(agg) {
|
|
|
58
60
|
options = driverOptions;
|
|
59
61
|
}
|
|
60
62
|
this.cursor = connection.db.aggregate(agg._pipeline, options);
|
|
63
|
+
this._initializationComplete = true;
|
|
61
64
|
setImmediate(() => this.emit('cursor', this.cursor));
|
|
62
65
|
} else {
|
|
63
66
|
_init(model, this, agg);
|
|
@@ -108,8 +111,10 @@ function _getRawCursor(model, aggregationCursor, agg) {
|
|
|
108
111
|
}
|
|
109
112
|
const cursor = model.collection.aggregate(agg._pipeline, options);
|
|
110
113
|
aggregationCursor.cursor = cursor;
|
|
114
|
+
aggregationCursor._initializationComplete = true;
|
|
111
115
|
aggregationCursor.emit('cursor', cursor);
|
|
112
116
|
} catch (err) {
|
|
117
|
+
aggregationCursor._initializationComplete = true;
|
|
113
118
|
aggregationCursor._markError(err);
|
|
114
119
|
aggregationCursor.listeners('error').length > 0 && aggregationCursor.emit('error', aggregationCursor._error);
|
|
115
120
|
}
|
|
@@ -126,6 +131,7 @@ function _getRawCursor(model, aggregationCursor, agg) {
|
|
|
126
131
|
*/
|
|
127
132
|
|
|
128
133
|
function _handlePreHookError(queryCursor, err) {
|
|
134
|
+
queryCursor._initializationComplete = true;
|
|
129
135
|
if (err instanceof kareem.skipWrappedFunction) {
|
|
130
136
|
const resultValue = err.args[0];
|
|
131
137
|
if (resultValue != null && (!Array.isArray(resultValue) || resultValue.length)) {
|
|
@@ -275,16 +281,14 @@ AggregationCursor.prototype.close = async function close() {
|
|
|
275
281
|
|
|
276
282
|
AggregationCursor.prototype._destroy = function _destroy(_err, callback) {
|
|
277
283
|
let waitForCursor = null;
|
|
278
|
-
if (!this.
|
|
279
|
-
waitForCursor =
|
|
280
|
-
this.once('cursor', resolve);
|
|
281
|
-
});
|
|
284
|
+
if (!this._initializationComplete) {
|
|
285
|
+
waitForCursor = waitForInitialization(this);
|
|
282
286
|
} else {
|
|
283
287
|
waitForCursor = Promise.resolve();
|
|
284
288
|
}
|
|
285
289
|
|
|
286
290
|
waitForCursor
|
|
287
|
-
.then(() => this.cursor
|
|
291
|
+
.then(() => this.cursor?.close())
|
|
288
292
|
.then(() => {
|
|
289
293
|
this._closed = true;
|
|
290
294
|
callback();
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
const MongooseError = require('../error/mongooseError');
|
|
8
8
|
const Readable = require('stream').Readable;
|
|
9
9
|
const eachAsync = require('../helpers/cursor/eachAsync');
|
|
10
|
+
const waitForInitialization = require('../helpers/cursor/waitForInitialization');
|
|
10
11
|
const helpers = require('../queryHelpers');
|
|
11
12
|
const kareem = require('kareem');
|
|
12
13
|
const immediate = require('../helpers/immediate');
|
|
@@ -44,6 +45,7 @@ function QueryCursor(query) {
|
|
|
44
45
|
Readable.call(this, { autoDestroy: true, objectMode: true });
|
|
45
46
|
|
|
46
47
|
this.cursor = null;
|
|
48
|
+
this._initializationComplete = false;
|
|
47
49
|
this.skipped = false;
|
|
48
50
|
this.query = query;
|
|
49
51
|
this._closed = false;
|
|
@@ -56,6 +58,7 @@ function QueryCursor(query) {
|
|
|
56
58
|
|
|
57
59
|
const onPreComplete = (err) => {
|
|
58
60
|
if (err != null) {
|
|
61
|
+
this._initializationComplete = true;
|
|
59
62
|
if (err instanceof kareem.skipWrappedFunction) {
|
|
60
63
|
const resultValue = err.args[0];
|
|
61
64
|
if (resultValue != null && (!Array.isArray(resultValue) || resultValue.length)) {
|
|
@@ -123,8 +126,10 @@ function _getRawCursor(query, queryCursor) {
|
|
|
123
126
|
try {
|
|
124
127
|
const cursor = query.model.collection.find(query._conditions, queryCursor.options);
|
|
125
128
|
queryCursor.cursor = cursor;
|
|
129
|
+
queryCursor._initializationComplete = true;
|
|
126
130
|
queryCursor.emit('cursor', cursor);
|
|
127
131
|
} catch (err) {
|
|
132
|
+
queryCursor._initializationComplete = true;
|
|
128
133
|
queryCursor._markError(err);
|
|
129
134
|
queryCursor.listeners('error').length > 0 && queryCursor.emit('error', queryCursor._error);
|
|
130
135
|
}
|
|
@@ -271,17 +276,15 @@ QueryCursor.prototype.close = async function close() {
|
|
|
271
276
|
|
|
272
277
|
QueryCursor.prototype._destroy = function _destroy(_err, callback) {
|
|
273
278
|
let waitForCursor = null;
|
|
274
|
-
if (!this.
|
|
275
|
-
waitForCursor =
|
|
276
|
-
this.once('cursor', resolve);
|
|
277
|
-
});
|
|
279
|
+
if (!this._initializationComplete) {
|
|
280
|
+
waitForCursor = waitForInitialization(this);
|
|
278
281
|
} else {
|
|
279
282
|
waitForCursor = Promise.resolve();
|
|
280
283
|
}
|
|
281
284
|
|
|
282
285
|
waitForCursor
|
|
283
286
|
.then(() => {
|
|
284
|
-
return this.cursor
|
|
287
|
+
return this.cursor?.close();
|
|
285
288
|
})
|
|
286
289
|
.then(() => {
|
|
287
290
|
this._closed = true;
|