mongoose 9.0.1 → 9.0.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/document.js +9 -2
- package/lib/drivers/node-mongodb-native/collection.js +28 -109
- package/lib/model.js +9 -4
- package/package.json +2 -1
- package/types/index.d.ts +11 -4
- package/types/inferrawdoctype.d.ts +9 -3
- package/types/inferschematype.d.ts +20 -27
- package/types/query.d.ts +1 -1
- package/types/utility.d.ts +1 -1
- package/types/virtuals.d.ts +3 -3
package/lib/document.js
CHANGED
|
@@ -648,6 +648,10 @@ Document.prototype.init = function(doc, opts, fn) {
|
|
|
648
648
|
opts = null;
|
|
649
649
|
}
|
|
650
650
|
|
|
651
|
+
if (doc == null) {
|
|
652
|
+
throw new ObjectParameterError(doc, 'doc', 'init');
|
|
653
|
+
}
|
|
654
|
+
|
|
651
655
|
this.$__init(doc, opts);
|
|
652
656
|
|
|
653
657
|
if (fn) {
|
|
@@ -677,6 +681,9 @@ Document.prototype.$init = function() {
|
|
|
677
681
|
*/
|
|
678
682
|
|
|
679
683
|
Document.prototype.$__init = function(doc, opts) {
|
|
684
|
+
if (doc == null) {
|
|
685
|
+
throw new ObjectParameterError(doc, 'doc', 'init');
|
|
686
|
+
}
|
|
680
687
|
this.$isNew = false;
|
|
681
688
|
opts = opts || {};
|
|
682
689
|
|
|
@@ -5174,7 +5181,7 @@ function operand(self, where, delta, data, val, op) {
|
|
|
5174
5181
|
if (/\.\d+\.|\.\d+$/.test(data.path)) {
|
|
5175
5182
|
self.$__.version = VERSION_ALL;
|
|
5176
5183
|
} else {
|
|
5177
|
-
self.$__.version
|
|
5184
|
+
self.$__.version |= VERSION_INC;
|
|
5178
5185
|
}
|
|
5179
5186
|
} else if (/^\$p/.test(op)) {
|
|
5180
5187
|
// potentially changing array positions
|
|
@@ -5185,7 +5192,7 @@ function operand(self, where, delta, data, val, op) {
|
|
|
5185
5192
|
} else if (/\.\d+\.|\.\d+$/.test(data.path)) {
|
|
5186
5193
|
// now handling $set, $unset
|
|
5187
5194
|
// subpath of array
|
|
5188
|
-
self.$__.version
|
|
5195
|
+
self.$__.version |= VERSION_WHERE;
|
|
5189
5196
|
}
|
|
5190
5197
|
}
|
|
5191
5198
|
|
|
@@ -80,12 +80,6 @@ NativeCollection.prototype._getCollection = function _getCollection() {
|
|
|
80
80
|
return null;
|
|
81
81
|
};
|
|
82
82
|
|
|
83
|
-
/*!
|
|
84
|
-
* ignore
|
|
85
|
-
*/
|
|
86
|
-
|
|
87
|
-
const syncCollectionMethods = { watch: true, find: true, aggregate: true };
|
|
88
|
-
|
|
89
83
|
/**
|
|
90
84
|
* Copy the collection methods and make them subject to queues
|
|
91
85
|
* @param {Number|String} I
|
|
@@ -107,7 +101,6 @@ function iter(i) {
|
|
|
107
101
|
_this.conn.options &&
|
|
108
102
|
_this.conn.options.debug;
|
|
109
103
|
const debug = connectionDebug == null ? globalDebug : connectionDebug;
|
|
110
|
-
const lastArg = arguments[arguments.length - 1];
|
|
111
104
|
const opId = new ObjectId();
|
|
112
105
|
|
|
113
106
|
// If user force closed, queueing will hang forever. See #5664
|
|
@@ -122,9 +115,8 @@ function iter(i) {
|
|
|
122
115
|
}
|
|
123
116
|
}
|
|
124
117
|
|
|
125
|
-
let _args = args;
|
|
126
|
-
let callback = null;
|
|
127
118
|
let timeout = null;
|
|
119
|
+
let waitForBufferPromise = null;
|
|
128
120
|
if (this._shouldBufferCommands() && this.buffer) {
|
|
129
121
|
this.conn.emit('buffer', {
|
|
130
122
|
_id: opId,
|
|
@@ -134,78 +126,28 @@ function iter(i) {
|
|
|
134
126
|
args: args
|
|
135
127
|
});
|
|
136
128
|
|
|
137
|
-
let callback;
|
|
138
|
-
let _args = args;
|
|
139
|
-
let promise = null;
|
|
140
|
-
if (syncCollectionMethods[i] && typeof lastArg === 'function') {
|
|
141
|
-
this.addQueue(i, _args);
|
|
142
|
-
callback = lastArg;
|
|
143
|
-
} else if (syncCollectionMethods[i]) {
|
|
144
|
-
promise = new this.Promise((resolve, reject) => {
|
|
145
|
-
callback = function collectionOperationCallback(err, res) {
|
|
146
|
-
if (timeout != null) {
|
|
147
|
-
clearTimeout(timeout);
|
|
148
|
-
}
|
|
149
|
-
if (err != null) {
|
|
150
|
-
return reject(err);
|
|
151
|
-
}
|
|
152
|
-
resolve(res);
|
|
153
|
-
};
|
|
154
|
-
_args = args.concat([callback]);
|
|
155
|
-
this.addQueue(i, _args);
|
|
156
|
-
});
|
|
157
|
-
} else if (typeof lastArg === 'function') {
|
|
158
|
-
callback = function collectionOperationCallback() {
|
|
159
|
-
if (timeout != null) {
|
|
160
|
-
clearTimeout(timeout);
|
|
161
|
-
}
|
|
162
|
-
return lastArg.apply(this, arguments);
|
|
163
|
-
};
|
|
164
|
-
_args = args.slice(0, args.length - 1).concat([callback]);
|
|
165
|
-
} else {
|
|
166
|
-
promise = new Promise((resolve, reject) => {
|
|
167
|
-
callback = function collectionOperationCallback(err, res) {
|
|
168
|
-
if (timeout != null) {
|
|
169
|
-
clearTimeout(timeout);
|
|
170
|
-
}
|
|
171
|
-
if (err != null) {
|
|
172
|
-
return reject(err);
|
|
173
|
-
}
|
|
174
|
-
resolve(res);
|
|
175
|
-
};
|
|
176
|
-
_args = args.concat([callback]);
|
|
177
|
-
this.addQueue(i, _args);
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
|
|
181
129
|
const bufferTimeoutMS = this._getBufferTimeoutMS();
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
}
|
|
130
|
+
waitForBufferPromise = new Promise((resolve, reject) => {
|
|
131
|
+
this.addQueue(resolve);
|
|
132
|
+
|
|
133
|
+
timeout = setTimeout(() => {
|
|
134
|
+
const removed = this.removeQueue(resolve);
|
|
135
|
+
if (removed) {
|
|
136
|
+
const message = 'Operation `' + this.name + '.' + i + '()` buffering timed out after ' +
|
|
137
|
+
bufferTimeoutMS + 'ms';
|
|
138
|
+
const err = new MongooseError(message);
|
|
139
|
+
this.conn.emit('buffer-end', { _id: opId, modelName: _this.modelName, collectionName: _this.name, method: i, error: err });
|
|
140
|
+
reject(err);
|
|
141
|
+
}
|
|
142
|
+
}, bufferTimeoutMS);
|
|
143
|
+
});
|
|
197
144
|
|
|
198
|
-
return
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
if (err != null) {
|
|
202
|
-
_this.conn.emit('operation-end', { _id: opId, modelName: _this.modelName, collectionName: _this.name, method: i, error: err });
|
|
203
|
-
} else {
|
|
204
|
-
_this.conn.emit('operation-end', { _id: opId, modelName: _this.modelName, collectionName: _this.name, method: i, result: res });
|
|
145
|
+
return waitForBufferPromise.then(() => {
|
|
146
|
+
if (timeout) {
|
|
147
|
+
clearTimeout(timeout);
|
|
205
148
|
}
|
|
206
|
-
return
|
|
207
|
-
};
|
|
208
|
-
_args = args.slice(0, args.length - 1).concat([callback]);
|
|
149
|
+
return this[i].apply(this, args);
|
|
150
|
+
});
|
|
209
151
|
}
|
|
210
152
|
|
|
211
153
|
if (debug) {
|
|
@@ -227,7 +169,7 @@ function iter(i) {
|
|
|
227
169
|
}
|
|
228
170
|
}
|
|
229
171
|
|
|
230
|
-
this.conn.emit('operation-start', { _id: opId, modelName: _this.modelName, collectionName: this.name, method: i, params:
|
|
172
|
+
this.conn.emit('operation-start', { _id: opId, modelName: _this.modelName, collectionName: this.name, method: i, params: args });
|
|
231
173
|
|
|
232
174
|
try {
|
|
233
175
|
if (collection == null) {
|
|
@@ -237,60 +179,37 @@ function iter(i) {
|
|
|
237
179
|
throw new MongooseError(message);
|
|
238
180
|
}
|
|
239
181
|
|
|
240
|
-
|
|
241
|
-
const result = collection[i].apply(collection, _args.slice(0, _args.length - 1));
|
|
242
|
-
if (timeout != null) {
|
|
243
|
-
clearTimeout(timeout);
|
|
244
|
-
}
|
|
245
|
-
this.conn.emit('operation-end', { _id: opId, modelName: _this.modelName, collectionName: this.name, method: i, result });
|
|
246
|
-
return lastArg.call(this, null, result);
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
const ret = collection[i].apply(collection, _args);
|
|
182
|
+
const ret = collection[i].apply(collection, args);
|
|
250
183
|
if (ret != null && typeof ret.then === 'function') {
|
|
251
184
|
return ret.then(
|
|
252
185
|
result => {
|
|
253
186
|
if (timeout != null) {
|
|
254
187
|
clearTimeout(timeout);
|
|
255
188
|
}
|
|
256
|
-
|
|
257
|
-
lastArg(null, result);
|
|
258
|
-
} else {
|
|
259
|
-
this.conn.emit('operation-end', { _id: opId, modelName: _this.modelName, collectionName: this.name, method: i, result });
|
|
260
|
-
}
|
|
189
|
+
this.conn.emit('operation-end', { _id: opId, modelName: _this.modelName, collectionName: this.name, method: i, result });
|
|
261
190
|
return result;
|
|
262
191
|
},
|
|
263
192
|
error => {
|
|
264
193
|
if (timeout != null) {
|
|
265
194
|
clearTimeout(timeout);
|
|
266
195
|
}
|
|
267
|
-
|
|
268
|
-
lastArg(error);
|
|
269
|
-
return;
|
|
270
|
-
} else {
|
|
271
|
-
this.conn.emit('operation-end', { _id: opId, modelName: _this.modelName, collectionName: this.name, method: i, error });
|
|
272
|
-
}
|
|
196
|
+
this.conn.emit('operation-end', { _id: opId, modelName: _this.modelName, collectionName: this.name, method: i, error });
|
|
273
197
|
throw error;
|
|
274
198
|
}
|
|
275
199
|
);
|
|
276
200
|
}
|
|
201
|
+
|
|
202
|
+
this.conn.emit('operation-end', { _id: opId, modelName: _this.modelName, collectionName: this.name, method: i, result: ret });
|
|
277
203
|
if (timeout != null) {
|
|
278
204
|
clearTimeout(timeout);
|
|
279
205
|
}
|
|
280
206
|
return ret;
|
|
281
207
|
} catch (error) {
|
|
282
|
-
// Collection operation may throw because of max bson size, catch it here
|
|
283
|
-
// See gh-3906
|
|
284
208
|
if (timeout != null) {
|
|
285
209
|
clearTimeout(timeout);
|
|
286
210
|
}
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
} else {
|
|
290
|
-
this.conn.emit('operation-end', { _id: opId, modelName: _this.modelName, collectionName: this.name, method: i, error: error });
|
|
291
|
-
|
|
292
|
-
throw error;
|
|
293
|
-
}
|
|
211
|
+
this.conn.emit('operation-end', { _id: opId, modelName: _this.modelName, collectionName: this.name, method: i, error: error });
|
|
212
|
+
throw error;
|
|
294
213
|
}
|
|
295
214
|
};
|
|
296
215
|
}
|
package/lib/model.js
CHANGED
|
@@ -3248,12 +3248,14 @@ function _setIsNew(doc, val) {
|
|
|
3248
3248
|
* @param {Object} [ops.updateOne.update] An object containing [update operators](https://www.mongodb.com/docs/manual/reference/operator/update/)
|
|
3249
3249
|
* @param {Boolean} [ops.updateOne.upsert=false] If true, insert a doc if none match
|
|
3250
3250
|
* @param {Boolean} [ops.updateOne.timestamps=true] If false, do not apply [timestamps](https://mongoosejs.com/docs/guide.html#timestamps) to the operation
|
|
3251
|
+
* @param {Boolean} [ops.updateOne.overwriteImmutable=false] Mongoose removes updated immutable properties from `update` by default (excluding $setOnInsert). Set `overwriteImmutable` to `true` to allow updating immutable properties using other update operators.
|
|
3251
3252
|
* @param {Object} [ops.updateOne.collation] The [MongoDB collation](https://thecodebarbarian.com/a-nodejs-perspective-on-mongodb-34-collations) to use
|
|
3252
3253
|
* @param {Array} [ops.updateOne.arrayFilters] The [array filters](https://thecodebarbarian.com/a-nodejs-perspective-on-mongodb-36-array-filters.html) used in `update`
|
|
3253
3254
|
* @param {Object} [ops.updateMany.filter] Update all the documents that match this filter
|
|
3254
3255
|
* @param {Object} [ops.updateMany.update] An object containing [update operators](https://www.mongodb.com/docs/manual/reference/operator/update/)
|
|
3255
3256
|
* @param {Boolean} [ops.updateMany.upsert=false] If true, insert a doc if no documents match `filter`
|
|
3256
3257
|
* @param {Boolean} [ops.updateMany.timestamps=true] If false, do not apply [timestamps](https://mongoosejs.com/docs/guide.html#timestamps) to the operation
|
|
3258
|
+
* @param {Boolean} [ops.updateMany.overwriteImmutable=false] Mongoose removes updated immutable properties from `update` by default (excluding $setOnInsert). Set `overwriteImmutable` to `true` to allow updating immutable properties using other update operators.
|
|
3257
3259
|
* @param {Object} [ops.updateMany.collation] The [MongoDB collation](https://thecodebarbarian.com/a-nodejs-perspective-on-mongodb-34-collations) to use
|
|
3258
3260
|
* @param {Array} [ops.updateMany.arrayFilters] The [array filters](https://thecodebarbarian.com/a-nodejs-perspective-on-mongodb-36-array-filters.html) used in `update`
|
|
3259
3261
|
* @param {Object} [ops.deleteOne.filter] Delete the first document that matches this filter
|
|
@@ -3286,12 +3288,15 @@ Model.bulkWrite = async function bulkWrite(ops, options) {
|
|
|
3286
3288
|
}
|
|
3287
3289
|
options = options || {};
|
|
3288
3290
|
|
|
3289
|
-
|
|
3291
|
+
try {
|
|
3292
|
+
[ops, options] = await this.hooks.execPre('bulkWrite', this, [ops, options]);
|
|
3293
|
+
} catch (err) {
|
|
3290
3294
|
if (err instanceof Kareem.skipWrappedFunction) {
|
|
3291
|
-
|
|
3295
|
+
ops = err;
|
|
3296
|
+
} else {
|
|
3297
|
+
await this.hooks.execPost('bulkWrite', this, [null], { error: err });
|
|
3292
3298
|
}
|
|
3293
|
-
|
|
3294
|
-
});
|
|
3299
|
+
}
|
|
3295
3300
|
|
|
3296
3301
|
if (ops instanceof Kareem.skipWrappedFunction) {
|
|
3297
3302
|
return ops.args[0];
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mongoose",
|
|
3
3
|
"description": "Mongoose MongoDB ODM",
|
|
4
|
-
"version": "9.0.
|
|
4
|
+
"version": "9.0.2",
|
|
5
5
|
"author": "Guillermo Rauch <guillermo@learnboost.com>",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"mongodb",
|
|
@@ -102,6 +102,7 @@
|
|
|
102
102
|
"tdd": "mocha --watch --inspect --recursive ./test/*.test.js --watch-files lib/**/*.js test/**/*.js",
|
|
103
103
|
"test-coverage": "nyc --reporter=html --reporter=text npm test",
|
|
104
104
|
"ts-benchmark": "cd ./benchmarks/typescript/simple && npm install && npm run benchmark | node ../../../scripts/tsc-diagnostics-check",
|
|
105
|
+
"ts-benchmark:local": "node ./scripts/create-tarball && cd ./benchmarks/typescript/simple && rm -rf ./node_modules && npm install && npm run benchmark | node ../../../scripts/tsc-diagnostics-check",
|
|
105
106
|
"attest-benchmark": "node ./benchmarks/typescript/infer.bench.mts"
|
|
106
107
|
},
|
|
107
108
|
"main": "./index.js",
|
package/types/index.d.ts
CHANGED
|
@@ -345,7 +345,8 @@ declare module 'mongoose' {
|
|
|
345
345
|
TSchemaOptions extends { methods: infer M } ? M : {},
|
|
346
346
|
TSchemaOptions extends { query: any } ? TSchemaOptions['query'] : {},
|
|
347
347
|
TSchemaOptions extends { virtuals: any } ? TSchemaOptions['virtuals'] : {},
|
|
348
|
-
RawDocType
|
|
348
|
+
RawDocType,
|
|
349
|
+
ResolveSchemaOptions<TSchemaOptions>
|
|
349
350
|
>
|
|
350
351
|
>(def: TSchemaDefinition): Schema<
|
|
351
352
|
RawDocType,
|
|
@@ -374,7 +375,14 @@ declare module 'mongoose' {
|
|
|
374
375
|
InferRawDocType<TSchemaDefinition, ResolveSchemaOptions<TSchemaOptions>>,
|
|
375
376
|
ResolveSchemaOptions<TSchemaOptions>
|
|
376
377
|
>,
|
|
377
|
-
THydratedDocumentType extends AnyObject = HydratedDocument<
|
|
378
|
+
THydratedDocumentType extends AnyObject = HydratedDocument<
|
|
379
|
+
InferHydratedDocType<TSchemaDefinition, ResolveSchemaOptions<TSchemaOptions>>,
|
|
380
|
+
TSchemaOptions extends { methods: infer M } ? M : {},
|
|
381
|
+
TSchemaOptions extends { query: any } ? TSchemaOptions['query'] : {},
|
|
382
|
+
TSchemaOptions extends { virtuals: any } ? TSchemaOptions['virtuals'] : {},
|
|
383
|
+
RawDocType,
|
|
384
|
+
ResolveSchemaOptions<TSchemaOptions>
|
|
385
|
+
>
|
|
378
386
|
>(def: TSchemaDefinition, options: TSchemaOptions): Schema<
|
|
379
387
|
RawDocType,
|
|
380
388
|
Model<RawDocType, any, any, any>,
|
|
@@ -623,8 +631,7 @@ declare module 'mongoose' {
|
|
|
623
631
|
|
|
624
632
|
/** Adds static "class" methods to Models compiled from this schema. */
|
|
625
633
|
static<K extends keyof TStaticMethods>(name: K, fn: TStaticMethods[K]): this;
|
|
626
|
-
static(obj: { [
|
|
627
|
-
static(obj: { [F in keyof TStaticMethods]: TStaticMethods[F] } & { [name: string]: (this: TModelType, ...args: any[]) => any }): this;
|
|
634
|
+
static(obj: Partial<TStaticMethods> & { [name: string]: (this: TModelType, ...args: any[]) => any }): this;
|
|
628
635
|
static(name: string, fn: (this: TModelType, ...args: any[]) => any): this;
|
|
629
636
|
|
|
630
637
|
/** Object of currently defined statics on this schema. */
|
|
@@ -12,18 +12,24 @@ declare module 'mongoose' {
|
|
|
12
12
|
? ObtainSchemaGeneric<TSchema, 'EnforcedDocType'>
|
|
13
13
|
: FlattenMaps<SubdocsToPOJOs<ObtainSchemaGeneric<TSchema, 'DocType'>>>;
|
|
14
14
|
|
|
15
|
-
export type
|
|
15
|
+
export type InferRawDocTypeWithout_id<
|
|
16
16
|
SchemaDefinition,
|
|
17
17
|
TSchemaOptions extends Record<any, any> = DefaultSchemaOptions,
|
|
18
18
|
TTransformOptions = { bufferToBinary: false }
|
|
19
|
-
> =
|
|
19
|
+
> = ApplySchemaOptions<{
|
|
20
20
|
[
|
|
21
21
|
K in keyof (RequiredPaths<SchemaDefinition, TSchemaOptions['typeKey']> &
|
|
22
22
|
OptionalPaths<SchemaDefinition, TSchemaOptions['typeKey']>)
|
|
23
23
|
]: IsPathRequired<SchemaDefinition[K], TSchemaOptions['typeKey']> extends true
|
|
24
24
|
? ObtainRawDocumentPathType<SchemaDefinition[K], TSchemaOptions['typeKey'], TTransformOptions>
|
|
25
25
|
: ObtainRawDocumentPathType<SchemaDefinition[K], TSchemaOptions['typeKey'], TTransformOptions> | null;
|
|
26
|
-
}, TSchemaOptions
|
|
26
|
+
}, TSchemaOptions>;
|
|
27
|
+
|
|
28
|
+
export type InferRawDocType<
|
|
29
|
+
SchemaDefinition,
|
|
30
|
+
TSchemaOptions extends Record<any, any> = DefaultSchemaOptions,
|
|
31
|
+
TTransformOptions = { bufferToBinary: false }
|
|
32
|
+
> = Require_id<InferRawDocTypeWithout_id<SchemaDefinition, TSchemaOptions, TTransformOptions>>;
|
|
27
33
|
|
|
28
34
|
/**
|
|
29
35
|
* @summary Allows users to optionally choose their own type for a schema field for stronger typing.
|
|
@@ -100,10 +100,10 @@ declare module 'mongoose' {
|
|
|
100
100
|
{
|
|
101
101
|
EnforcedDocType: EnforcedDocType;
|
|
102
102
|
M: M;
|
|
103
|
-
TInstanceMethods: TInstanceMethods
|
|
104
|
-
TQueryHelpers: TQueryHelpers
|
|
105
|
-
TVirtuals: AddDefaultId<DocType, TVirtuals, TSchemaOptions>;
|
|
106
|
-
TStaticMethods: TStaticMethods
|
|
103
|
+
TInstanceMethods: IfEquals<TInstanceMethods, {}, TSchemaOptions extends { methods: infer M } ? M : {}, TInstanceMethods>;
|
|
104
|
+
TQueryHelpers: IfEquals<TQueryHelpers, {}, TSchemaOptions extends { query: infer Q } ? Q : {}, TQueryHelpers>;
|
|
105
|
+
TVirtuals: AddDefaultId<DocType, IfEquals<TVirtuals, {}, TSchemaOptions extends { virtuals: infer V } ? V : {}, TVirtuals>, TSchemaOptions>;
|
|
106
|
+
TStaticMethods: IfEquals<TStaticMethods, {}, TSchemaOptions extends { statics: infer S } ? S : {}, TStaticMethods>;
|
|
107
107
|
TSchemaOptions: TSchemaOptions;
|
|
108
108
|
DocType: DocType;
|
|
109
109
|
THydratedDocumentType: THydratedDocumentType;
|
|
@@ -136,11 +136,6 @@ declare module 'mongoose' {
|
|
|
136
136
|
: T;
|
|
137
137
|
}
|
|
138
138
|
|
|
139
|
-
type IsPathDefaultUndefined<PathType> =
|
|
140
|
-
PathType extends { default: undefined } ? true
|
|
141
|
-
: PathType extends { default: (...args: any[]) => undefined } ? true
|
|
142
|
-
: false;
|
|
143
|
-
|
|
144
139
|
type RequiredPropertyDefinition =
|
|
145
140
|
| {
|
|
146
141
|
required: true | string | [true, string | undefined] | { isRequired: true };
|
|
@@ -159,16 +154,17 @@ type IsPathRequired<P, TypeKey extends string = DefaultTypeKey> =
|
|
|
159
154
|
P extends { required: false } ?
|
|
160
155
|
false
|
|
161
156
|
: true
|
|
162
|
-
: P extends
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
: true
|
|
166
|
-
: P extends Record<TypeKey, any> ?
|
|
167
|
-
P extends { default: any } ?
|
|
168
|
-
IfEquals<P['default'], undefined, false, true>
|
|
169
|
-
: false
|
|
157
|
+
: P extends { default: undefined | null | ((...args: any[]) => undefined) | ((...args: any[]) => null) } ? false
|
|
158
|
+
: P extends { default: any } ? true
|
|
159
|
+
: P extends Record<TypeKey, ArrayConstructor | any[]> ? true
|
|
170
160
|
: false;
|
|
171
161
|
|
|
162
|
+
// Internal type used to efficiently check for never or any types
|
|
163
|
+
// can be efficiently checked like:
|
|
164
|
+
// `[T] extends [neverOrAny] ? T : ...`
|
|
165
|
+
// to avoid edge cases
|
|
166
|
+
type neverOrAny = ' ~neverOrAny~';
|
|
167
|
+
|
|
172
168
|
/**
|
|
173
169
|
* @summary A Utility to obtain schema's required path keys.
|
|
174
170
|
* @param {T} T A generic refers to document definition.
|
|
@@ -251,6 +247,7 @@ type UnionToType<T extends readonly any[]> = T[number] extends infer U
|
|
|
251
247
|
|
|
252
248
|
type IsSchemaTypeFromBuiltinClass<T> =
|
|
253
249
|
T extends typeof String ? true
|
|
250
|
+
: unknown extends Buffer ? false
|
|
254
251
|
: T extends typeof Number ? true
|
|
255
252
|
: T extends typeof Boolean ? true
|
|
256
253
|
: T extends typeof Buffer ? true
|
|
@@ -268,7 +265,6 @@ type IsSchemaTypeFromBuiltinClass<T> =
|
|
|
268
265
|
: T extends NativeDate ? true
|
|
269
266
|
: T extends typeof Schema.Types.Mixed ? true
|
|
270
267
|
: T extends Types.UUID ? true
|
|
271
|
-
: unknown extends Buffer ? false
|
|
272
268
|
: T extends Buffer ? true
|
|
273
269
|
: false;
|
|
274
270
|
|
|
@@ -284,12 +280,10 @@ type ResolvePathType<
|
|
|
284
280
|
Options extends SchemaTypeOptions<PathValueType> = {},
|
|
285
281
|
TypeKey extends string = DefaultSchemaOptions['typeKey'],
|
|
286
282
|
TypeHint = never
|
|
287
|
-
> =
|
|
288
|
-
|
|
289
|
-
never,
|
|
290
|
-
PathValueType extends Schema ? InferSchemaType<PathValueType>
|
|
283
|
+
> = [TypeHint] extends [never]
|
|
284
|
+
? PathValueType extends Schema ? InferSchemaType<PathValueType>
|
|
291
285
|
: PathValueType extends AnyArray<infer Item> ?
|
|
292
|
-
|
|
286
|
+
[Item] extends [never]
|
|
293
287
|
? any[]
|
|
294
288
|
: Item extends Schema ?
|
|
295
289
|
// If Item is a schema, infer its type.
|
|
@@ -328,7 +322,7 @@ type ResolvePathType<
|
|
|
328
322
|
: never
|
|
329
323
|
: PathValueType extends ArrayConstructor ? any[]
|
|
330
324
|
: PathValueType extends typeof Schema.Types.Mixed ? any
|
|
331
|
-
:
|
|
325
|
+
: PathValueType extends ObjectConstructor ? any
|
|
332
326
|
: IfEquals<PathValueType, {}> extends true ? any
|
|
333
327
|
: PathValueType extends typeof SchemaType ? PathValueType['prototype']
|
|
334
328
|
: PathValueType extends Record<string, any> ?
|
|
@@ -339,6 +333,5 @@ type ResolvePathType<
|
|
|
339
333
|
typeKey: TypeKey;
|
|
340
334
|
}
|
|
341
335
|
>
|
|
342
|
-
: unknown
|
|
343
|
-
TypeHint
|
|
344
|
-
>;
|
|
336
|
+
: unknown
|
|
337
|
+
: TypeHint;
|
package/types/query.d.ts
CHANGED
|
@@ -20,7 +20,7 @@ declare module 'mongoose' {
|
|
|
20
20
|
|
|
21
21
|
export type ApplyBasicQueryCasting<T> = QueryTypeCasting<T> | QueryTypeCasting<T[]> | (T extends (infer U)[] ? QueryTypeCasting<U> : T) | null;
|
|
22
22
|
|
|
23
|
-
type _QueryFilter<T> = ({ [P in keyof T]?: mongodb.Condition<ApplyBasicQueryCasting<T[P]>>; } & mongodb.RootFilterOperators<{ [P in keyof T]?: ApplyBasicQueryCasting<T[P]>; }>);
|
|
23
|
+
type _QueryFilter<T> = ({ [P in keyof T]?: mongodb.Condition<ApplyBasicQueryCasting<T[P]>>; } & mongodb.RootFilterOperators<{ [P in keyof mongodb.WithId<T>]?: ApplyBasicQueryCasting<mongodb.WithId<T>[P]>; }>);
|
|
24
24
|
type QueryFilter<T> = IsItRecordAndNotAny<T> extends true ? _QueryFilter<WithLevel1NestedPaths<T>> : _QueryFilter<Record<string, any>>;
|
|
25
25
|
|
|
26
26
|
type MongooseBaseQueryOptionKeys =
|
package/types/utility.d.ts
CHANGED
|
@@ -146,7 +146,7 @@ declare module 'mongoose' {
|
|
|
146
146
|
* @param {T} T Function type to extract 'this' parameter from.
|
|
147
147
|
* @param {F} F Fallback type to return if 'this' parameter does not exist.
|
|
148
148
|
*/
|
|
149
|
-
type ThisParameter<T, F> = T extends { (this: infer This): void } ? This : F;
|
|
149
|
+
type ThisParameter<T, F> = T extends { (this: infer This, ...args: never): void } ? This : F;
|
|
150
150
|
|
|
151
151
|
/**
|
|
152
152
|
* @summary Decorates all functions in an object with 'this' parameter.
|
package/types/virtuals.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ declare module 'mongoose' {
|
|
|
8
8
|
type TVirtualPathFN<DocType = {}, PathType = unknown, TInstanceMethods = {}, TReturn = unknown> =
|
|
9
9
|
<T = HydratedDocument<DocType, TInstanceMethods>>(this: Document<any, any, DocType> & DocType, value: PathType, virtual: VirtualType<T>, doc: Document<any, any, DocType> & DocType) => TReturn;
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
type SchemaOptionsVirtualsPropertyType<DocType = any, VirtualPaths = Record<any, unknown>, TInstanceMethods = {}> = {
|
|
12
|
+
[K in keyof VirtualPaths]: VirtualPathFunctions<IsItRecordAndNotAny<DocType> extends true ? DocType : any, VirtualPaths[K], TInstanceMethods>
|
|
13
|
+
};
|
|
14
14
|
}
|