mongoose 7.4.2 → 7.4.4
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/connection.js +49 -32
- package/lib/cursor/ChangeStream.js +13 -2
- package/lib/helpers/query/selectPopulatedFields.js +13 -0
- package/lib/helpers/schema/getIndexes.js +9 -1
- package/lib/queryhelpers.js +1 -1
- package/lib/schema.js +8 -0
- package/lib/utils.js +14 -1
- package/package.json +1 -1
- package/types/document.d.ts +12 -0
- package/types/index.d.ts +46 -17
- package/types/inferschematype.d.ts +3 -3
- package/types/middlewares.d.ts +10 -2
- package/types/models.d.ts +111 -0
- package/types/query.d.ts +16 -2
package/lib/connection.js
CHANGED
|
@@ -517,49 +517,66 @@ Connection.prototype.startSession = async function startSession(options) {
|
|
|
517
517
|
Connection.prototype.transaction = function transaction(fn, options) {
|
|
518
518
|
return this.startSession().then(session => {
|
|
519
519
|
session[sessionNewDocuments] = new Map();
|
|
520
|
-
return session.withTransaction(() => fn
|
|
520
|
+
return session.withTransaction(() => _wrapUserTransaction(fn, session), options).
|
|
521
521
|
then(res => {
|
|
522
522
|
delete session[sessionNewDocuments];
|
|
523
523
|
return res;
|
|
524
524
|
}).
|
|
525
525
|
catch(err => {
|
|
526
|
-
// If transaction was aborted, we need to reset newly
|
|
527
|
-
// inserted documents' `isNew`.
|
|
528
|
-
for (const doc of session[sessionNewDocuments].keys()) {
|
|
529
|
-
const state = session[sessionNewDocuments].get(doc);
|
|
530
|
-
if (state.hasOwnProperty('isNew')) {
|
|
531
|
-
doc.$isNew = state.$isNew;
|
|
532
|
-
}
|
|
533
|
-
if (state.hasOwnProperty('versionKey')) {
|
|
534
|
-
doc.set(doc.schema.options.versionKey, state.versionKey);
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
if (state.modifiedPaths.length > 0 && doc.$__.activePaths.states.modify == null) {
|
|
538
|
-
doc.$__.activePaths.states.modify = {};
|
|
539
|
-
}
|
|
540
|
-
for (const path of state.modifiedPaths) {
|
|
541
|
-
doc.$__.activePaths.paths[path] = 'modify';
|
|
542
|
-
doc.$__.activePaths.states.modify[path] = true;
|
|
543
|
-
}
|
|
544
|
-
|
|
545
|
-
for (const path of state.atomics.keys()) {
|
|
546
|
-
const val = doc.$__getValue(path);
|
|
547
|
-
if (val == null) {
|
|
548
|
-
continue;
|
|
549
|
-
}
|
|
550
|
-
val[arrayAtomicsSymbol] = state.atomics.get(path);
|
|
551
|
-
}
|
|
552
|
-
}
|
|
553
526
|
delete session[sessionNewDocuments];
|
|
554
527
|
throw err;
|
|
555
|
-
})
|
|
556
|
-
|
|
557
|
-
session.endSession()
|
|
558
|
-
.catch(() => {});
|
|
528
|
+
}).
|
|
529
|
+
finally(() => {
|
|
530
|
+
session.endSession().catch(() => {});
|
|
559
531
|
});
|
|
560
532
|
});
|
|
561
533
|
};
|
|
562
534
|
|
|
535
|
+
/*!
|
|
536
|
+
* Reset document state in between transaction retries re: gh-13698
|
|
537
|
+
*/
|
|
538
|
+
|
|
539
|
+
async function _wrapUserTransaction(fn, session) {
|
|
540
|
+
try {
|
|
541
|
+
const res = await fn(session);
|
|
542
|
+
return res;
|
|
543
|
+
} catch (err) {
|
|
544
|
+
_resetSessionDocuments(session);
|
|
545
|
+
throw err;
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
/*!
|
|
550
|
+
* If transaction was aborted, we need to reset newly inserted documents' `isNew`.
|
|
551
|
+
*/
|
|
552
|
+
function _resetSessionDocuments(session) {
|
|
553
|
+
for (const doc of session[sessionNewDocuments].keys()) {
|
|
554
|
+
const state = session[sessionNewDocuments].get(doc);
|
|
555
|
+
if (state.hasOwnProperty('isNew')) {
|
|
556
|
+
doc.$isNew = state.isNew;
|
|
557
|
+
}
|
|
558
|
+
if (state.hasOwnProperty('versionKey')) {
|
|
559
|
+
doc.set(doc.schema.options.versionKey, state.versionKey);
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
if (state.modifiedPaths.length > 0 && doc.$__.activePaths.states.modify == null) {
|
|
563
|
+
doc.$__.activePaths.states.modify = {};
|
|
564
|
+
}
|
|
565
|
+
for (const path of state.modifiedPaths) {
|
|
566
|
+
doc.$__.activePaths.paths[path] = 'modify';
|
|
567
|
+
doc.$__.activePaths.states.modify[path] = true;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
for (const path of state.atomics.keys()) {
|
|
571
|
+
const val = doc.$__getValue(path);
|
|
572
|
+
if (val == null) {
|
|
573
|
+
continue;
|
|
574
|
+
}
|
|
575
|
+
val[arrayAtomicsSymbol] = state.atomics.get(path);
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
|
|
563
580
|
/**
|
|
564
581
|
* Helper for `dropCollection()`. Will delete the given collection, including
|
|
565
582
|
* all documents and indexes.
|
|
@@ -6,6 +6,12 @@
|
|
|
6
6
|
|
|
7
7
|
const EventEmitter = require('events').EventEmitter;
|
|
8
8
|
|
|
9
|
+
/*!
|
|
10
|
+
* ignore
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const driverChangeStreamEvents = ['close', 'change', 'end', 'error', 'resumeTokenChanged'];
|
|
14
|
+
|
|
9
15
|
/*!
|
|
10
16
|
* ignore
|
|
11
17
|
*/
|
|
@@ -52,7 +58,7 @@ class ChangeStream extends EventEmitter {
|
|
|
52
58
|
this.closed = true;
|
|
53
59
|
});
|
|
54
60
|
|
|
55
|
-
|
|
61
|
+
driverChangeStreamEvents.forEach(ev => {
|
|
56
62
|
this.driverChangeStream.on(ev, data => {
|
|
57
63
|
// Sometimes Node driver still polls after close, so
|
|
58
64
|
// avoid any uncaught exceptions due to closed change streams
|
|
@@ -75,7 +81,7 @@ class ChangeStream extends EventEmitter {
|
|
|
75
81
|
this.closed = true;
|
|
76
82
|
});
|
|
77
83
|
|
|
78
|
-
|
|
84
|
+
driverChangeStreamEvents.forEach(ev => {
|
|
79
85
|
this.driverChangeStream.on(ev, data => {
|
|
80
86
|
// Sometimes Node driver still polls after close, so
|
|
81
87
|
// avoid any uncaught exceptions due to closed change streams
|
|
@@ -122,6 +128,11 @@ class ChangeStream extends EventEmitter {
|
|
|
122
128
|
return this.driverChangeStream.next(cb);
|
|
123
129
|
}
|
|
124
130
|
|
|
131
|
+
addListener(event, handler) {
|
|
132
|
+
this._bindEvents();
|
|
133
|
+
return super.addListener(event, handler);
|
|
134
|
+
}
|
|
135
|
+
|
|
125
136
|
on(event, handler) {
|
|
126
137
|
this._bindEvents();
|
|
127
138
|
return super.on(event, handler);
|
|
@@ -21,12 +21,25 @@ module.exports = function selectPopulatedFields(fields, userProvidedFields, popu
|
|
|
21
21
|
} else if (userProvidedFields[path] === 0) {
|
|
22
22
|
delete fields[path];
|
|
23
23
|
}
|
|
24
|
+
|
|
25
|
+
const refPath = populateOptions[path]?.refPath;
|
|
26
|
+
if (typeof refPath === 'string') {
|
|
27
|
+
if (!isPathInFields(userProvidedFields, refPath)) {
|
|
28
|
+
fields[refPath] = 1;
|
|
29
|
+
} else if (userProvidedFields[refPath] === 0) {
|
|
30
|
+
delete fields[refPath];
|
|
31
|
+
}
|
|
32
|
+
}
|
|
24
33
|
}
|
|
25
34
|
} else if (isExclusive(fields)) {
|
|
26
35
|
for (const path of paths) {
|
|
27
36
|
if (userProvidedFields[path] == null) {
|
|
28
37
|
delete fields[path];
|
|
29
38
|
}
|
|
39
|
+
const refPath = populateOptions[path]?.refPath;
|
|
40
|
+
if (typeof refPath === 'string' && userProvidedFields[refPath] == null) {
|
|
41
|
+
delete fields[refPath];
|
|
42
|
+
}
|
|
30
43
|
}
|
|
31
44
|
}
|
|
32
45
|
};
|
|
@@ -78,7 +78,15 @@ module.exports = function getIndexes(schema) {
|
|
|
78
78
|
field[prefix + key] = 'text';
|
|
79
79
|
delete options.text;
|
|
80
80
|
} else {
|
|
81
|
-
|
|
81
|
+
let isDescendingIndex = false;
|
|
82
|
+
if (index === 'descending' || index === 'desc') {
|
|
83
|
+
isDescendingIndex = true;
|
|
84
|
+
} else if (index === 'ascending' || index === 'asc') {
|
|
85
|
+
isDescendingIndex = false;
|
|
86
|
+
} else {
|
|
87
|
+
isDescendingIndex = Number(index) === -1;
|
|
88
|
+
}
|
|
89
|
+
|
|
82
90
|
field[prefix + key] = isDescendingIndex ? -1 : 1;
|
|
83
91
|
}
|
|
84
92
|
|
package/lib/queryhelpers.js
CHANGED
|
@@ -325,7 +325,7 @@ exports.applyPaths = function applyPaths(fields, schema) {
|
|
|
325
325
|
// If set to 0, we're explicitly excluding the discriminator key. Can't do this for all fields,
|
|
326
326
|
// because we have tests that assert that using `-path` to exclude schema-level `select: true`
|
|
327
327
|
// fields counts as an exclusive projection. See gh-11546
|
|
328
|
-
if (exclude && type.selected && path === schema.options.discriminatorKey && fields[path] != null && !fields[path]) {
|
|
328
|
+
if (!exclude && type.selected && path === schema.options.discriminatorKey && fields[path] != null && !fields[path]) {
|
|
329
329
|
delete fields[path];
|
|
330
330
|
return;
|
|
331
331
|
}
|
package/lib/schema.js
CHANGED
|
@@ -2046,6 +2046,14 @@ Schema.prototype.index = function(fields, options) {
|
|
|
2046
2046
|
utils.expires(options);
|
|
2047
2047
|
}
|
|
2048
2048
|
|
|
2049
|
+
for (const field of Object.keys(fields)) {
|
|
2050
|
+
if (fields[field] === 'ascending' || fields[field] === 'asc') {
|
|
2051
|
+
fields[field] = 1;
|
|
2052
|
+
} else if (fields[field] === 'descending' || fields[field] === 'desc') {
|
|
2053
|
+
fields[field] = -1;
|
|
2054
|
+
}
|
|
2055
|
+
}
|
|
2056
|
+
|
|
2049
2057
|
this._indexes.push([fields, options]);
|
|
2050
2058
|
return this;
|
|
2051
2059
|
};
|
package/lib/utils.js
CHANGED
|
@@ -630,9 +630,22 @@ function _populateObj(obj) {
|
|
|
630
630
|
*/
|
|
631
631
|
|
|
632
632
|
exports.getValue = function(path, obj, map) {
|
|
633
|
-
return mpath.get(path, obj,
|
|
633
|
+
return mpath.get(path, obj, getValueLookup, map);
|
|
634
634
|
};
|
|
635
635
|
|
|
636
|
+
/*!
|
|
637
|
+
* ignore
|
|
638
|
+
*/
|
|
639
|
+
|
|
640
|
+
const mapGetterOptions = Object.freeze({ getters: false });
|
|
641
|
+
|
|
642
|
+
function getValueLookup(obj, part) {
|
|
643
|
+
const _from = obj?._doc || obj;
|
|
644
|
+
return _from instanceof Map ?
|
|
645
|
+
_from.get(part, mapGetterOptions) :
|
|
646
|
+
_from[part];
|
|
647
|
+
}
|
|
648
|
+
|
|
636
649
|
/**
|
|
637
650
|
* Sets the value of `obj` at the given `path`.
|
|
638
651
|
*
|
package/package.json
CHANGED
package/types/document.d.ts
CHANGED
|
@@ -135,6 +135,7 @@ declare module 'mongoose' {
|
|
|
135
135
|
errors?: Error.ValidationError;
|
|
136
136
|
|
|
137
137
|
/** Returns the value of a path. */
|
|
138
|
+
get<T extends keyof DocType>(path: T, type?: any, options?: any): DocType[T];
|
|
138
139
|
get(path: string, type?: any, options?: any): any;
|
|
139
140
|
|
|
140
141
|
/**
|
|
@@ -157,30 +158,37 @@ declare module 'mongoose' {
|
|
|
157
158
|
init(obj: AnyObject, opts?: AnyObject): this;
|
|
158
159
|
|
|
159
160
|
/** Marks a path as invalid, causing validation to fail. */
|
|
161
|
+
invalidate<T extends keyof DocType>(path: T, errorMsg: string | NativeError, value?: any, kind?: string): NativeError | null;
|
|
160
162
|
invalidate(path: string, errorMsg: string | NativeError, value?: any, kind?: string): NativeError | null;
|
|
161
163
|
|
|
162
164
|
/** Returns true if `path` was directly set and modified, else false. */
|
|
165
|
+
isDirectModified<T extends keyof DocType>(path: T | Array<T>): boolean;
|
|
163
166
|
isDirectModified(path: string | Array<string>): boolean;
|
|
164
167
|
|
|
165
168
|
/** Checks if `path` was explicitly selected. If no projection, always returns true. */
|
|
169
|
+
isDirectSelected<T extends keyof DocType>(path: T): boolean;
|
|
166
170
|
isDirectSelected(path: string): boolean;
|
|
167
171
|
|
|
168
172
|
/** Checks if `path` is in the `init` state, that is, it was set by `Document#init()` and not modified since. */
|
|
173
|
+
isInit<T extends keyof DocType>(path: T): boolean;
|
|
169
174
|
isInit(path: string): boolean;
|
|
170
175
|
|
|
171
176
|
/**
|
|
172
177
|
* Returns true if any of the given paths are modified, else false. If no arguments, returns `true` if any path
|
|
173
178
|
* in this document is modified.
|
|
174
179
|
*/
|
|
180
|
+
isModified<T extends keyof DocType>(path?: T | Array<T>): boolean;
|
|
175
181
|
isModified(path?: string | Array<string>): boolean;
|
|
176
182
|
|
|
177
183
|
/** Boolean flag specifying if the document is new. */
|
|
178
184
|
isNew: boolean;
|
|
179
185
|
|
|
180
186
|
/** Checks if `path` was selected in the source query which initialized this document. */
|
|
187
|
+
isSelected<T extends keyof DocType>(path: T): boolean;
|
|
181
188
|
isSelected(path: string): boolean;
|
|
182
189
|
|
|
183
190
|
/** Marks the path as having pending changes to write to the db. */
|
|
191
|
+
markModified<T extends keyof DocType>(path: T, scope?: any): void;
|
|
184
192
|
markModified(path: string, scope?: any): void;
|
|
185
193
|
|
|
186
194
|
/** Returns the list of paths that have been modified. */
|
|
@@ -216,6 +224,7 @@ declare module 'mongoose' {
|
|
|
216
224
|
schema: Schema;
|
|
217
225
|
|
|
218
226
|
/** Sets the value of a path, or many paths. */
|
|
227
|
+
set<T extends keyof DocType>(path: T, val: DocType[T], type: any, options?: DocumentSetOptions): this;
|
|
219
228
|
set(path: string | Record<string, any>, val: any, type: any, options?: DocumentSetOptions): this;
|
|
220
229
|
set(path: string | Record<string, any>, val: any, options?: DocumentSetOptions): this;
|
|
221
230
|
set(value: string | Record<string, any>): this;
|
|
@@ -228,17 +237,20 @@ declare module 'mongoose' {
|
|
|
228
237
|
toObject<T = Require_id<DocType>>(options?: ToObjectOptions): Require_id<T>;
|
|
229
238
|
|
|
230
239
|
/** Clears the modified state on the specified path. */
|
|
240
|
+
unmarkModified<T extends keyof DocType>(path: T): void;
|
|
231
241
|
unmarkModified(path: string): void;
|
|
232
242
|
|
|
233
243
|
/** Sends an updateOne command with this document `_id` as the query selector. */
|
|
234
244
|
updateOne(update?: UpdateQuery<this> | UpdateWithAggregationPipeline, options?: QueryOptions | null): Query<any, this>;
|
|
235
245
|
|
|
236
246
|
/** Executes registered validation rules for this document. */
|
|
247
|
+
validate<T extends keyof DocType>(pathsToValidate?: T | T[], options?: AnyObject): Promise<void>;
|
|
237
248
|
validate(pathsToValidate?: pathsToValidate, options?: AnyObject): Promise<void>;
|
|
238
249
|
validate(options: { pathsToSkip?: pathsToSkip }): Promise<void>;
|
|
239
250
|
|
|
240
251
|
/** Executes registered validation rules (skipping asynchronous validators) for this document. */
|
|
241
252
|
validateSync(options: { pathsToSkip?: pathsToSkip, [k: string]: any }): Error.ValidationError | null;
|
|
253
|
+
validateSync<T extends keyof DocType>(pathsToValidate?: T | T[], options?: AnyObject): Error.ValidationError | null;
|
|
242
254
|
validateSync(pathsToValidate?: pathsToValidate, options?: AnyObject): Error.ValidationError | null;
|
|
243
255
|
}
|
|
244
256
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -278,7 +278,7 @@ declare module 'mongoose' {
|
|
|
278
278
|
* Returns a list of indexes that this schema declares, via `schema.index()`
|
|
279
279
|
* or by `index: true` in a path's options.
|
|
280
280
|
*/
|
|
281
|
-
indexes(): Array<IndexDefinition>;
|
|
281
|
+
indexes(): Array<[IndexDefinition, IndexOptions]>;
|
|
282
282
|
|
|
283
283
|
/** Gets a schema option. */
|
|
284
284
|
get<K extends keyof SchemaOptions>(key: K): SchemaOptions[K];
|
|
@@ -406,8 +406,25 @@ declare module 'mongoose' {
|
|
|
406
406
|
pre<T extends Aggregate<any>>(method: 'aggregate' | RegExp, fn: PreMiddlewareFunction<T>): this;
|
|
407
407
|
pre<T extends Aggregate<any>>(method: 'aggregate' | RegExp, options: SchemaPreOptions, fn: PreMiddlewareFunction<T>): this;
|
|
408
408
|
/* method insertMany */
|
|
409
|
-
pre<T = TModelType>(
|
|
410
|
-
|
|
409
|
+
pre<T = TModelType>(
|
|
410
|
+
method: 'insertMany' | RegExp,
|
|
411
|
+
fn: (
|
|
412
|
+
this: T,
|
|
413
|
+
next: (err?: CallbackError) => void,
|
|
414
|
+
docs: any | Array<any>,
|
|
415
|
+
options?: InsertManyOptions & { lean?: boolean }
|
|
416
|
+
) => void | Promise<void>
|
|
417
|
+
): this;
|
|
418
|
+
pre<T = TModelType>(
|
|
419
|
+
method: 'insertMany' | RegExp,
|
|
420
|
+
options: SchemaPreOptions,
|
|
421
|
+
fn: (
|
|
422
|
+
this: T,
|
|
423
|
+
next: (err?: CallbackError) => void,
|
|
424
|
+
docs: any | Array<any>,
|
|
425
|
+
options?: InsertManyOptions & { lean?: boolean }
|
|
426
|
+
) => void | Promise<void>
|
|
427
|
+
): this;
|
|
411
428
|
|
|
412
429
|
/** Object of currently defined query helpers on this schema. */
|
|
413
430
|
query: TQueryHelpers;
|
|
@@ -578,24 +595,24 @@ declare module 'mongoose' {
|
|
|
578
595
|
|
|
579
596
|
export type SortOrder = -1 | 1 | 'asc' | 'ascending' | 'desc' | 'descending';
|
|
580
597
|
|
|
581
|
-
type _UpdateQuery<TSchema> = {
|
|
598
|
+
type _UpdateQuery<TSchema, AdditionalProperties = AnyObject> = {
|
|
582
599
|
/** @see https://www.mongodb.com/docs/manual/reference/operator/update-field/ */
|
|
583
|
-
$currentDate?: AnyKeys<TSchema> &
|
|
584
|
-
$inc?: AnyKeys<TSchema> &
|
|
585
|
-
$min?: AnyKeys<TSchema> &
|
|
586
|
-
$max?: AnyKeys<TSchema> &
|
|
587
|
-
$mul?: AnyKeys<TSchema> &
|
|
600
|
+
$currentDate?: AnyKeys<TSchema> & AdditionalProperties;
|
|
601
|
+
$inc?: AnyKeys<TSchema> & AdditionalProperties;
|
|
602
|
+
$min?: AnyKeys<TSchema> & AdditionalProperties;
|
|
603
|
+
$max?: AnyKeys<TSchema> & AdditionalProperties;
|
|
604
|
+
$mul?: AnyKeys<TSchema> & AdditionalProperties;
|
|
588
605
|
$rename?: Record<string, string>;
|
|
589
|
-
$set?: AnyKeys<TSchema> &
|
|
590
|
-
$setOnInsert?: AnyKeys<TSchema> &
|
|
591
|
-
$unset?: AnyKeys<TSchema> &
|
|
606
|
+
$set?: AnyKeys<TSchema> & AdditionalProperties;
|
|
607
|
+
$setOnInsert?: AnyKeys<TSchema> & AdditionalProperties;
|
|
608
|
+
$unset?: AnyKeys<TSchema> & AdditionalProperties;
|
|
592
609
|
|
|
593
610
|
/** @see https://www.mongodb.com/docs/manual/reference/operator/update-array/ */
|
|
594
|
-
$addToSet?: AnyKeys<TSchema> &
|
|
595
|
-
$pop?: AnyKeys<TSchema> &
|
|
596
|
-
$pull?: AnyKeys<TSchema> &
|
|
597
|
-
$push?: AnyKeys<TSchema> &
|
|
598
|
-
$pullAll?: AnyKeys<TSchema> &
|
|
611
|
+
$addToSet?: AnyKeys<TSchema> & AdditionalProperties;
|
|
612
|
+
$pop?: AnyKeys<TSchema> & AdditionalProperties;
|
|
613
|
+
$pull?: AnyKeys<TSchema> & AdditionalProperties;
|
|
614
|
+
$push?: AnyKeys<TSchema> & AdditionalProperties;
|
|
615
|
+
$pullAll?: AnyKeys<TSchema> & AdditionalProperties;
|
|
599
616
|
|
|
600
617
|
/** @see https://www.mongodb.com/docs/manual/reference/operator/update-bitwise/ */
|
|
601
618
|
$bit?: AnyKeys<TSchema>;
|
|
@@ -618,6 +635,18 @@ declare module 'mongoose' {
|
|
|
618
635
|
*/
|
|
619
636
|
export type UpdateQuery<T> = _UpdateQuery<T> & AnyObject;
|
|
620
637
|
|
|
638
|
+
/**
|
|
639
|
+
* A more strict form of UpdateQuery that enforces updating only
|
|
640
|
+
* known top-level properties.
|
|
641
|
+
* @example
|
|
642
|
+
* ```ts
|
|
643
|
+
* function updateUser(_id: mongoose.Types.ObjectId, update: UpdateQueryKnownOnly<IUser>) {
|
|
644
|
+
* return User.updateOne({ _id }, update);
|
|
645
|
+
* }
|
|
646
|
+
* ```
|
|
647
|
+
*/
|
|
648
|
+
export type UpdateQueryKnownOnly<T> = _UpdateQuery<T, {}>;
|
|
649
|
+
|
|
621
650
|
export type FlattenMaps<T> = {
|
|
622
651
|
[K in keyof T]: FlattenProperty<T[K]>;
|
|
623
652
|
};
|
|
@@ -30,7 +30,7 @@ declare module 'mongoose' {
|
|
|
30
30
|
OptionalPaths<DocDefinition, TSchemaOptions['typeKey']>)]: ObtainDocumentPathType<DocDefinition[K], TSchemaOptions['typeKey']>;
|
|
31
31
|
};
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
/**
|
|
34
34
|
* @summary Obtains document schema type from Schema instance.
|
|
35
35
|
* @param {Schema} TSchema `typeof` a schema instance.
|
|
36
36
|
* @example
|
|
@@ -39,7 +39,7 @@ declare module 'mongoose' {
|
|
|
39
39
|
* // result
|
|
40
40
|
* type UserType = {userName?: string}
|
|
41
41
|
*/
|
|
42
|
-
|
|
42
|
+
export type InferSchemaType<TSchema> = IfAny<TSchema, any, ObtainSchemaGeneric<TSchema, 'DocType'>>;
|
|
43
43
|
|
|
44
44
|
/**
|
|
45
45
|
* @summary Obtains schema Generic type by using generic alias.
|
|
@@ -86,7 +86,7 @@ type IsPathDefaultUndefined<PathType> = PathType extends { default: undefined }
|
|
|
86
86
|
* @param {TypeKey} TypeKey A generic of literal string type."Refers to the property used for path type definition".
|
|
87
87
|
*/
|
|
88
88
|
type IsPathRequired<P, TypeKey extends string = DefaultTypeKey> =
|
|
89
|
-
P extends { required: true | [true, string | undefined] } | ArrayConstructor | any[]
|
|
89
|
+
P extends { required: true | [true, string | undefined] | { isRequired: true } } | ArrayConstructor | any[]
|
|
90
90
|
? true
|
|
91
91
|
: P extends { required: boolean }
|
|
92
92
|
? P extends { required: false }
|
package/types/middlewares.d.ts
CHANGED
|
@@ -31,8 +31,16 @@ declare module 'mongoose' {
|
|
|
31
31
|
type SchemaPreOptions = MiddlewareOptions;
|
|
32
32
|
type SchemaPostOptions = MiddlewareOptions;
|
|
33
33
|
|
|
34
|
-
type PreMiddlewareFunction<ThisType = any> = (
|
|
35
|
-
|
|
34
|
+
type PreMiddlewareFunction<ThisType = any> = (
|
|
35
|
+
this: ThisType,
|
|
36
|
+
next: CallbackWithoutResultAndOptionalError,
|
|
37
|
+
opts?: Record<string, any>
|
|
38
|
+
) => void | Promise<void>;
|
|
39
|
+
type PreSaveMiddlewareFunction<ThisType = any> = (
|
|
40
|
+
this: ThisType,
|
|
41
|
+
next: CallbackWithoutResultAndOptionalError,
|
|
42
|
+
opts: SaveOptions
|
|
43
|
+
) => void | Promise<void>;
|
|
36
44
|
type PostMiddlewareFunction<ThisType = any, ResType = any> = (this: ThisType, res: ResType, next: CallbackWithoutResultAndOptionalError) => void | Promise<void>;
|
|
37
45
|
type ErrorHandlingMiddlewareFunction<ThisType = any, ResType = any> = (this: ThisType, err: NativeError, res: ResType, next: CallbackWithoutResultAndOptionalError) => void;
|
|
38
46
|
type ErrorHandlingMiddlewareWithOption<ThisType = any, ResType = any> = (this: ThisType, err: NativeError, res: ResType | null, next: CallbackWithoutResultAndOptionalError) => void | Promise<void>;
|
package/types/models.d.ts
CHANGED
|
@@ -297,6 +297,17 @@ declare module 'mongoose' {
|
|
|
297
297
|
* equivalent to `findOne({ _id: id })`. If you want to query by a document's
|
|
298
298
|
* `_id`, use `findById()` instead of `findOne()`.
|
|
299
299
|
*/
|
|
300
|
+
findById<ResultDoc = THydratedDocumentType>(
|
|
301
|
+
id: any,
|
|
302
|
+
projection: ProjectionType<TRawDocType> | null | undefined,
|
|
303
|
+
options: QueryOptions<TRawDocType> & { lean: true }
|
|
304
|
+
): QueryWithHelpers<
|
|
305
|
+
GetLeanResultType<TRawDocType, TRawDocType, 'findOne'> | null,
|
|
306
|
+
ResultDoc,
|
|
307
|
+
TQueryHelpers,
|
|
308
|
+
TRawDocType,
|
|
309
|
+
'findOne'
|
|
310
|
+
>;
|
|
300
311
|
findById<ResultDoc = THydratedDocumentType>(
|
|
301
312
|
id: any,
|
|
302
313
|
projection?: ProjectionType<TRawDocType> | null,
|
|
@@ -308,6 +319,17 @@ declare module 'mongoose' {
|
|
|
308
319
|
): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOne'>;
|
|
309
320
|
|
|
310
321
|
/** Finds one document. */
|
|
322
|
+
findOne<ResultDoc = THydratedDocumentType>(
|
|
323
|
+
filter: FilterQuery<TRawDocType>,
|
|
324
|
+
projection: ProjectionType<TRawDocType> | null | undefined,
|
|
325
|
+
options: QueryOptions<TRawDocType> & { lean: true }
|
|
326
|
+
): QueryWithHelpers<
|
|
327
|
+
GetLeanResultType<TRawDocType, TRawDocType, 'findOne'> | null,
|
|
328
|
+
ResultDoc,
|
|
329
|
+
TQueryHelpers,
|
|
330
|
+
TRawDocType,
|
|
331
|
+
'findOne'
|
|
332
|
+
>;
|
|
311
333
|
findOne<ResultDoc = THydratedDocumentType>(
|
|
312
334
|
filter?: FilterQuery<TRawDocType>,
|
|
313
335
|
projection?: ProjectionType<TRawDocType> | null,
|
|
@@ -460,6 +482,17 @@ declare module 'mongoose' {
|
|
|
460
482
|
>;
|
|
461
483
|
|
|
462
484
|
/** Creates a `find` query: gets a list of documents that match `filter`. */
|
|
485
|
+
find<ResultDoc = THydratedDocumentType>(
|
|
486
|
+
filter: FilterQuery<TRawDocType>,
|
|
487
|
+
projection: ProjectionType<TRawDocType> | null | undefined,
|
|
488
|
+
options: QueryOptions<TRawDocType> & { lean: true }
|
|
489
|
+
): QueryWithHelpers<
|
|
490
|
+
GetLeanResultType<TRawDocType, TRawDocType[], 'find'>,
|
|
491
|
+
ResultDoc,
|
|
492
|
+
TQueryHelpers,
|
|
493
|
+
TRawDocType,
|
|
494
|
+
'find'
|
|
495
|
+
>;
|
|
463
496
|
find<ResultDoc = THydratedDocumentType>(
|
|
464
497
|
filter: FilterQuery<TRawDocType>,
|
|
465
498
|
projection?: ProjectionType<TRawDocType> | null | undefined,
|
|
@@ -476,23 +509,59 @@ declare module 'mongoose' {
|
|
|
476
509
|
): QueryWithHelpers<Array<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'find'>;
|
|
477
510
|
|
|
478
511
|
/** Creates a `findByIdAndDelete` query, filtering by the given `_id`. */
|
|
512
|
+
findByIdAndDelete<ResultDoc = THydratedDocumentType>(
|
|
513
|
+
id: mongodb.ObjectId | any,
|
|
514
|
+
options: QueryOptions<TRawDocType> & { lean: true }
|
|
515
|
+
): QueryWithHelpers<
|
|
516
|
+
GetLeanResultType<TRawDocType, TRawDocType, 'findOneAndDelete'> | null,
|
|
517
|
+
ResultDoc,
|
|
518
|
+
TQueryHelpers,
|
|
519
|
+
TRawDocType,
|
|
520
|
+
'findOneAndDelete'
|
|
521
|
+
>;
|
|
479
522
|
findByIdAndDelete<ResultDoc = THydratedDocumentType>(
|
|
480
523
|
id?: mongodb.ObjectId | any,
|
|
481
524
|
options?: QueryOptions<TRawDocType> | null
|
|
482
525
|
): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndDelete'>;
|
|
483
526
|
|
|
484
527
|
/** Creates a `findByIdAndRemove` query, filtering by the given `_id`. */
|
|
528
|
+
findByIdAndRemove<ResultDoc = THydratedDocumentType>(
|
|
529
|
+
id: mongodb.ObjectId | any,
|
|
530
|
+
options: QueryOptions<TRawDocType> & { lean: true }
|
|
531
|
+
): QueryWithHelpers<
|
|
532
|
+
GetLeanResultType<TRawDocType, TRawDocType, 'findOneAndDelete'> | null,
|
|
533
|
+
ResultDoc,
|
|
534
|
+
TQueryHelpers,
|
|
535
|
+
TRawDocType,
|
|
536
|
+
'findOneAndDelete'
|
|
537
|
+
>;
|
|
485
538
|
findByIdAndRemove<ResultDoc = THydratedDocumentType>(
|
|
486
539
|
id?: mongodb.ObjectId | any,
|
|
487
540
|
options?: QueryOptions<TRawDocType> | null
|
|
488
541
|
): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndDelete'>;
|
|
489
542
|
|
|
490
543
|
/** Creates a `findOneAndUpdate` query, filtering by the given `_id`. */
|
|
544
|
+
findByIdAndUpdate<ResultDoc = THydratedDocumentType>(
|
|
545
|
+
id: mongodb.ObjectId | any,
|
|
546
|
+
update: UpdateQuery<TRawDocType>,
|
|
547
|
+
options: QueryOptions<TRawDocType> & { lean: true }
|
|
548
|
+
): QueryWithHelpers<
|
|
549
|
+
GetLeanResultType<TRawDocType, TRawDocType, 'findOneAndUpdate'> | null,
|
|
550
|
+
ResultDoc,
|
|
551
|
+
TQueryHelpers,
|
|
552
|
+
TRawDocType,
|
|
553
|
+
'findOneAndUpdate'
|
|
554
|
+
>;
|
|
491
555
|
findByIdAndUpdate<ResultDoc = THydratedDocumentType>(
|
|
492
556
|
id: mongodb.ObjectId | any,
|
|
493
557
|
update: UpdateQuery<TRawDocType>,
|
|
494
558
|
options: QueryOptions<TRawDocType> & { rawResult: true }
|
|
495
559
|
): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndUpdate'>;
|
|
560
|
+
findByIdAndUpdate<ResultDoc = THydratedDocumentType>(
|
|
561
|
+
id: mongodb.ObjectId | any,
|
|
562
|
+
update: UpdateQuery<TRawDocType>,
|
|
563
|
+
options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
|
|
564
|
+
): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndUpdate'>;
|
|
496
565
|
findByIdAndUpdate<ResultDoc = THydratedDocumentType>(
|
|
497
566
|
id: mongodb.ObjectId | any,
|
|
498
567
|
update: UpdateQuery<TRawDocType>,
|
|
@@ -509,6 +578,16 @@ declare module 'mongoose' {
|
|
|
509
578
|
): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndUpdate'>;
|
|
510
579
|
|
|
511
580
|
/** Creates a `findOneAndDelete` query: atomically finds the given document, deletes it, and returns the document as it was before deletion. */
|
|
581
|
+
findOneAndDelete<ResultDoc = THydratedDocumentType>(
|
|
582
|
+
filter: FilterQuery<TRawDocType>,
|
|
583
|
+
options: QueryOptions<TRawDocType> & { lean: true }
|
|
584
|
+
): QueryWithHelpers<
|
|
585
|
+
GetLeanResultType<TRawDocType, TRawDocType, 'findOneAndDelete'> | null,
|
|
586
|
+
ResultDoc,
|
|
587
|
+
TQueryHelpers,
|
|
588
|
+
TRawDocType,
|
|
589
|
+
'findOneAndDelete'
|
|
590
|
+
>;
|
|
512
591
|
findOneAndDelete<ResultDoc = THydratedDocumentType>(
|
|
513
592
|
filter?: FilterQuery<TRawDocType>,
|
|
514
593
|
options?: QueryOptions<TRawDocType> | null
|
|
@@ -521,11 +600,27 @@ declare module 'mongoose' {
|
|
|
521
600
|
): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndRemove'>;
|
|
522
601
|
|
|
523
602
|
/** Creates a `findOneAndReplace` query: atomically finds the given document and replaces it with `replacement`. */
|
|
603
|
+
findOneAndReplace<ResultDoc = THydratedDocumentType>(
|
|
604
|
+
filter: FilterQuery<TRawDocType>,
|
|
605
|
+
replacement: TRawDocType | AnyObject,
|
|
606
|
+
options: QueryOptions<TRawDocType> & { lean: true }
|
|
607
|
+
): QueryWithHelpers<
|
|
608
|
+
GetLeanResultType<TRawDocType, TRawDocType, 'findOneAndReplace'> | null,
|
|
609
|
+
ResultDoc,
|
|
610
|
+
TQueryHelpers,
|
|
611
|
+
TRawDocType,
|
|
612
|
+
'findOneAndReplace'
|
|
613
|
+
>;
|
|
524
614
|
findOneAndReplace<ResultDoc = THydratedDocumentType>(
|
|
525
615
|
filter: FilterQuery<TRawDocType>,
|
|
526
616
|
replacement: TRawDocType | AnyObject,
|
|
527
617
|
options: QueryOptions<TRawDocType> & { rawResult: true }
|
|
528
618
|
): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndReplace'>;
|
|
619
|
+
findOneAndReplace<ResultDoc = THydratedDocumentType>(
|
|
620
|
+
filter: FilterQuery<TRawDocType>,
|
|
621
|
+
replacement: TRawDocType | AnyObject,
|
|
622
|
+
options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
|
|
623
|
+
): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndReplace'>;
|
|
529
624
|
findOneAndReplace<ResultDoc = THydratedDocumentType>(
|
|
530
625
|
filter: FilterQuery<TRawDocType>,
|
|
531
626
|
replacement: TRawDocType | AnyObject,
|
|
@@ -538,11 +633,27 @@ declare module 'mongoose' {
|
|
|
538
633
|
): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndReplace'>;
|
|
539
634
|
|
|
540
635
|
/** Creates a `findOneAndUpdate` query: atomically find the first document that matches `filter` and apply `update`. */
|
|
636
|
+
findOneAndUpdate<ResultDoc = THydratedDocumentType>(
|
|
637
|
+
filter: FilterQuery<TRawDocType>,
|
|
638
|
+
update: UpdateQuery<TRawDocType>,
|
|
639
|
+
options: QueryOptions<TRawDocType> & { lean: true }
|
|
640
|
+
): QueryWithHelpers<
|
|
641
|
+
GetLeanResultType<TRawDocType, TRawDocType, 'findOneAndUpdate'> | null,
|
|
642
|
+
ResultDoc,
|
|
643
|
+
TQueryHelpers,
|
|
644
|
+
TRawDocType,
|
|
645
|
+
'findOneAndUpdate'
|
|
646
|
+
>;
|
|
541
647
|
findOneAndUpdate<ResultDoc = THydratedDocumentType>(
|
|
542
648
|
filter: FilterQuery<TRawDocType>,
|
|
543
649
|
update: UpdateQuery<TRawDocType>,
|
|
544
650
|
options: QueryOptions<TRawDocType> & { rawResult: true }
|
|
545
651
|
): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndUpdate'>;
|
|
652
|
+
findOneAndUpdate<ResultDoc = THydratedDocumentType>(
|
|
653
|
+
filter: FilterQuery<TRawDocType>,
|
|
654
|
+
update: UpdateQuery<TRawDocType>,
|
|
655
|
+
options: QueryOptions<TRawDocType> & { includeResultMetadata: true }
|
|
656
|
+
): QueryWithHelpers<ModifyResult<ResultDoc>, ResultDoc, TQueryHelpers, TRawDocType, 'findOneAndUpdate'>;
|
|
546
657
|
findOneAndUpdate<ResultDoc = THydratedDocumentType>(
|
|
547
658
|
filter: FilterQuery<TRawDocType>,
|
|
548
659
|
update: UpdateQuery<TRawDocType>,
|