mongoose 6.4.4 → 6.4.7
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/aggregate.js +32 -30
- package/lib/browser.js +1 -1
- package/lib/cast.js +2 -2
- package/lib/collection.js +1 -1
- package/lib/connection.js +34 -16
- package/lib/document.js +175 -102
- package/lib/helpers/timestamps/setDocumentTimestamps.js +26 -0
- package/lib/helpers/timestamps/setupTimestamps.js +14 -18
- package/lib/helpers/topology/isAtlas.js +14 -9
- package/lib/index.js +3 -2
- package/lib/model.js +117 -159
- package/lib/query.js +18 -12
- package/lib/schema.js +8 -1
- package/lib/schematype.js +95 -81
- package/lib/virtualtype.js +14 -16
- package/package.json +4 -4
- package/types/connection.d.ts +1 -1
- package/types/expressions.d.ts +5 -4
- package/types/index.d.ts +9 -7
- package/types/indexes.d.ts +2 -2
- package/types/inferschematype.d.ts +26 -10
- package/types/models.d.ts +5 -5
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports = function setDocumentTimestamps(doc, timestampOption, currentTime, createdAt, updatedAt) {
|
|
4
|
+
const skipUpdatedAt = timestampOption != null && timestampOption.updatedAt === false;
|
|
5
|
+
const skipCreatedAt = timestampOption != null && timestampOption.createdAt === false;
|
|
6
|
+
|
|
7
|
+
const defaultTimestamp = currentTime != null ?
|
|
8
|
+
currentTime() :
|
|
9
|
+
doc.ownerDocument().constructor.base.now();
|
|
10
|
+
|
|
11
|
+
if (!skipCreatedAt &&
|
|
12
|
+
(doc.isNew || doc.$isSubdocument) &&
|
|
13
|
+
createdAt &&
|
|
14
|
+
!doc.$__getValue(createdAt) &&
|
|
15
|
+
doc.$__isSelected(createdAt)) {
|
|
16
|
+
doc.$set(createdAt, defaultTimestamp, undefined, { overwriteImmutable: true });
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (!skipUpdatedAt && updatedAt && (doc.isNew || doc.$isModified())) {
|
|
20
|
+
let ts = defaultTimestamp;
|
|
21
|
+
if (doc.isNew && createdAt != null) {
|
|
22
|
+
ts = doc.$__getValue(createdAt);
|
|
23
|
+
}
|
|
24
|
+
doc.$set(updatedAt, ts);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
@@ -4,6 +4,7 @@ const applyTimestampsToChildren = require('../update/applyTimestampsToChildren')
|
|
|
4
4
|
const applyTimestampsToUpdate = require('../update/applyTimestampsToUpdate');
|
|
5
5
|
const get = require('../get');
|
|
6
6
|
const handleTimestampOption = require('../schema/handleTimestampOption');
|
|
7
|
+
const setDocumentTimestamps = require('./setDocumentTimestamps');
|
|
7
8
|
const symbols = require('../../schema/symbols');
|
|
8
9
|
|
|
9
10
|
module.exports = function setupTimestamps(schema, timestamps) {
|
|
@@ -44,24 +45,7 @@ module.exports = function setupTimestamps(schema, timestamps) {
|
|
|
44
45
|
return next();
|
|
45
46
|
}
|
|
46
47
|
|
|
47
|
-
|
|
48
|
-
const skipCreatedAt = timestampOption != null && timestampOption.createdAt === false;
|
|
49
|
-
|
|
50
|
-
const defaultTimestamp = currentTime != null ?
|
|
51
|
-
currentTime() :
|
|
52
|
-
this.ownerDocument().constructor.base.now();
|
|
53
|
-
|
|
54
|
-
if (!skipCreatedAt && (this.isNew || this.$isSubdocument) && createdAt && !this.$__getValue(createdAt) && this.$__isSelected(createdAt)) {
|
|
55
|
-
this.$set(createdAt, defaultTimestamp, undefined, { overwriteImmutable: true });
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
if (!skipUpdatedAt && updatedAt && (this.isNew || this.$isModified())) {
|
|
59
|
-
let ts = defaultTimestamp;
|
|
60
|
-
if (this.isNew && createdAt != null) {
|
|
61
|
-
ts = this.$__getValue(createdAt);
|
|
62
|
-
}
|
|
63
|
-
this.$set(updatedAt, ts);
|
|
64
|
-
}
|
|
48
|
+
setDocumentTimestamps(this, timestampOption, currentTime, createdAt, updatedAt);
|
|
65
49
|
|
|
66
50
|
next();
|
|
67
51
|
});
|
|
@@ -76,6 +60,18 @@ module.exports = function setupTimestamps(schema, timestamps) {
|
|
|
76
60
|
if (updatedAt && !this.get(updatedAt)) {
|
|
77
61
|
this.$set(updatedAt, ts);
|
|
78
62
|
}
|
|
63
|
+
|
|
64
|
+
if (this.$isSubdocument) {
|
|
65
|
+
return this;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const subdocs = this.$getAllSubdocs();
|
|
69
|
+
for (const subdoc of subdocs) {
|
|
70
|
+
if (subdoc.initializeTimestamps) {
|
|
71
|
+
subdoc.initializeTimestamps();
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
79
75
|
return this;
|
|
80
76
|
};
|
|
81
77
|
|
|
@@ -2,25 +2,30 @@
|
|
|
2
2
|
|
|
3
3
|
const getConstructorName = require('../getConstructorName');
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* @typedef { import('mongodb').TopologyDescription } TopologyDescription
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Checks if topologyDescription contains servers connected to an atlas instance
|
|
11
|
+
*
|
|
12
|
+
* @param {TopologyDescription} topologyDescription
|
|
13
|
+
* @returns {boolean}
|
|
14
|
+
*/
|
|
5
15
|
module.exports = function isAtlas(topologyDescription) {
|
|
6
16
|
if (getConstructorName(topologyDescription) !== 'TopologyDescription') {
|
|
7
17
|
return false;
|
|
8
18
|
}
|
|
9
19
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
if (hostnames.length === 0) {
|
|
20
|
+
if (topologyDescription.servers.size === 0) {
|
|
13
21
|
return false;
|
|
14
22
|
}
|
|
15
23
|
|
|
16
|
-
for (
|
|
17
|
-
|
|
18
|
-
if (
|
|
19
|
-
url.hostname.endsWith('.mongodb.net') === false ||
|
|
20
|
-
url.port !== '27017'
|
|
21
|
-
) {
|
|
24
|
+
for (const server of topologyDescription.servers.values()) {
|
|
25
|
+
if (server.host.endsWith('.mongodb.net') === false || server.port !== 27017) {
|
|
22
26
|
return false;
|
|
23
27
|
}
|
|
24
28
|
}
|
|
29
|
+
|
|
25
30
|
return true;
|
|
26
31
|
};
|
package/lib/index.js
CHANGED
|
@@ -191,7 +191,7 @@ Mongoose.prototype.setDriver = function setDriver(driver) {
|
|
|
191
191
|
* Currently supported options are:
|
|
192
192
|
* - 'applyPluginsToChildSchemas': `true` by default. Set to false to skip applying global plugins to child schemas
|
|
193
193
|
* - 'applyPluginsToDiscriminators': `false` by default. Set to true to apply global plugins to discriminator schemas. This typically isn't necessary because plugins are applied to the base schema and discriminators copy all middleware, methods, statics, and properties from the base schema.
|
|
194
|
-
* - 'autoCreate': Set to `true` to make Mongoose call [`Model.createCollection()`](/docs/api/model.html#model_Model
|
|
194
|
+
* - 'autoCreate': Set to `true` to make Mongoose call [`Model.createCollection()`](/docs/api/model.html#model_Model-createCollection) automatically when you create a model with `mongoose.model()` or `conn.model()`. This is useful for testing transactions, change streams, and other features that require the collection to exist.
|
|
195
195
|
* - 'autoIndex': `true` by default. Set to false to disable automatic index creation for all models associated with this Mongoose instance.
|
|
196
196
|
* - 'debug': If `true`, prints the operations mongoose sends to MongoDB to the console. If a writable stream is passed, it will log to that stream, without colorization. If a callback function is passed, it will receive the collection name, the method name, then all arguments passed to the method. For example, if you wanted to replicate the default logging, you could output from the callback `Mongoose: ${collectionName}.${methodName}(${methodArgs.join(', ')})`.
|
|
197
197
|
* - 'returnOriginal': If `false`, changes the default `returnOriginal` option to `findOneAndUpdate()`, `findByIdAndUpdate`, and `findOneAndReplace()` to false. This is equivalent to setting the `new` option to `true` for `findOneAndX()` calls by default. Read our [`findOneAndUpdate()` tutorial](/docs/tutorials/findoneandupdate.html) for more information.
|
|
@@ -204,6 +204,7 @@ Mongoose.prototype.setDriver = function setDriver(driver) {
|
|
|
204
204
|
* - 'overwriteModels': Set to `true` to default to overwriting models with the same name when calling `mongoose.model()`, as opposed to throwing an `OverwriteModelError`.
|
|
205
205
|
* - 'returnOriginal': If `false`, changes the default `returnOriginal` option to `findOneAndUpdate()`, `findByIdAndUpdate`, and `findOneAndReplace()` to false. This is equivalent to setting the `new` option to `true` for `findOneAndX()` calls by default. Read our [`findOneAndUpdate()` tutorial](/docs/tutorials/findoneandupdate.html) for more information.
|
|
206
206
|
* - 'runValidators': `false` by default. Set to true to enable [update validators](/docs/validation.html#update-validators) for all validators by default.
|
|
207
|
+
* - 'sanitizeFilter': `false` by default. Set to true to enable the [sanitization of the query filters](/docs/api.html#mongoose_Mongoose-sanitizeFilter) against query selector injection attacks by wrapping any nested objects that have a property whose name starts with `$` in a `$eq`.
|
|
207
208
|
* - 'selectPopulatedPaths': `true` by default. Set to false to opt out of Mongoose adding all fields that you `populate()` to your `select()`. The schema-level option `selectPopulatedPaths` overwrites this one.
|
|
208
209
|
* - 'strict': `true` by default, may be `false`, `true`, or `'throw'`. Sets the default strict mode for schemas.
|
|
209
210
|
* - 'strictQuery': same value as 'strict' by default (`true`), may be `false`, `true`, or `'throw'`. Sets the default [strictQuery](/docs/guide.html#strictQuery) mode for schemas.
|
|
@@ -860,7 +861,7 @@ Mongoose.prototype.SchemaType = SchemaType;
|
|
|
860
861
|
* _Alias of mongoose.Schema.Types for backwards compatibility._
|
|
861
862
|
*
|
|
862
863
|
* @property SchemaTypes
|
|
863
|
-
* @see Schema.SchemaTypes #schema_Schema
|
|
864
|
+
* @see Schema.SchemaTypes #schema_Schema-Types
|
|
864
865
|
* @api public
|
|
865
866
|
*/
|
|
866
867
|
|