mongoose 7.8.0 → 7.8.2
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/cast.js
CHANGED
|
@@ -65,10 +65,11 @@ module.exports = function cast(schema, obj, options, context) {
|
|
|
65
65
|
if (!Array.isArray(val)) {
|
|
66
66
|
throw new CastError('Array', val, path);
|
|
67
67
|
}
|
|
68
|
-
for (let k =
|
|
68
|
+
for (let k = val.length - 1; k >= 0; k--) {
|
|
69
69
|
if (val[k] == null || typeof val[k] !== 'object') {
|
|
70
70
|
throw new CastError('Object', val[k], path + '.' + k);
|
|
71
71
|
}
|
|
72
|
+
const beforeCastKeysLength = Object.keys(val[k]).length;
|
|
72
73
|
const discriminatorValue = val[k][schema.options.discriminatorKey];
|
|
73
74
|
if (discriminatorValue == null) {
|
|
74
75
|
val[k] = cast(schema, val[k], options, context);
|
|
@@ -76,6 +77,15 @@ module.exports = function cast(schema, obj, options, context) {
|
|
|
76
77
|
const discriminatorSchema = getSchemaDiscriminatorByValue(context.schema, discriminatorValue);
|
|
77
78
|
val[k] = cast(discriminatorSchema ? discriminatorSchema : schema, val[k], options, context);
|
|
78
79
|
}
|
|
80
|
+
|
|
81
|
+
if (Object.keys(val[k]).length === 0 && beforeCastKeysLength !== 0) {
|
|
82
|
+
val.splice(k, 1);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// delete empty: {$or: []} -> {}
|
|
87
|
+
if (val.length === 0) {
|
|
88
|
+
delete obj[path];
|
|
79
89
|
}
|
|
80
90
|
} else if (path === '$where') {
|
|
81
91
|
type = typeof val;
|
package/lib/document.js
CHANGED
|
@@ -1204,7 +1204,7 @@ Document.prototype.$set = function $set(path, val, type, options) {
|
|
|
1204
1204
|
this.$__setValue(path, null);
|
|
1205
1205
|
cleanModifiedSubpaths(this, path);
|
|
1206
1206
|
} else {
|
|
1207
|
-
return this.$set(val, path, constructing);
|
|
1207
|
+
return this.$set(val, path, constructing, options);
|
|
1208
1208
|
}
|
|
1209
1209
|
|
|
1210
1210
|
const keys = getKeysInSchemaOrder(this.$__schema, val, path);
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const isDefiningProjection = require('./isDefiningProjection');
|
|
4
|
+
const isPOJO = require('../isPOJO');
|
|
4
5
|
|
|
5
6
|
/*!
|
|
6
7
|
* ignore
|
|
@@ -22,10 +23,12 @@ module.exports = function isExclusive(projection) {
|
|
|
22
23
|
// Explicitly avoid `$meta` and `$slice`
|
|
23
24
|
const key = keys[ki];
|
|
24
25
|
if (key !== '_id' && isDefiningProjection(projection[key])) {
|
|
25
|
-
exclude = (projection[key]
|
|
26
|
-
isExclusive(projection[key]) :
|
|
26
|
+
exclude = isPOJO(projection[key]) ?
|
|
27
|
+
(isExclusive(projection[key]) ?? exclude) :
|
|
27
28
|
!projection[key];
|
|
28
|
-
|
|
29
|
+
if (exclude != null) {
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
29
32
|
}
|
|
30
33
|
}
|
|
31
34
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const isDefiningProjection = require('./isDefiningProjection');
|
|
4
|
+
const isPOJO = require('../isPOJO');
|
|
4
5
|
|
|
5
6
|
/*!
|
|
6
7
|
* ignore
|
|
@@ -26,7 +27,7 @@ module.exports = function isInclusive(projection) {
|
|
|
26
27
|
// If field is truthy (1, true, etc.) and not an object, then this
|
|
27
28
|
// projection must be inclusive. If object, assume its $meta, $slice, etc.
|
|
28
29
|
if (isDefiningProjection(projection[prop]) && !!projection[prop]) {
|
|
29
|
-
if (projection[prop]
|
|
30
|
+
if (isPOJO(projection[prop])) {
|
|
30
31
|
return isInclusive(projection[prop]);
|
|
31
32
|
} else {
|
|
32
33
|
return !!projection[prop];
|
|
@@ -92,8 +92,12 @@ function _castExpression(val, schema, strictQuery) {
|
|
|
92
92
|
} else if (val.$ifNull != null) {
|
|
93
93
|
val.$ifNull.map(v => _castExpression(v, schema, strictQuery));
|
|
94
94
|
} else if (val.$switch != null) {
|
|
95
|
-
val.branches
|
|
96
|
-
|
|
95
|
+
if (Array.isArray(val.$switch.branches)) {
|
|
96
|
+
val.$switch.branches = val.$switch.branches.map(v => _castExpression(v, schema, strictQuery));
|
|
97
|
+
}
|
|
98
|
+
if ('default' in val.$switch) {
|
|
99
|
+
val.$switch.default = _castExpression(val.$switch.default, schema, strictQuery);
|
|
100
|
+
}
|
|
97
101
|
}
|
|
98
102
|
|
|
99
103
|
const keys = Object.keys(val);
|
package/lib/index.js
CHANGED
|
@@ -330,7 +330,7 @@ Mongoose.prototype.get = Mongoose.prototype.set;
|
|
|
330
330
|
*
|
|
331
331
|
* // initialize now, connect later
|
|
332
332
|
* db = mongoose.createConnection();
|
|
333
|
-
* db.openUri('127.0.0.1
|
|
333
|
+
* await db.openUri('mongodb://127.0.0.1:27017/database');
|
|
334
334
|
*
|
|
335
335
|
* @param {String} uri mongodb URI to connect to
|
|
336
336
|
* @param {Object} [options] passed down to the [MongoDB driver's `connect()` function](https://mongodb.github.io/node-mongodb-native/4.9/interfaces/MongoClientOptions.html), except for 4 mongoose-specific options explained below.
|
|
@@ -378,11 +378,10 @@ Mongoose.prototype.createConnection = function(uri, options) {
|
|
|
378
378
|
* // with options
|
|
379
379
|
* mongoose.connect(uri, options);
|
|
380
380
|
*
|
|
381
|
-
* //
|
|
381
|
+
* // Using `await` throws "MongooseServerSelectionError: Server selection timed out after 30000 ms"
|
|
382
|
+
* // if Mongoose can't connect.
|
|
382
383
|
* const uri = 'mongodb://nonexistent.domain:27000';
|
|
383
|
-
* mongoose.connect(uri
|
|
384
|
-
* // if error is truthy, the initial connection failed.
|
|
385
|
-
* })
|
|
384
|
+
* await mongoose.connect(uri);
|
|
386
385
|
*
|
|
387
386
|
* @param {String} uri mongodb URI to connect to
|
|
388
387
|
* @param {Object} [options] passed down to the [MongoDB driver's `connect()` function](https://mongodb.github.io/node-mongodb-native/4.9/interfaces/MongoClientOptions.html), except for 4 mongoose-specific options explained below.
|