mongoose 6.3.5 → 6.3.8
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/.eslintrc.json +1 -1
- package/dist/browser.umd.js +1 -1
- package/lgtm.yml +12 -0
- package/lib/connection.js +2 -1
- package/lib/document.js +22 -11
- package/lib/helpers/update/applyTimestampsToChildren.js +4 -0
- package/lib/helpers/update/castArrayFilters.js +3 -0
- package/lib/index.js +2 -1
- package/lib/options/SchemaTypeOptions.js +13 -0
- package/lib/query.js +4 -1
- package/lib/schema/SubdocumentPath.js +7 -0
- package/lib/schema/map.js +8 -0
- package/lib/schema/objectid.js +4 -3
- package/lib/schema.js +33 -2
- package/package.json +13 -14
- package/types/helpers.d.ts +3 -4
- package/types/index.d.ts +16 -3
- package/types/query.d.ts +1 -1
- package/types/schematypes.d.ts +22 -13
- package/.lgtm.yml +0 -3
package/lgtm.yml
ADDED
package/lib/connection.js
CHANGED
|
@@ -7,13 +7,13 @@
|
|
|
7
7
|
const ChangeStream = require('./cursor/ChangeStream');
|
|
8
8
|
const EventEmitter = require('events').EventEmitter;
|
|
9
9
|
const Schema = require('./schema');
|
|
10
|
-
const Collection = require('./driver').get().Collection;
|
|
11
10
|
const STATES = require('./connectionstate');
|
|
12
11
|
const MongooseError = require('./error/index');
|
|
13
12
|
const SyncIndexesError = require('./error/syncIndexes');
|
|
14
13
|
const PromiseProvider = require('./promise_provider');
|
|
15
14
|
const ServerSelectionError = require('./error/serverSelection');
|
|
16
15
|
const applyPlugins = require('./helpers/schema/applyPlugins');
|
|
16
|
+
const driver = require('./driver');
|
|
17
17
|
const promiseOrCallback = require('./helpers/promiseOrCallback');
|
|
18
18
|
const get = require('./helpers/get');
|
|
19
19
|
const immediate = require('./helpers/immediate');
|
|
@@ -1026,6 +1026,7 @@ Connection.prototype.collection = function(name, options) {
|
|
|
1026
1026
|
};
|
|
1027
1027
|
options = Object.assign({}, defaultOptions, options ? utils.clone(options) : {});
|
|
1028
1028
|
options.$wasForceClosed = this.$wasForceClosed;
|
|
1029
|
+
const Collection = driver.get().Collection;
|
|
1029
1030
|
if (!(name in this.collections)) {
|
|
1030
1031
|
this.collections[name] = new Collection(name, this, options);
|
|
1031
1032
|
}
|
package/lib/document.js
CHANGED
|
@@ -136,7 +136,6 @@ function Document(obj, fields, skipId, options) {
|
|
|
136
136
|
// excluded fields
|
|
137
137
|
if (utils.isPOJO(fields)) {
|
|
138
138
|
exclude = isExclusive(fields);
|
|
139
|
-
|
|
140
139
|
this.$__.fields = fields;
|
|
141
140
|
this.$__.exclude = exclude;
|
|
142
141
|
}
|
|
@@ -641,7 +640,11 @@ Document.prototype.$__buildDoc = function(obj, fields, skipId, exclude, hasInclu
|
|
|
641
640
|
for (let i = 0; i < len; ++i) {
|
|
642
641
|
const piece = path[i];
|
|
643
642
|
|
|
644
|
-
|
|
643
|
+
if (!curPath.length) {
|
|
644
|
+
curPath = piece;
|
|
645
|
+
} else {
|
|
646
|
+
curPath += '.' + piece;
|
|
647
|
+
}
|
|
645
648
|
|
|
646
649
|
// support excluding intermediary levels
|
|
647
650
|
if (exclude === true) {
|
|
@@ -1045,8 +1048,7 @@ Document.prototype.$set = function $set(path, val, type, options) {
|
|
|
1045
1048
|
type = undefined;
|
|
1046
1049
|
}
|
|
1047
1050
|
|
|
1048
|
-
|
|
1049
|
-
const merge = options.merge;
|
|
1051
|
+
const merge = options && options.merge;
|
|
1050
1052
|
const adhoc = type && type !== true;
|
|
1051
1053
|
const constructing = type === true;
|
|
1052
1054
|
let adhocs;
|
|
@@ -1056,7 +1058,7 @@ Document.prototype.$set = function $set(path, val, type, options) {
|
|
|
1056
1058
|
let key;
|
|
1057
1059
|
let prefix;
|
|
1058
1060
|
|
|
1059
|
-
const strict = 'strict' in options
|
|
1061
|
+
const strict = options && 'strict' in options
|
|
1060
1062
|
? options.strict
|
|
1061
1063
|
: this.$__.strictMode;
|
|
1062
1064
|
|
|
@@ -1087,7 +1089,7 @@ Document.prototype.$set = function $set(path, val, type, options) {
|
|
|
1087
1089
|
|
|
1088
1090
|
// `_skipMinimizeTopLevel` is because we may have deleted the top-level
|
|
1089
1091
|
// nested key to ensure key order.
|
|
1090
|
-
const _skipMinimizeTopLevel = options._skipMinimizeTopLevel || false;
|
|
1092
|
+
const _skipMinimizeTopLevel = options && options._skipMinimizeTopLevel || false;
|
|
1091
1093
|
if (len === 0 && _skipMinimizeTopLevel) {
|
|
1092
1094
|
delete options._skipMinimizeTopLevel;
|
|
1093
1095
|
if (val) {
|
|
@@ -1565,7 +1567,7 @@ Document.prototype.set = Document.prototype.$set;
|
|
|
1565
1567
|
*/
|
|
1566
1568
|
|
|
1567
1569
|
Document.prototype.$__shouldModify = function(pathToMark, path, options, constructing, parts, schema, val, priorVal) {
|
|
1568
|
-
if (options._skipMarkModified) {
|
|
1570
|
+
if (options && options._skipMarkModified) {
|
|
1569
1571
|
return false;
|
|
1570
1572
|
}
|
|
1571
1573
|
if (this.$isNew) {
|
|
@@ -3474,8 +3476,8 @@ Document.prototype.$toObject = function(options, json) {
|
|
|
3474
3476
|
defaultOptions = utils.options(defaultOptions, clone(schemaOptions[path] || {}));
|
|
3475
3477
|
|
|
3476
3478
|
// If options do not exist or is not an object, set it to empty object
|
|
3477
|
-
options = utils.isPOJO(options) ?
|
|
3478
|
-
options._calledWithOptions = options._calledWithOptions ||
|
|
3479
|
+
options = utils.isPOJO(options) ? { ...options } : {};
|
|
3480
|
+
options._calledWithOptions = options._calledWithOptions || { ...options };
|
|
3479
3481
|
|
|
3480
3482
|
let _minimize;
|
|
3481
3483
|
if (options._calledWithOptions.minimize != null) {
|
|
@@ -3499,7 +3501,7 @@ Document.prototype.$toObject = function(options, json) {
|
|
|
3499
3501
|
// `clone()` will recursively call `$toObject()` on embedded docs, so we
|
|
3500
3502
|
// need the original options the user passed in, plus `_isNested` and
|
|
3501
3503
|
// `_parentOptions` for checking whether we need to depopulate.
|
|
3502
|
-
const cloneOptions = Object.assign(
|
|
3504
|
+
const cloneOptions = Object.assign({}, options, {
|
|
3503
3505
|
_isNested: true,
|
|
3504
3506
|
json: json,
|
|
3505
3507
|
minimize: _minimize,
|
|
@@ -3929,12 +3931,19 @@ function applySchemaTypeTransforms(self, json) {
|
|
|
3929
3931
|
const schematype = schema.paths[path];
|
|
3930
3932
|
if (typeof schematype.options.transform === 'function') {
|
|
3931
3933
|
const val = self.$get(path);
|
|
3934
|
+
if (val === undefined) {
|
|
3935
|
+
continue;
|
|
3936
|
+
}
|
|
3932
3937
|
const transformedValue = schematype.options.transform.call(self, val);
|
|
3933
3938
|
throwErrorIfPromise(path, transformedValue);
|
|
3934
3939
|
utils.setValue(path, transformedValue, json);
|
|
3935
3940
|
} else if (schematype.$embeddedSchemaType != null &&
|
|
3936
3941
|
typeof schematype.$embeddedSchemaType.options.transform === 'function') {
|
|
3937
|
-
const
|
|
3942
|
+
const val = self.$get(path);
|
|
3943
|
+
if (val === undefined) {
|
|
3944
|
+
continue;
|
|
3945
|
+
}
|
|
3946
|
+
const vals = [].concat(val);
|
|
3938
3947
|
const transform = schematype.$embeddedSchemaType.options.transform;
|
|
3939
3948
|
for (let i = 0; i < vals.length; ++i) {
|
|
3940
3949
|
const transformedValue = transform.call(self, vals[i]);
|
|
@@ -4227,8 +4236,10 @@ Document.prototype.populate = function populate() {
|
|
|
4227
4236
|
* @api public
|
|
4228
4237
|
* @return {Array<Document>} array of populated documents. Empty array if there are no populated documents associated with this document.
|
|
4229
4238
|
* @memberOf Document
|
|
4239
|
+
* @method $getPopulatedDocs
|
|
4230
4240
|
* @instance
|
|
4231
4241
|
*/
|
|
4242
|
+
|
|
4232
4243
|
Document.prototype.$getPopulatedDocs = function $getPopulatedDocs() {
|
|
4233
4244
|
let keys = [];
|
|
4234
4245
|
if (this.$__.populated != null) {
|
|
@@ -61,6 +61,8 @@ function applyTimestampsToChildren(now, update, schema) {
|
|
|
61
61
|
if (createdAt != null) {
|
|
62
62
|
subdoc[createdAt] = now;
|
|
63
63
|
}
|
|
64
|
+
|
|
65
|
+
applyTimestampsToChildren(now, subdoc, $path.schema);
|
|
64
66
|
});
|
|
65
67
|
} else {
|
|
66
68
|
if (updatedAt != null) {
|
|
@@ -69,6 +71,8 @@ function applyTimestampsToChildren(now, update, schema) {
|
|
|
69
71
|
if (createdAt != null) {
|
|
70
72
|
op[key][createdAt] = now;
|
|
71
73
|
}
|
|
74
|
+
|
|
75
|
+
applyTimestampsToChildren(now, op[key], $path.schema);
|
|
72
76
|
}
|
|
73
77
|
}
|
|
74
78
|
}
|
|
@@ -15,6 +15,9 @@ module.exports = function castArrayFilters(query) {
|
|
|
15
15
|
if (query._mongooseOptions.strict != null) {
|
|
16
16
|
strictQuery = query._mongooseOptions.strict;
|
|
17
17
|
}
|
|
18
|
+
if (query.model && query.model.base.options.strictQuery != null) {
|
|
19
|
+
strictQuery = query.model.base.options.strictQuery;
|
|
20
|
+
}
|
|
18
21
|
if (schema._userProvidedOptions.strictQuery != null) {
|
|
19
22
|
strictQuery = schema._userProvidedOptions.strictQuery;
|
|
20
23
|
}
|
package/lib/index.js
CHANGED
|
@@ -154,7 +154,7 @@ Mongoose.prototype.driver = driver;
|
|
|
154
154
|
* mongoose.set('debug', function(collectionName, methodName, ...methodArgs) {}); // use custom function to log collection methods + arguments
|
|
155
155
|
*
|
|
156
156
|
* Currently supported options are:
|
|
157
|
-
* - '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
|
|
157
|
+
* - '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(', ')})`.
|
|
158
158
|
* - '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.
|
|
159
159
|
* - 'bufferCommands': enable/disable mongoose's buffering mechanism for all connections and models
|
|
160
160
|
* - 'cloneSchemas': false by default. Set to `true` to `clone()` all schemas before compiling into a model.
|
|
@@ -275,6 +275,7 @@ Mongoose.prototype.get = Mongoose.prototype.set;
|
|
|
275
275
|
Mongoose.prototype.createConnection = function(uri, options, callback) {
|
|
276
276
|
const _mongoose = this instanceof Mongoose ? this : mongoose;
|
|
277
277
|
|
|
278
|
+
const Connection = driver.get().getConnection();
|
|
278
279
|
const conn = new Connection(_mongoose);
|
|
279
280
|
if (typeof options === 'function') {
|
|
280
281
|
callback = options;
|
|
@@ -119,6 +119,19 @@ Object.defineProperty(SchemaTypeOptions.prototype, 'default', opts);
|
|
|
119
119
|
|
|
120
120
|
Object.defineProperty(SchemaTypeOptions.prototype, 'ref', opts);
|
|
121
121
|
|
|
122
|
+
/**
|
|
123
|
+
* The path in the document that `populate()` should use to find the model
|
|
124
|
+
* to use.
|
|
125
|
+
*
|
|
126
|
+
* @api public
|
|
127
|
+
* @property ref
|
|
128
|
+
* @memberOf SchemaTypeOptions
|
|
129
|
+
* @type Function|String
|
|
130
|
+
* @instance
|
|
131
|
+
*/
|
|
132
|
+
|
|
133
|
+
Object.defineProperty(SchemaTypeOptions.prototype, 'refPath', opts);
|
|
134
|
+
|
|
122
135
|
/**
|
|
123
136
|
* Whether to include or exclude this path by default when loading documents
|
|
124
137
|
* using `find()`, `findOne()`, etc.
|
package/lib/query.js
CHANGED
|
@@ -124,7 +124,10 @@ function Query(conditions, options, model, collection) {
|
|
|
124
124
|
}
|
|
125
125
|
|
|
126
126
|
// inherit mquery
|
|
127
|
-
mquery.call(this,
|
|
127
|
+
mquery.call(this, null, options);
|
|
128
|
+
if (collection) {
|
|
129
|
+
this.collection(collection);
|
|
130
|
+
}
|
|
128
131
|
|
|
129
132
|
if (conditions) {
|
|
130
133
|
this.find(conditions);
|
|
@@ -33,6 +33,13 @@ module.exports = SubdocumentPath;
|
|
|
33
33
|
*/
|
|
34
34
|
|
|
35
35
|
function SubdocumentPath(schema, path, options) {
|
|
36
|
+
const schemaTypeIdOption = SubdocumentPath.defaultOptions &&
|
|
37
|
+
SubdocumentPath.defaultOptions._id;
|
|
38
|
+
if (schemaTypeIdOption != null) {
|
|
39
|
+
options = options || {};
|
|
40
|
+
options._id = schemaTypeIdOption;
|
|
41
|
+
}
|
|
42
|
+
|
|
36
43
|
schema = handleIdOption(schema, options);
|
|
37
44
|
|
|
38
45
|
this.caster = _createConstructor(schema);
|
package/lib/schema/map.js
CHANGED
|
@@ -69,6 +69,14 @@ class Map extends SchemaType {
|
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
+
/**
|
|
73
|
+
* This schema type's name, to defend against minifiers that mangle
|
|
74
|
+
* function names.
|
|
75
|
+
*
|
|
76
|
+
* @api public
|
|
77
|
+
*/
|
|
78
|
+
Map.schemaName = 'Map';
|
|
79
|
+
|
|
72
80
|
Map.prototype.OptionsConstructor = SchemaMapOptions;
|
|
73
81
|
|
|
74
82
|
Map.defaultOptions = {};
|
package/lib/schema/objectid.js
CHANGED
|
@@ -9,6 +9,7 @@ const SchemaType = require('../schematype');
|
|
|
9
9
|
const castObjectId = require('../cast/objectid');
|
|
10
10
|
const getConstructorName = require('../helpers/getConstructorName');
|
|
11
11
|
const oid = require('../types/objectid');
|
|
12
|
+
const isBsonType = require('../helpers/isBsonType');
|
|
12
13
|
const utils = require('../utils');
|
|
13
14
|
|
|
14
15
|
const CastError = SchemaType.CastError;
|
|
@@ -113,7 +114,7 @@ ObjectId.prototype.auto = function(turnOn) {
|
|
|
113
114
|
* ignore
|
|
114
115
|
*/
|
|
115
116
|
|
|
116
|
-
ObjectId._checkRequired = v => v
|
|
117
|
+
ObjectId._checkRequired = v => isBsonType(v, 'ObjectID');
|
|
117
118
|
|
|
118
119
|
/*!
|
|
119
120
|
* ignore
|
|
@@ -161,7 +162,7 @@ ObjectId.cast = function cast(caster) {
|
|
|
161
162
|
*/
|
|
162
163
|
|
|
163
164
|
ObjectId._defaultCaster = v => {
|
|
164
|
-
if (!(v
|
|
165
|
+
if (!(isBsonType(v, 'ObjectID'))) {
|
|
165
166
|
throw new Error(v + ' is not an instance of ObjectId');
|
|
166
167
|
}
|
|
167
168
|
return v;
|
|
@@ -221,7 +222,7 @@ ObjectId.prototype.checkRequired = function checkRequired(value, doc) {
|
|
|
221
222
|
*/
|
|
222
223
|
|
|
223
224
|
ObjectId.prototype.cast = function(value, doc, init) {
|
|
224
|
-
if (!(value
|
|
225
|
+
if (!(isBsonType(value, 'ObjectID')) && SchemaType._isRef(this, value, doc, init)) {
|
|
225
226
|
// wait! we may need to cast this to a document
|
|
226
227
|
if ((getConstructorName(value) || '').toLowerCase() === 'objectid') {
|
|
227
228
|
return new oid(value.toHexString());
|
package/lib/schema.js
CHANGED
|
@@ -473,9 +473,39 @@ Schema.prototype.defaultOptions = function(options) {
|
|
|
473
473
|
return options;
|
|
474
474
|
};
|
|
475
475
|
|
|
476
|
+
/**
|
|
477
|
+
* Inherit a Schema by applying a discriminator on an existing Schema.
|
|
478
|
+
*
|
|
479
|
+
*
|
|
480
|
+
* ####Example:
|
|
481
|
+
*
|
|
482
|
+
* const options = { discriminatorKey: 'kind' };
|
|
483
|
+
*
|
|
484
|
+
* const eventSchema = new mongoose.Schema({ time: Date }, options);
|
|
485
|
+
* const Event = mongoose.model('Event', eventSchema);
|
|
486
|
+
*
|
|
487
|
+
* // ClickedLinkEvent is a special type of Event that has
|
|
488
|
+
* // a URL.
|
|
489
|
+
* const ClickedLinkEvent = Event.discriminator('ClickedLink',
|
|
490
|
+
* new mongoose.Schema({ url: String }, options));
|
|
491
|
+
*
|
|
492
|
+
* // When you create a generic event, it can't have a URL field...
|
|
493
|
+
* const genericEvent = new Event({ time: Date.now(), url: 'google.com' });
|
|
494
|
+
* assert.ok(!genericEvent.url);
|
|
495
|
+
* // But a ClickedLinkEvent can
|
|
496
|
+
* const clickedEvent = new ClickedLinkEvent({ time: Date.now(), url: 'google.com' });
|
|
497
|
+
* assert.ok(clickedEvent.url);
|
|
498
|
+
*
|
|
499
|
+
* @param {String} name the name of the discriminator
|
|
500
|
+
* @param {Schema} schema the Schema of the discriminated Schema
|
|
501
|
+
* @return {Schema} the Schema instance
|
|
502
|
+
* @api public
|
|
503
|
+
*/
|
|
504
|
+
|
|
476
505
|
Schema.prototype.discriminator = function(name, schema) {
|
|
477
|
-
this._applyDiscriminators = {};
|
|
478
|
-
|
|
506
|
+
this._applyDiscriminators = Object.assign(this._applyDiscriminators || {}, { [name]: schema });
|
|
507
|
+
|
|
508
|
+
return this;
|
|
479
509
|
};
|
|
480
510
|
|
|
481
511
|
/**
|
|
@@ -1172,6 +1202,7 @@ Schema.prototype.interpretAsType = function(path, obj, options) {
|
|
|
1172
1202
|
}
|
|
1173
1203
|
|
|
1174
1204
|
if (type && type.instanceOfSchema) {
|
|
1205
|
+
|
|
1175
1206
|
return new MongooseTypes.Subdocument(type, path, obj);
|
|
1176
1207
|
}
|
|
1177
1208
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mongoose",
|
|
3
3
|
"description": "Mongoose MongoDB ODM",
|
|
4
|
-
"version": "6.3.
|
|
4
|
+
"version": "6.3.8",
|
|
5
5
|
"author": "Guillermo Rauch <guillermo@learnboost.com>",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"mongodb",
|
|
@@ -28,10 +28,9 @@
|
|
|
28
28
|
"sift": "16.0.0"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"@babel/core": "7.
|
|
32
|
-
"@
|
|
33
|
-
"@typescript-eslint/
|
|
34
|
-
"@typescript-eslint/parser": "5.21.0",
|
|
31
|
+
"@babel/core": "7.18.2",
|
|
32
|
+
"@typescript-eslint/eslint-plugin": "5.27.0",
|
|
33
|
+
"@typescript-eslint/parser": "5.27.0",
|
|
35
34
|
"acquit": "1.2.1",
|
|
36
35
|
"acquit-ignore": "0.2.0",
|
|
37
36
|
"acquit-require": "0.1.1",
|
|
@@ -41,31 +40,31 @@
|
|
|
41
40
|
"benchmark": "2.1.4",
|
|
42
41
|
"bluebird": "3.7.2",
|
|
43
42
|
"buffer": "^5.6.0",
|
|
44
|
-
"cheerio": "1.0.0-rc.
|
|
43
|
+
"cheerio": "1.0.0-rc.11",
|
|
45
44
|
"crypto-browserify": "3.12.0",
|
|
46
45
|
"dox": "0.3.1",
|
|
47
|
-
"eslint": "8.
|
|
46
|
+
"eslint": "8.16.0",
|
|
48
47
|
"eslint-plugin-mocha-no-only": "1.1.1",
|
|
49
48
|
"highlight.js": "11.5.1",
|
|
50
49
|
"lodash.isequal": "4.5.0",
|
|
51
50
|
"lodash.isequalwith": "4.4.0",
|
|
52
|
-
"marked": "4.0.
|
|
51
|
+
"marked": "4.0.16",
|
|
53
52
|
"mkdirp": "^1.0.4",
|
|
54
53
|
"mocha": "10.0.0",
|
|
55
54
|
"moment": "2.x",
|
|
56
|
-
"mongodb-memory-server": "8.
|
|
55
|
+
"mongodb-memory-server": "8.6.0",
|
|
57
56
|
"ncp": "^2.0.0",
|
|
58
57
|
"nyc": "15.1.0",
|
|
59
58
|
"pug": "3.0.2",
|
|
60
59
|
"q": "1.5.1",
|
|
61
60
|
"serve-handler": "6.1.3",
|
|
62
|
-
"sinon": "
|
|
61
|
+
"sinon": "14.0.0",
|
|
63
62
|
"stream-browserify": "3.0.0",
|
|
64
63
|
"ts-benchmark": "^1.0.2",
|
|
65
64
|
"tsd": "0.20.0",
|
|
66
|
-
"typescript": "4.
|
|
65
|
+
"typescript": "4.7.2",
|
|
67
66
|
"uuid": "8.3.2",
|
|
68
|
-
"webpack": "5.72.
|
|
67
|
+
"webpack": "5.72.1"
|
|
69
68
|
},
|
|
70
69
|
"directories": {
|
|
71
70
|
"lib": "./lib/mongoose"
|
|
@@ -97,7 +96,7 @@
|
|
|
97
96
|
"test": "mocha --exit ./test/*.test.js",
|
|
98
97
|
"test-rs": "START_REPLICA_SET=1 mocha --timeout 30000 --exit ./test/*.test.js",
|
|
99
98
|
"test-tsd": "node ./test/types/check-types-filename && tsd",
|
|
100
|
-
"tdd": "mocha ./test/*.test.js
|
|
99
|
+
"tdd": "mocha ./test/*.test.js --inspect --watch --recursive --watch-files ./**/*.{js,ts}",
|
|
101
100
|
"test-coverage": "nyc --reporter=html --reporter=text npm test",
|
|
102
101
|
"ts-benchmark": "ts-benchmark -p ./benchmarks/typescript/simple -f 17 18 29 32",
|
|
103
102
|
"ts-benchmark-watch": "ts-benchmark -p ./benchmarks/typescript/simple -w ./types -i -s -f 17 18 29 32 -b master"
|
|
@@ -146,4 +145,4 @@
|
|
|
146
145
|
"target": "ES2017"
|
|
147
146
|
}
|
|
148
147
|
}
|
|
149
|
-
}
|
|
148
|
+
}
|
package/types/helpers.d.ts
CHANGED
|
@@ -20,14 +20,13 @@ declare module 'mongoose' {
|
|
|
20
20
|
* of an ObjectId.
|
|
21
21
|
*/
|
|
22
22
|
function isObjectIdOrHexString(v: mongodb.ObjectId): true;
|
|
23
|
-
function isObjectIdOrHexString(v: string): boolean;
|
|
23
|
+
function isObjectIdOrHexString(v: mongodb.ObjectId | string): boolean;
|
|
24
24
|
function isObjectIdOrHexString(v: any): false;
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
27
|
* Returns true if Mongoose can cast the given value to an ObjectId, or
|
|
28
28
|
* false otherwise.
|
|
29
29
|
*/
|
|
30
|
-
function isValidObjectId(v: mongodb.ObjectId): true;
|
|
31
|
-
function isValidObjectId(v: Types.ObjectId): true;
|
|
30
|
+
function isValidObjectId(v: mongodb.ObjectId | Types.ObjectId): true;
|
|
32
31
|
function isValidObjectId(v: any): boolean;
|
|
33
|
-
}
|
|
32
|
+
}
|
package/types/index.d.ts
CHANGED
|
@@ -120,6 +120,17 @@ declare module 'mongoose' {
|
|
|
120
120
|
useProjection?: boolean;
|
|
121
121
|
}
|
|
122
122
|
|
|
123
|
+
export type DiscriminatorModel<M, T> = T extends Model<infer T1, infer T2, infer T3, infer T4>
|
|
124
|
+
?
|
|
125
|
+
M extends Model<infer M1, infer M2, infer M3, infer M4>
|
|
126
|
+
? Model<Omit<M1, keyof T1> & T1, M2 | T2, M3 | T3, M4 | T4>
|
|
127
|
+
: M
|
|
128
|
+
: M;
|
|
129
|
+
|
|
130
|
+
export type DiscriminatorSchema<DocType, M, TInstanceMethods, TQueryHelpers, TVirtuals, T> = T extends Schema<infer T1, infer T2, infer T3, infer T4, infer T5>
|
|
131
|
+
? Schema<Omit<DocType, keyof T1> & T1, DiscriminatorModel<T2, M>, T3 | TInstanceMethods, T4 | TQueryHelpers, T5 | TVirtuals>
|
|
132
|
+
: Schema<DocType, M, TInstanceMethods, TQueryHelpers, TVirtuals>;
|
|
133
|
+
|
|
123
134
|
export class Schema<DocType = any, M = Model<DocType, any, any, any>, TInstanceMethods = {}, TQueryHelpers = {}, TVirtuals = any> extends events.EventEmitter {
|
|
124
135
|
/**
|
|
125
136
|
* Create a new schema
|
|
@@ -142,6 +153,8 @@ declare module 'mongoose' {
|
|
|
142
153
|
/** Returns a copy of this schema */
|
|
143
154
|
clone<T = this>(): T;
|
|
144
155
|
|
|
156
|
+
discriminator<T extends Schema = Schema>(name: string, schema: T): DiscriminatorSchema<DocType, M, TInstanceMethods, TQueryHelpers, TVirtuals, T>;
|
|
157
|
+
|
|
145
158
|
/** Returns a new schema that has the picked `paths` from this schema. */
|
|
146
159
|
pick<T = this>(paths: string[], options?: SchemaOptions): T;
|
|
147
160
|
|
|
@@ -474,10 +487,10 @@ declare module 'mongoose' {
|
|
|
474
487
|
type LeanTypeOrArray<T> = T extends unknown[] ? LeanArray<T> : LeanType<T>;
|
|
475
488
|
|
|
476
489
|
export type LeanArray<T extends unknown[]> =
|
|
477
|
-
|
|
478
|
-
|
|
490
|
+
// By checking if it extends Types.Array we can get the original base type before collapsing down,
|
|
491
|
+
// rather than trying to manually remove the old types. This matches both Array and DocumentArray
|
|
479
492
|
T extends Types.Array<infer U> ? LeanTypeOrArray<U>[] :
|
|
480
|
-
|
|
493
|
+
// If it isn't a custom mongoose type we fall back to "do our best"
|
|
481
494
|
T extends unknown[][] ? LeanArray<T[number]>[] : LeanType<T[number]>[];
|
|
482
495
|
|
|
483
496
|
export type _LeanDocument<T> = {
|
package/types/query.d.ts
CHANGED
|
@@ -579,7 +579,7 @@ declare module 'mongoose' {
|
|
|
579
579
|
snapshot(val?: boolean): this;
|
|
580
580
|
|
|
581
581
|
/** Sets the sort order. If an object is passed, values allowed are `asc`, `desc`, `ascending`, `descending`, `1`, and `-1`. */
|
|
582
|
-
sort(arg
|
|
582
|
+
sort(arg?: string | { [key: string]: SortOrder | { $meta: 'textScore' } } | undefined | null): this;
|
|
583
583
|
|
|
584
584
|
/** Sets the tailable option (for use with capped collections). */
|
|
585
585
|
tailable(bool?: boolean, opts?: {
|
package/types/schematypes.d.ts
CHANGED
|
@@ -37,6 +37,8 @@ declare module 'mongoose' {
|
|
|
37
37
|
/** The various Mongoose SchemaTypes. */
|
|
38
38
|
const SchemaTypes: typeof Schema.Types;
|
|
39
39
|
|
|
40
|
+
type DefaultType<T> = T extends Schema.Types.Mixed ? any : Partial<ExtractMongooseArray<T>>;
|
|
41
|
+
|
|
40
42
|
class SchemaTypeOptions<T> {
|
|
41
43
|
type?:
|
|
42
44
|
T extends string ? StringSchemaDefinition :
|
|
@@ -74,13 +76,20 @@ declare module 'mongoose' {
|
|
|
74
76
|
* The default value for this path. If a function, Mongoose executes the function
|
|
75
77
|
* and uses the return value as the default.
|
|
76
78
|
*/
|
|
77
|
-
default?: T
|
|
79
|
+
default?: DefaultType<T> | ((this: any, doc: any) => DefaultType<T>) | null;
|
|
78
80
|
|
|
79
81
|
/**
|
|
80
82
|
* The model that `populate()` should use if populating this path.
|
|
81
83
|
*/
|
|
82
84
|
ref?: string | Model<any> | ((this: any, doc: any) => string | Model<any>);
|
|
83
85
|
|
|
86
|
+
/**
|
|
87
|
+
* The path in the document that `populate()` should use to find the model
|
|
88
|
+
* to use.
|
|
89
|
+
*/
|
|
90
|
+
|
|
91
|
+
refPath?: string | ((this: any, doc: any) => string);
|
|
92
|
+
|
|
84
93
|
/**
|
|
85
94
|
* Whether to include or exclude this path by default when loading documents
|
|
86
95
|
* using `find()`, `findOne()`, etc.
|
|
@@ -126,7 +135,7 @@ declare module 'mongoose' {
|
|
|
126
135
|
transform?: (this: any, val: T) => any;
|
|
127
136
|
|
|
128
137
|
/** defines a custom getter for this property using [`Object.defineProperty()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty). */
|
|
129
|
-
get?: (value: any, doc?: this) => T;
|
|
138
|
+
get?: (value: any, doc?: this) => T | undefined;
|
|
130
139
|
|
|
131
140
|
/** defines a custom setter for this property using [`Object.defineProperty()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty). */
|
|
132
141
|
set?: (value: any, priorVal?: T, doc?: this) => any;
|
|
@@ -271,7 +280,7 @@ declare module 'mongoose' {
|
|
|
271
280
|
namespace Types {
|
|
272
281
|
class Array extends SchemaType implements AcceptsDiscriminator {
|
|
273
282
|
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
274
|
-
static schemaName:
|
|
283
|
+
static schemaName: 'Array';
|
|
275
284
|
|
|
276
285
|
static options: { castNonArrays: boolean; };
|
|
277
286
|
|
|
@@ -290,7 +299,7 @@ declare module 'mongoose' {
|
|
|
290
299
|
|
|
291
300
|
class Boolean extends SchemaType {
|
|
292
301
|
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
293
|
-
static schemaName:
|
|
302
|
+
static schemaName: 'Boolean';
|
|
294
303
|
|
|
295
304
|
/** Configure which values get casted to `true`. */
|
|
296
305
|
static convertToTrue: Set<any>;
|
|
@@ -301,7 +310,7 @@ declare module 'mongoose' {
|
|
|
301
310
|
|
|
302
311
|
class Buffer extends SchemaType {
|
|
303
312
|
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
304
|
-
static schemaName:
|
|
313
|
+
static schemaName: 'Buffer';
|
|
305
314
|
|
|
306
315
|
/**
|
|
307
316
|
* Sets the default [subtype](https://studio3t.com/whats-new/best-practices-uuid-mongodb/)
|
|
@@ -312,7 +321,7 @@ declare module 'mongoose' {
|
|
|
312
321
|
|
|
313
322
|
class Date extends SchemaType {
|
|
314
323
|
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
315
|
-
static schemaName:
|
|
324
|
+
static schemaName: 'Date';
|
|
316
325
|
|
|
317
326
|
/** Declares a TTL index (rounded to the nearest second) for _Date_ types only. */
|
|
318
327
|
expires(when: number | string): this;
|
|
@@ -326,12 +335,12 @@ declare module 'mongoose' {
|
|
|
326
335
|
|
|
327
336
|
class Decimal128 extends SchemaType {
|
|
328
337
|
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
329
|
-
static schemaName:
|
|
338
|
+
static schemaName: 'Decimal128';
|
|
330
339
|
}
|
|
331
340
|
|
|
332
341
|
class DocumentArray extends SchemaType implements AcceptsDiscriminator {
|
|
333
342
|
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
334
|
-
static schemaName:
|
|
343
|
+
static schemaName: 'DocumentArray';
|
|
335
344
|
|
|
336
345
|
static options: { castNonArrays: boolean; };
|
|
337
346
|
|
|
@@ -347,17 +356,17 @@ declare module 'mongoose' {
|
|
|
347
356
|
|
|
348
357
|
class Map extends SchemaType {
|
|
349
358
|
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
350
|
-
static schemaName:
|
|
359
|
+
static schemaName: 'Map';
|
|
351
360
|
}
|
|
352
361
|
|
|
353
362
|
class Mixed extends SchemaType {
|
|
354
363
|
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
355
|
-
static schemaName:
|
|
364
|
+
static schemaName: 'Mixed';
|
|
356
365
|
}
|
|
357
366
|
|
|
358
367
|
class Number extends SchemaType {
|
|
359
368
|
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
360
|
-
static schemaName:
|
|
369
|
+
static schemaName: 'Number';
|
|
361
370
|
|
|
362
371
|
/** Sets a enum validator */
|
|
363
372
|
enum(vals: number[]): this;
|
|
@@ -371,7 +380,7 @@ declare module 'mongoose' {
|
|
|
371
380
|
|
|
372
381
|
class ObjectId extends SchemaType {
|
|
373
382
|
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
374
|
-
static schemaName:
|
|
383
|
+
static schemaName: 'ObjectId';
|
|
375
384
|
|
|
376
385
|
/** Adds an auto-generated ObjectId default if turnOn is true. */
|
|
377
386
|
auto(turnOn: boolean): this;
|
|
@@ -390,7 +399,7 @@ declare module 'mongoose' {
|
|
|
390
399
|
|
|
391
400
|
class String extends SchemaType {
|
|
392
401
|
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
393
|
-
static schemaName:
|
|
402
|
+
static schemaName: 'String';
|
|
394
403
|
|
|
395
404
|
/** Adds an enum validator */
|
|
396
405
|
enum(vals: string[] | any): this;
|
package/.lgtm.yml
DELETED