mongoose 8.1.2 → 8.2.0
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/dist/browser.umd.js +1 -1
- package/lib/aggregate.js +2 -2
- package/lib/connection.js +22 -0
- package/lib/constants.js +36 -0
- package/lib/cursor/aggregationCursor.js +8 -0
- package/lib/document.js +17 -11
- package/lib/helpers/model/applyStaticHooks.js +1 -1
- package/lib/helpers/populate/getModelsMapForPopulate.js +2 -1
- package/lib/helpers/query/applyGlobalOption.js +9 -9
- package/lib/helpers/query/castFilterPath.js +1 -2
- package/lib/helpers/query/validOps.js +1 -18
- package/lib/model.js +192 -62
- package/lib/options.js +2 -1
- package/lib/plugins/trackTransaction.js +1 -1
- package/lib/query.js +44 -15
- package/lib/schema.js +1 -2
- package/package.json +1 -1
- package/types/connection.d.ts +2 -0
- package/types/index.d.ts +40 -2
- package/types/models.d.ts +9 -1
- package/lib/helpers/query/applyQueryMiddleware.js +0 -54
- package/lib/helpers/query/completeMany.js +0 -36
package/lib/query.js
CHANGED
|
@@ -19,7 +19,6 @@ const castArrayFilters = require('./helpers/update/castArrayFilters');
|
|
|
19
19
|
const castNumber = require('./cast/number');
|
|
20
20
|
const castUpdate = require('./helpers/query/castUpdate');
|
|
21
21
|
const clone = require('./helpers/clone');
|
|
22
|
-
const completeMany = require('./helpers/query/completeMany');
|
|
23
22
|
const getDiscriminatorByValue = require('./helpers/discriminator/getDiscriminatorByValue');
|
|
24
23
|
const helpers = require('./queryHelpers');
|
|
25
24
|
const immediate = require('./helpers/immediate');
|
|
@@ -40,7 +39,7 @@ const specialProperties = require('./helpers/specialProperties');
|
|
|
40
39
|
const updateValidators = require('./helpers/updateValidators');
|
|
41
40
|
const util = require('util');
|
|
42
41
|
const utils = require('./utils');
|
|
43
|
-
const
|
|
42
|
+
const queryMiddlewareFunctions = require('./constants').queryMiddlewareFunctions;
|
|
44
43
|
|
|
45
44
|
const queryOptionMethods = new Set([
|
|
46
45
|
'allowDiskUse',
|
|
@@ -457,7 +456,7 @@ Query.prototype.slice = function() {
|
|
|
457
456
|
* ignore
|
|
458
457
|
*/
|
|
459
458
|
|
|
460
|
-
const validOpsSet = new Set(
|
|
459
|
+
const validOpsSet = new Set(queryMiddlewareFunctions);
|
|
461
460
|
|
|
462
461
|
Query.prototype._validateOp = function() {
|
|
463
462
|
if (this.op != null && !validOpsSet.has(this.op)) {
|
|
@@ -2233,8 +2232,8 @@ Query.prototype._find = async function _find() {
|
|
|
2233
2232
|
const _this = this;
|
|
2234
2233
|
const userProvidedFields = _this._userProvidedFields || {};
|
|
2235
2234
|
|
|
2236
|
-
applyGlobalMaxTimeMS(this.options, this.model);
|
|
2237
|
-
applyGlobalDiskUse(this.options, this.model);
|
|
2235
|
+
applyGlobalMaxTimeMS(this.options, this.model.db.options, this.model.base.options);
|
|
2236
|
+
applyGlobalDiskUse(this.options, this.model.db.options, this.model.base.options);
|
|
2238
2237
|
|
|
2239
2238
|
// Separate options to pass down to `completeMany()` in case we need to
|
|
2240
2239
|
// set a session on the document
|
|
@@ -2271,7 +2270,7 @@ Query.prototype._find = async function _find() {
|
|
|
2271
2270
|
}
|
|
2272
2271
|
return mongooseOptions.lean ?
|
|
2273
2272
|
_completeManyLean(_this.model.schema, docs, null, completeManyOptions) :
|
|
2274
|
-
|
|
2273
|
+
_this._completeMany(docs, fields, userProvidedFields, completeManyOptions);
|
|
2275
2274
|
}
|
|
2276
2275
|
const pop = helpers.preparePopulationOptionsMQ(_this, mongooseOptions);
|
|
2277
2276
|
|
|
@@ -2279,7 +2278,7 @@ Query.prototype._find = async function _find() {
|
|
|
2279
2278
|
return _this.model.populate(docs, pop);
|
|
2280
2279
|
}
|
|
2281
2280
|
|
|
2282
|
-
docs = await
|
|
2281
|
+
docs = await _this._completeMany(docs, fields, userProvidedFields, completeManyOptions);
|
|
2283
2282
|
await this.model.populate(docs, pop);
|
|
2284
2283
|
|
|
2285
2284
|
return docs;
|
|
@@ -2473,6 +2472,36 @@ Query.prototype._completeOne = function(doc, res, callback) {
|
|
|
2473
2472
|
});
|
|
2474
2473
|
};
|
|
2475
2474
|
|
|
2475
|
+
/**
|
|
2476
|
+
* Given a model and an array of docs, hydrates all the docs to be instances
|
|
2477
|
+
* of the model. Used to initialize docs returned from the db from `find()`
|
|
2478
|
+
*
|
|
2479
|
+
* @param {Array} docs
|
|
2480
|
+
* @param {Object} fields the projection used, including `select` from schemas
|
|
2481
|
+
* @param {Object} userProvidedFields the user-specified projection
|
|
2482
|
+
* @param {Object} [opts]
|
|
2483
|
+
* @param {Array} [opts.populated]
|
|
2484
|
+
* @param {ClientSession} [opts.session]
|
|
2485
|
+
* @api private
|
|
2486
|
+
*/
|
|
2487
|
+
|
|
2488
|
+
Query.prototype._completeMany = async function _completeMany(docs, fields, userProvidedFields, opts) {
|
|
2489
|
+
const model = this.model;
|
|
2490
|
+
return Promise.all(docs.map(doc => new Promise((resolve, reject) => {
|
|
2491
|
+
const rawDoc = doc;
|
|
2492
|
+
doc = helpers.createModel(model, doc, fields, userProvidedFields);
|
|
2493
|
+
if (opts.session != null) {
|
|
2494
|
+
doc.$session(opts.session);
|
|
2495
|
+
}
|
|
2496
|
+
doc.$init(rawDoc, opts, (err) => {
|
|
2497
|
+
if (err != null) {
|
|
2498
|
+
return reject(err);
|
|
2499
|
+
}
|
|
2500
|
+
resolve(doc);
|
|
2501
|
+
});
|
|
2502
|
+
})));
|
|
2503
|
+
};
|
|
2504
|
+
|
|
2476
2505
|
/**
|
|
2477
2506
|
* Internal helper to execute a findOne() operation
|
|
2478
2507
|
*
|
|
@@ -2488,8 +2517,8 @@ Query.prototype._findOne = async function _findOne() {
|
|
|
2488
2517
|
throw err;
|
|
2489
2518
|
}
|
|
2490
2519
|
|
|
2491
|
-
applyGlobalMaxTimeMS(this.options, this.model);
|
|
2492
|
-
applyGlobalDiskUse(this.options, this.model);
|
|
2520
|
+
applyGlobalMaxTimeMS(this.options, this.model.db.options, this.model.base.options);
|
|
2521
|
+
applyGlobalDiskUse(this.options, this.model.db.options, this.model.base.options);
|
|
2493
2522
|
|
|
2494
2523
|
const options = this._optionsForExec();
|
|
2495
2524
|
|
|
@@ -2585,8 +2614,8 @@ Query.prototype._countDocuments = async function _countDocuments() {
|
|
|
2585
2614
|
throw this.error();
|
|
2586
2615
|
}
|
|
2587
2616
|
|
|
2588
|
-
applyGlobalMaxTimeMS(this.options, this.model);
|
|
2589
|
-
applyGlobalDiskUse(this.options, this.model);
|
|
2617
|
+
applyGlobalMaxTimeMS(this.options, this.model.db.options, this.model.base.options);
|
|
2618
|
+
applyGlobalDiskUse(this.options, this.model.db.options, this.model.base.options);
|
|
2590
2619
|
|
|
2591
2620
|
const options = this._optionsForExec();
|
|
2592
2621
|
|
|
@@ -2754,8 +2783,8 @@ Query.prototype.__distinct = async function __distinct() {
|
|
|
2754
2783
|
throw this.error();
|
|
2755
2784
|
}
|
|
2756
2785
|
|
|
2757
|
-
applyGlobalMaxTimeMS(this.options, this.model);
|
|
2758
|
-
applyGlobalDiskUse(this.options, this.model);
|
|
2786
|
+
applyGlobalMaxTimeMS(this.options, this.model.db.options, this.model.base.options);
|
|
2787
|
+
applyGlobalDiskUse(this.options, this.model.db.options, this.model.base.options);
|
|
2759
2788
|
|
|
2760
2789
|
const options = this._optionsForExec();
|
|
2761
2790
|
this._applyTranslateAliases(options);
|
|
@@ -3247,8 +3276,8 @@ Query.prototype._findOneAndUpdate = async function _findOneAndUpdate() {
|
|
|
3247
3276
|
throw this.error();
|
|
3248
3277
|
}
|
|
3249
3278
|
|
|
3250
|
-
applyGlobalMaxTimeMS(this.options, this.model);
|
|
3251
|
-
applyGlobalDiskUse(this.options, this.model);
|
|
3279
|
+
applyGlobalMaxTimeMS(this.options, this.model.db.options, this.model.base.options);
|
|
3280
|
+
applyGlobalDiskUse(this.options, this.model.db.options, this.model.base.options);
|
|
3252
3281
|
|
|
3253
3282
|
if ('strict' in this.options) {
|
|
3254
3283
|
this._mongooseOptions.strict = this.options.strict;
|
package/lib/schema.js
CHANGED
|
@@ -30,8 +30,7 @@ const hasNumericSubpathRegex = /\.\d+(\.|$)/;
|
|
|
30
30
|
|
|
31
31
|
let MongooseTypes;
|
|
32
32
|
|
|
33
|
-
const queryHooks = require('./
|
|
34
|
-
middlewareFunctions;
|
|
33
|
+
const queryHooks = require('./constants').queryMiddlewareFunctions;
|
|
35
34
|
const documentHooks = require('./helpers/model/applyHooks').middlewareFunctions;
|
|
36
35
|
const hookNames = queryHooks.concat(documentHooks).
|
|
37
36
|
reduce((s, hook) => s.add(hook), new Set());
|
package/package.json
CHANGED
package/types/connection.d.ts
CHANGED
|
@@ -240,6 +240,8 @@ declare module 'mongoose' {
|
|
|
240
240
|
|
|
241
241
|
/** Watches the entire underlying database for changes. Similar to [`Model.watch()`](/docs/api/model.html#model_Model-watch). */
|
|
242
242
|
watch<ResultType extends mongodb.Document = any>(pipeline?: Array<any>, options?: mongodb.ChangeStreamOptions): mongodb.ChangeStream<ResultType>;
|
|
243
|
+
|
|
244
|
+
withSession<T = any>(executor: (session: ClientSession) => Promise<T>): T;
|
|
243
245
|
}
|
|
244
246
|
|
|
245
247
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -373,8 +373,8 @@ declare module 'mongoose' {
|
|
|
373
373
|
// method aggregate and insertMany with ErrorHandlingMiddlewareFunction
|
|
374
374
|
post<T extends Aggregate<any>>(method: 'aggregate' | RegExp, fn: ErrorHandlingMiddlewareFunction<T, Array<any>>): this;
|
|
375
375
|
post<T extends Aggregate<any>>(method: 'aggregate' | RegExp, options: SchemaPostOptions, fn: ErrorHandlingMiddlewareFunction<T, Array<any>>): this;
|
|
376
|
-
post<T = TModelType>(method: 'insertMany' | RegExp, fn: ErrorHandlingMiddlewareFunction<T>): this;
|
|
377
|
-
post<T = TModelType>(method: 'insertMany' | RegExp, options: SchemaPostOptions, fn: ErrorHandlingMiddlewareFunction<T>): this;
|
|
376
|
+
post<T = TModelType>(method: 'bulkWrite' | 'createCollection' | 'insertMany' | RegExp, fn: ErrorHandlingMiddlewareFunction<T>): this;
|
|
377
|
+
post<T = TModelType>(method: 'bulkWrite' | 'createCollection' | 'insertMany' | RegExp, options: SchemaPostOptions, fn: ErrorHandlingMiddlewareFunction<T>): this;
|
|
378
378
|
|
|
379
379
|
/** Defines a pre hook for the model. */
|
|
380
380
|
// this = never since it never happens
|
|
@@ -429,6 +429,44 @@ declare module 'mongoose' {
|
|
|
429
429
|
options?: InsertManyOptions & { lean?: boolean }
|
|
430
430
|
) => void | Promise<void>
|
|
431
431
|
): this;
|
|
432
|
+
/* method bulkWrite */
|
|
433
|
+
pre<T = TModelType>(
|
|
434
|
+
method: 'bulkWrite' | RegExp,
|
|
435
|
+
fn: (
|
|
436
|
+
this: T,
|
|
437
|
+
next: (err?: CallbackError) => void,
|
|
438
|
+
ops: Array<mongodb.AnyBulkWriteOperation<any> & MongooseBulkWritePerWriteOptions>,
|
|
439
|
+
options?: mongodb.BulkWriteOptions & MongooseBulkWriteOptions
|
|
440
|
+
) => void | Promise<void>
|
|
441
|
+
): this;
|
|
442
|
+
pre<T = TModelType>(
|
|
443
|
+
method: 'bulkWrite' | RegExp,
|
|
444
|
+
options: SchemaPreOptions,
|
|
445
|
+
fn: (
|
|
446
|
+
this: T,
|
|
447
|
+
next: (err?: CallbackError) => void,
|
|
448
|
+
ops: Array<mongodb.AnyBulkWriteOperation<any> & MongooseBulkWritePerWriteOptions>,
|
|
449
|
+
options?: mongodb.BulkWriteOptions & MongooseBulkWriteOptions
|
|
450
|
+
) => void | Promise<void>
|
|
451
|
+
): this;
|
|
452
|
+
/* method createCollection */
|
|
453
|
+
pre<T = TModelType>(
|
|
454
|
+
method: 'createCollection' | RegExp,
|
|
455
|
+
fn: (
|
|
456
|
+
this: T,
|
|
457
|
+
next: (err?: CallbackError) => void,
|
|
458
|
+
options?: mongodb.CreateCollectionOptions & Pick<SchemaOptions, 'expires'>
|
|
459
|
+
) => void | Promise<void>
|
|
460
|
+
): this;
|
|
461
|
+
pre<T = TModelType>(
|
|
462
|
+
method: 'createCollection' | RegExp,
|
|
463
|
+
options: SchemaPreOptions,
|
|
464
|
+
fn: (
|
|
465
|
+
this: T,
|
|
466
|
+
next: (err?: CallbackError) => void,
|
|
467
|
+
options?: mongodb.CreateCollectionOptions & Pick<SchemaOptions, 'expires'>
|
|
468
|
+
) => void | Promise<void>
|
|
469
|
+
): this;
|
|
432
470
|
|
|
433
471
|
/** Object of currently defined query helpers on this schema. */
|
|
434
472
|
query: TQueryHelpers;
|
package/types/models.d.ts
CHANGED
|
@@ -37,6 +37,11 @@ declare module 'mongoose' {
|
|
|
37
37
|
skipValidation?: boolean;
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
+
interface HydrateOptions {
|
|
41
|
+
setters?: boolean;
|
|
42
|
+
hydratedPopulatedDocs?: boolean;
|
|
43
|
+
}
|
|
44
|
+
|
|
40
45
|
interface InsertManyOptions extends
|
|
41
46
|
PopulateOption,
|
|
42
47
|
SessionOption {
|
|
@@ -371,7 +376,7 @@ declare module 'mongoose' {
|
|
|
371
376
|
* Shortcut for creating a new Document from existing raw data, pre-saved in the DB.
|
|
372
377
|
* The document returned has no paths marked as modified initially.
|
|
373
378
|
*/
|
|
374
|
-
hydrate(obj: any, projection?: AnyObject, options?:
|
|
379
|
+
hydrate(obj: any, projection?: AnyObject, options?: HydrateOptions): THydratedDocumentType;
|
|
375
380
|
|
|
376
381
|
/**
|
|
377
382
|
* This function is responsible for building [indexes](https://www.mongodb.com/docs/manual/indexes/),
|
|
@@ -728,6 +733,9 @@ declare module 'mongoose' {
|
|
|
728
733
|
options?: (mongodb.ReplaceOptions & MongooseQueryOptions<TRawDocType>) | null
|
|
729
734
|
): QueryWithHelpers<UpdateWriteOpResult, ResultDoc, TQueryHelpers, TRawDocType, 'replaceOne'>;
|
|
730
735
|
|
|
736
|
+
/** Apply changes made to this model's schema after this model was compiled. */
|
|
737
|
+
recompileSchema(): void;
|
|
738
|
+
|
|
731
739
|
/** Schema the model uses. */
|
|
732
740
|
schema: Schema<TRawDocType>;
|
|
733
741
|
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/*!
|
|
4
|
-
* ignore
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
module.exports = applyQueryMiddleware;
|
|
8
|
-
|
|
9
|
-
const validOps = require('./validOps');
|
|
10
|
-
|
|
11
|
-
/*!
|
|
12
|
-
* ignore
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
applyQueryMiddleware.middlewareFunctions = validOps.concat([
|
|
16
|
-
'validate'
|
|
17
|
-
]);
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Apply query middleware
|
|
21
|
-
*
|
|
22
|
-
* @param {Query} Query constructor
|
|
23
|
-
* @param {Model} model
|
|
24
|
-
* @api private
|
|
25
|
-
*/
|
|
26
|
-
|
|
27
|
-
function applyQueryMiddleware(Query, model) {
|
|
28
|
-
const queryMiddleware = model.schema.s.hooks.filter(hook => {
|
|
29
|
-
const contexts = _getContexts(hook);
|
|
30
|
-
if (hook.name === 'validate') {
|
|
31
|
-
return !!contexts.query;
|
|
32
|
-
}
|
|
33
|
-
if (hook.name === 'deleteOne' || hook.name === 'updateOne') {
|
|
34
|
-
return !!contexts.query || Object.keys(contexts).length === 0;
|
|
35
|
-
}
|
|
36
|
-
if (hook.query != null || hook.document != null) {
|
|
37
|
-
return !!hook.query;
|
|
38
|
-
}
|
|
39
|
-
return true;
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
Query.prototype._queryMiddleware = queryMiddleware;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function _getContexts(hook) {
|
|
46
|
-
const ret = {};
|
|
47
|
-
if (hook.hasOwnProperty('query')) {
|
|
48
|
-
ret.query = hook.query;
|
|
49
|
-
}
|
|
50
|
-
if (hook.hasOwnProperty('document')) {
|
|
51
|
-
ret.document = hook.document;
|
|
52
|
-
}
|
|
53
|
-
return ret;
|
|
54
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const helpers = require('../../queryHelpers');
|
|
4
|
-
|
|
5
|
-
module.exports = completeMany;
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Given a model and an array of docs, hydrates all the docs to be instances
|
|
9
|
-
* of the model. Used to initialize docs returned from the db from `find()`
|
|
10
|
-
*
|
|
11
|
-
* @param {Model} model
|
|
12
|
-
* @param {Array} docs
|
|
13
|
-
* @param {Object} fields the projection used, including `select` from schemas
|
|
14
|
-
* @param {Object} userProvidedFields the user-specified projection
|
|
15
|
-
* @param {Object} [opts]
|
|
16
|
-
* @param {Array} [opts.populated]
|
|
17
|
-
* @param {ClientSession} [opts.session]
|
|
18
|
-
* @param {Function} callback
|
|
19
|
-
* @api private
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
async function completeMany(model, docs, fields, userProvidedFields, opts) {
|
|
23
|
-
return Promise.all(docs.map(doc => new Promise((resolve, reject) => {
|
|
24
|
-
const rawDoc = doc;
|
|
25
|
-
doc = helpers.createModel(model, doc, fields, userProvidedFields);
|
|
26
|
-
if (opts.session != null) {
|
|
27
|
-
doc.$session(opts.session);
|
|
28
|
-
}
|
|
29
|
-
doc.$init(rawDoc, opts, (err) => {
|
|
30
|
-
if (err != null) {
|
|
31
|
-
return reject(err);
|
|
32
|
-
}
|
|
33
|
-
resolve(doc);
|
|
34
|
-
});
|
|
35
|
-
})));
|
|
36
|
-
}
|