mongoose 8.24.7 → 8.24.9
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/document.js +10 -0
- package/lib/helpers/processConnectionOptions.js +6 -1
- package/lib/schema/union.js +4 -0
- package/lib/schemaType.js +1 -1
- package/lib/types/map.js +16 -0
- package/package.json +1 -1
package/lib/document.js
CHANGED
|
@@ -26,6 +26,7 @@ const compile = require('./helpers/document/compile').compile;
|
|
|
26
26
|
const defineKey = require('./helpers/document/compile').defineKey;
|
|
27
27
|
const firstKey = require('./helpers/firstKey');
|
|
28
28
|
const flatten = require('./helpers/common').flatten;
|
|
29
|
+
const getConstructorName = require('./helpers/getConstructorName');
|
|
29
30
|
const getEmbeddedDiscriminatorPath = require('./helpers/document/getEmbeddedDiscriminatorPath');
|
|
30
31
|
const getKeysInSchemaOrder = require('./helpers/schema/getKeysInSchemaOrder');
|
|
31
32
|
const getSubdocumentStrictValue = require('./helpers/schema/getSubdocumentStrictValue');
|
|
@@ -3414,6 +3415,15 @@ Document.prototype.invalidate = function(path, err, val, kind) {
|
|
|
3414
3415
|
return this.$__.validationError;
|
|
3415
3416
|
}
|
|
3416
3417
|
|
|
3418
|
+
// Set the model on cast errors with a custom message so `{MODEL}` gets
|
|
3419
|
+
// replaced for document validation, not just query casting (gh-8300)
|
|
3420
|
+
if (typeof err.setModel === 'function' && err.messageFormat != null) {
|
|
3421
|
+
const owner = this.ownerDocument();
|
|
3422
|
+
if (getConstructorName(owner) === 'model') {
|
|
3423
|
+
err.setModel(owner.constructor);
|
|
3424
|
+
}
|
|
3425
|
+
}
|
|
3426
|
+
|
|
3417
3427
|
this.$__.validationError.addError(path, err);
|
|
3418
3428
|
return this.$__.validationError;
|
|
3419
3429
|
};
|
|
@@ -6,7 +6,7 @@ const MongooseError = require('../error/index');
|
|
|
6
6
|
function processConnectionOptions(uri, options) {
|
|
7
7
|
const opts = options ? options : {};
|
|
8
8
|
const readPreference = opts.readPreference
|
|
9
|
-
? opts.readPreference
|
|
9
|
+
? getReadPreferenceMode(opts.readPreference)
|
|
10
10
|
: getUriReadPreference(uri);
|
|
11
11
|
|
|
12
12
|
const clonedOpts = clone(opts);
|
|
@@ -40,6 +40,11 @@ function setsSecondaryRead(prefString) {
|
|
|
40
40
|
return !!(prefString === 'secondary' || prefString === 'secondaryPreferred');
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
+
function getReadPreferenceMode(readPreference) {
|
|
44
|
+
// the MongoDB driver also accepts a `ReadPreference` instance or a `{ mode }` object
|
|
45
|
+
return typeof readPreference === 'string' ? readPreference : readPreference.mode;
|
|
46
|
+
}
|
|
47
|
+
|
|
43
48
|
function getUriReadPreference(connectionString) {
|
|
44
49
|
const exp = /(?:&|\?)readPreference=(\w+)(?:&|$)/;
|
|
45
50
|
const match = exp.exec(connectionString);
|
package/lib/schema/union.js
CHANGED
|
@@ -59,6 +59,10 @@ class Union extends SchemaType {
|
|
|
59
59
|
|
|
60
60
|
// Setters also need to be aware of casting - we need to apply the setters of the entry in the union we choose.
|
|
61
61
|
applySetters(val, doc, init, prev, options) {
|
|
62
|
+
// Setters on the union path itself run first, so they can change which entry
|
|
63
|
+
// in the union the value resolves to.
|
|
64
|
+
val = this._applySetters(val, doc, init, prev, options);
|
|
65
|
+
|
|
62
66
|
let firstValue = firstValueSymbol;
|
|
63
67
|
let lastError;
|
|
64
68
|
// Loop through each schema type in the union. If one of the schematypes returns a value that is `=== val`, then
|
package/lib/schemaType.js
CHANGED
|
@@ -1625,7 +1625,7 @@ SchemaType.prototype._castRef = function _castRef(value, doc, init, options) {
|
|
|
1625
1625
|
!doc.$__.populated[path].options ||
|
|
1626
1626
|
!doc.$__.populated[path].options.options ||
|
|
1627
1627
|
!doc.$__.populated[path].options.options.lean) {
|
|
1628
|
-
const PopulatedModel = pop ? pop.options[populateModelSymbol] :
|
|
1628
|
+
const PopulatedModel = pop ? pop.options[populateModelSymbol] : owner.constructor.db.model(this.options.ref);
|
|
1629
1629
|
ret = PopulatedModel.hydrate(value, null, options);
|
|
1630
1630
|
ret.$__.wasPopulated = { value: ret._doc._id, options: { [populateModelSymbol]: PopulatedModel } };
|
|
1631
1631
|
}
|
package/lib/types/map.js
CHANGED
|
@@ -87,6 +87,22 @@ class MongooseMap extends Map {
|
|
|
87
87
|
return this.$__schemaType.applyGetters(super.get(key), this.$__parent);
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
+
/**
|
|
91
|
+
* Overwrites native Map's `has()` function to support ObjectIds as keys.
|
|
92
|
+
*
|
|
93
|
+
* @api public
|
|
94
|
+
* @method has
|
|
95
|
+
* @memberOf Map
|
|
96
|
+
*/
|
|
97
|
+
|
|
98
|
+
has(key) {
|
|
99
|
+
if (isBsonType(key, 'ObjectId')) {
|
|
100
|
+
key = key.toString();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return super.has(key);
|
|
104
|
+
}
|
|
105
|
+
|
|
90
106
|
/**
|
|
91
107
|
* Overwrites native Map's `set()` function to support setters, `populate()`,
|
|
92
108
|
* and change tracking. Note that Mongoose maps _only_ support strings and
|