mongoose 6.4.7 → 6.5.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/connection.js +23 -3
- package/lib/cursor/ChangeStream.js +39 -1
- package/lib/document.js +129 -154
- package/lib/error/index.js +1 -0
- package/lib/error/validation.js +9 -0
- package/lib/helpers/document/applyDefaults.js +115 -0
- package/lib/helpers/document/cleanModifiedSubpaths.js +3 -3
- package/lib/helpers/document/compile.js +7 -6
- package/lib/helpers/firstKey.js +8 -0
- package/lib/helpers/model/applyDefaultsToPOJO.js +52 -0
- package/lib/helpers/model/castBulkWrite.js +1 -1
- package/lib/helpers/model/pushNestedArrayPaths.js +15 -0
- package/lib/helpers/populate/markArraySubdocsPopulated.js +1 -0
- package/lib/helpers/projection/hasIncludedChildren.js +1 -0
- package/lib/helpers/promiseOrCallback.js +24 -15
- package/lib/helpers/update/applyTimestampsToChildren.js +6 -2
- package/lib/index.js +2 -1
- package/lib/internal.js +3 -1
- package/lib/model.js +237 -95
- package/lib/options/SchemaArrayOptions.js +19 -0
- package/lib/options/SchemaNumberOptions.js +2 -0
- package/lib/options/SchemaObjectIdOptions.js +1 -0
- package/lib/plugins/trackTransaction.js +2 -2
- package/lib/query.js +30 -11
- package/lib/schema/SubdocumentPath.js +10 -0
- package/lib/schema/array.js +2 -1
- package/lib/schema/documentarray.js +14 -1
- package/lib/schema/string.js +3 -0
- package/lib/schema.js +22 -3
- package/lib/schematype.js +5 -1
- package/lib/statemachine.js +23 -9
- package/lib/types/buffer.js +23 -21
- package/lib/types/map.js +2 -0
- package/lib/utils.js +8 -0
- package/lib/validoptions.js +1 -0
- package/package.json +14 -14
- package/{build-browser.js → scripts/build-browser.js} +1 -1
- package/types/connection.d.ts +7 -1
- package/types/document.d.ts +8 -1
- package/types/expressions.d.ts +1 -1
- package/types/index.d.ts +31 -28
- package/types/inferschematype.d.ts +3 -20
- package/types/models.d.ts +53 -49
- package/types/mongooseoptions.d.ts +6 -0
- package/types/schemaoptions.d.ts +15 -4
- package/types/utility.d.ts +19 -0
- package/types/virtuals.d.ts +14 -0
package/types/connection.d.ts
CHANGED
|
@@ -142,9 +142,15 @@ declare module 'mongoose' {
|
|
|
142
142
|
readonly models: Readonly<{ [index: string]: Model<any> }>;
|
|
143
143
|
|
|
144
144
|
/** Defines or retrieves a model. */
|
|
145
|
+
model<TSchema extends Schema = any>(
|
|
146
|
+
name: string,
|
|
147
|
+
schema?: TSchema,
|
|
148
|
+
collection?: string,
|
|
149
|
+
options?: CompileModelOptions
|
|
150
|
+
): Model<InferSchemaType<TSchema>, ObtainSchemaGeneric<TSchema, 'TQueryHelpers'>, ObtainSchemaGeneric<TSchema, 'TInstanceMethods'>, {}, TSchema> & ObtainSchemaGeneric<TSchema, 'TStaticMethods'>;
|
|
145
151
|
model<T, U, TQueryHelpers = {}>(
|
|
146
152
|
name: string,
|
|
147
|
-
schema?: Schema<T, U, TQueryHelpers>,
|
|
153
|
+
schema?: Schema<T, U, any, TQueryHelpers, any, any>,
|
|
148
154
|
collection?: string,
|
|
149
155
|
options?: CompileModelOptions
|
|
150
156
|
): U;
|
package/types/document.d.ts
CHANGED
|
@@ -26,7 +26,7 @@ declare module 'mongoose' {
|
|
|
26
26
|
__v?: any;
|
|
27
27
|
|
|
28
28
|
/** Assert that a given path or paths is populated. Throws an error if not populated. */
|
|
29
|
-
$assertPopulated<Paths = {}>(paths: string | string[]): Omit<
|
|
29
|
+
$assertPopulated<Paths = {}>(paths: string | string[]): Omit<DocType, keyof Paths> & Paths;
|
|
30
30
|
|
|
31
31
|
/* Get all subdocs (by bfs) */
|
|
32
32
|
$getAllSubdocs(): Document[];
|
|
@@ -43,6 +43,13 @@ declare module 'mongoose' {
|
|
|
43
43
|
/** Returns an array of all populated documents associated with the query */
|
|
44
44
|
$getPopulatedDocs(): Document[];
|
|
45
45
|
|
|
46
|
+
/**
|
|
47
|
+
* Increments the numeric value at `path` by the given `val`.
|
|
48
|
+
* When you call `save()` on this document, Mongoose will send a
|
|
49
|
+
* `$inc` as opposed to a `$set`.
|
|
50
|
+
*/
|
|
51
|
+
$inc(path: string | string[], val?: number): this;
|
|
52
|
+
|
|
46
53
|
/**
|
|
47
54
|
* Returns true if the given path is nullish or only contains empty objects.
|
|
48
55
|
* Useful for determining whether this subdoc will get stripped out by the
|
package/types/expressions.d.ts
CHANGED
|
@@ -1977,7 +1977,7 @@ declare module 'mongoose' {
|
|
|
1977
1977
|
* @version 5.0
|
|
1978
1978
|
* @see https://docs.mongodb.com/manual/reference/operator/aggregation/count/#mongodb-expression-exp.-count
|
|
1979
1979
|
*/
|
|
1980
|
-
$count: Record<string | number | symbol, never
|
|
1980
|
+
$count: Record<string | number | symbol, never> | Path;
|
|
1981
1981
|
}
|
|
1982
1982
|
|
|
1983
1983
|
export interface CovariancePop {
|
package/types/index.d.ts
CHANGED
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
/// <reference path="./utility.d.ts" />
|
|
22
22
|
/// <reference path="./validation.d.ts" />
|
|
23
23
|
/// <reference path="./inferschematype.d.ts" />
|
|
24
|
+
/// <reference path="./virtuals.d.ts" />
|
|
24
25
|
|
|
25
26
|
declare class NativeDate extends global.Date { }
|
|
26
27
|
|
|
@@ -74,7 +75,7 @@ declare module 'mongoose' {
|
|
|
74
75
|
|
|
75
76
|
export function model<T, U, TQueryHelpers = {}>(
|
|
76
77
|
name: string,
|
|
77
|
-
schema?: Schema<T, any, TQueryHelpers>,
|
|
78
|
+
schema?: Schema<T, any, any, TQueryHelpers, any, any>,
|
|
78
79
|
collection?: string,
|
|
79
80
|
options?: CompileModelOptions
|
|
80
81
|
): U;
|
|
@@ -156,7 +157,20 @@ declare module 'mongoose' {
|
|
|
156
157
|
|
|
157
158
|
type QueryResultType<T> = T extends Query<infer ResultType, any> ? ResultType : never;
|
|
158
159
|
|
|
159
|
-
|
|
160
|
+
type PluginFunction<
|
|
161
|
+
DocType,
|
|
162
|
+
M = Model<DocType, any, any, any>,
|
|
163
|
+
TInstanceMethods = {},
|
|
164
|
+
TQueryHelpers = {},
|
|
165
|
+
TVirtuals = {},
|
|
166
|
+
TStaticMethods = {}> = (schema: Schema<DocType, M, TInstanceMethods, TQueryHelpers, TVirtuals, TStaticMethods>, opts?: any) => void;
|
|
167
|
+
|
|
168
|
+
export class Schema<
|
|
169
|
+
EnforcedDocType = any,
|
|
170
|
+
M = Model<EnforcedDocType, any, any, any>,
|
|
171
|
+
TInstanceMethods = {},
|
|
172
|
+
TQueryHelpers = {},
|
|
173
|
+
TVirtuals = {},
|
|
160
174
|
TStaticMethods = {},
|
|
161
175
|
TPathTypeKey extends TypeKeyBaseType = DefaultTypeKey,
|
|
162
176
|
DocType extends ObtainDocumentType<DocType, EnforcedDocType, TPathTypeKey> = ObtainDocumentType<any, EnforcedDocType, TPathTypeKey>>
|
|
@@ -164,7 +178,7 @@ declare module 'mongoose' {
|
|
|
164
178
|
/**
|
|
165
179
|
* Create a new schema
|
|
166
180
|
*/
|
|
167
|
-
constructor(definition?: SchemaDefinition<SchemaDefinitionType<EnforcedDocType>> | DocType, options?: SchemaOptions<TPathTypeKey, FlatRecord<DocType>, TInstanceMethods, TQueryHelpers, TStaticMethods>);
|
|
181
|
+
constructor(definition?: SchemaDefinition<SchemaDefinitionType<EnforcedDocType>> | DocType, options?: SchemaOptions<TPathTypeKey, FlatRecord<DocType>, TInstanceMethods, TQueryHelpers, TStaticMethods, TVirtuals>);
|
|
168
182
|
|
|
169
183
|
/** Adds key path / schema type pairs to this schema. */
|
|
170
184
|
add(obj: SchemaDefinition<SchemaDefinitionType<EnforcedDocType>> | Schema, prefix?: string): this;
|
|
@@ -238,7 +252,7 @@ declare module 'mongoose' {
|
|
|
238
252
|
pathType(path: string): string;
|
|
239
253
|
|
|
240
254
|
/** Registers a plugin for this schema. */
|
|
241
|
-
plugin
|
|
255
|
+
plugin<PFunc extends PluginFunction<DocType, M, TInstanceMethods, TQueryHelpers, TVirtuals, TStaticMethods>, POptions extends Parameters<PFunc>[1] = Parameters<PFunc>[1]>(fn: PFunc, opts?: POptions): this;
|
|
242
256
|
|
|
243
257
|
/** Defines a post hook for the model. */
|
|
244
258
|
post<T = HydratedDocument<DocType, TInstanceMethods>>(method: MongooseDocumentMiddleware | MongooseDocumentMiddleware[] | RegExp, fn: PostMiddlewareFunction<T, T>): this;
|
|
@@ -291,15 +305,16 @@ declare module 'mongoose' {
|
|
|
291
305
|
set<K extends keyof SchemaOptions>(key: K, value: SchemaOptions[K], _tags?: any): this;
|
|
292
306
|
|
|
293
307
|
/** Adds static "class" methods to Models compiled from this schema. */
|
|
308
|
+
static<K extends keyof TStaticMethods>(name: K, fn: TStaticMethods[K]): this;
|
|
309
|
+
static(obj: { [F in keyof TStaticMethods]: TStaticMethods[F] } & { [name: string]: (this: M, ...args: any[]) => any }): this;
|
|
294
310
|
static(name: string, fn: (this: M, ...args: any[]) => any): this;
|
|
295
|
-
static(obj: { [name: string]: (this: M, ...args: any[]) => any }): this;
|
|
296
311
|
|
|
297
312
|
/** Object of currently defined statics on this schema. */
|
|
298
|
-
statics: { [name: string]: (this: M, ...args: any[]) => any };
|
|
313
|
+
statics: { [F in keyof TStaticMethods]: TStaticMethods[F] } & { [name: string]: (this: M, ...args: any[]) => any };
|
|
299
314
|
|
|
300
315
|
/** Creates a virtual type with the given name. */
|
|
301
316
|
virtual<T = HydratedDocument<DocType, TInstanceMethods>>(
|
|
302
|
-
name: string,
|
|
317
|
+
name: keyof TVirtuals | string,
|
|
303
318
|
options?: VirtualTypeOptions<T, DocType>
|
|
304
319
|
): VirtualType<T>;
|
|
305
320
|
|
|
@@ -456,9 +471,9 @@ declare module 'mongoose' {
|
|
|
456
471
|
/** @see https://docs.mongodb.com/manual/reference/operator/update-array/ */
|
|
457
472
|
$addToSet?: Mutable<mongodb.SetFields<TSchema>>;
|
|
458
473
|
$pop?: AnyKeys<TSchema> & AnyObject;
|
|
459
|
-
$pull?:
|
|
460
|
-
$push?:
|
|
461
|
-
$pullAll?:
|
|
474
|
+
$pull?: AnyKeys<TSchema> & AnyObject;
|
|
475
|
+
$push?: AnyKeys<TSchema> & AnyObject;
|
|
476
|
+
$pullAll?: AnyKeys<TSchema> & AnyObject;
|
|
462
477
|
|
|
463
478
|
/** @see https://docs.mongodb.com/manual/reference/operator/update-bitwise/ */
|
|
464
479
|
$bit?: Record<string, mongodb.NumericType>;
|
|
@@ -506,24 +521,14 @@ declare module 'mongoose' {
|
|
|
506
521
|
export type actualPrimitives = string | boolean | number | bigint | symbol | null | undefined;
|
|
507
522
|
export type TreatAsPrimitives = actualPrimitives | NativeDate | RegExp | symbol | Error | BigInt | Types.ObjectId;
|
|
508
523
|
|
|
509
|
-
// This will -- when possible -- extract the original type of the subdocument in question
|
|
510
|
-
type LeanSubdocument<T> = T extends (Types.Subdocument<Require_id<T>['_id']> & infer U) ? LeanDocument<U> : Omit<LeanDocument<T>, '$isSingleNested' | 'ownerDocument' | 'parent'>;
|
|
511
|
-
|
|
512
524
|
export type LeanType<T> =
|
|
513
525
|
0 extends (1 & T) ? T : // any
|
|
514
526
|
T extends TreatAsPrimitives ? T : // primitives
|
|
515
|
-
T extends Types.Subdocument ?
|
|
527
|
+
T extends Types.Subdocument ? Omit<LeanDocument<T>, '$isSingleNested' | 'ownerDocument' | 'parent'> :
|
|
516
528
|
LeanDocument<T>; // Documents and everything else
|
|
517
529
|
|
|
518
|
-
// Used only when collapsing lean arrays for ts performance reasons:
|
|
519
|
-
type LeanTypeOrArray<T> = T extends unknown[] ? LeanArray<T> : LeanType<T>;
|
|
520
530
|
|
|
521
|
-
export type LeanArray<T extends unknown[]> =
|
|
522
|
-
// By checking if it extends Types.Array we can get the original base type before collapsing down,
|
|
523
|
-
// rather than trying to manually remove the old types. This matches both Array and DocumentArray
|
|
524
|
-
T extends Types.Array<infer U> ? LeanTypeOrArray<U>[] :
|
|
525
|
-
// If it isn't a custom mongoose type we fall back to "do our best"
|
|
526
|
-
T extends unknown[][] ? LeanArray<T[number]>[] : LeanType<T[number]>[];
|
|
531
|
+
export type LeanArray<T extends unknown[]> = T extends unknown[][] ? LeanArray<T[number]>[] : LeanType<T[number]>[];
|
|
527
532
|
|
|
528
533
|
export type _LeanDocument<T> = {
|
|
529
534
|
[K in keyof T]: LeanDocumentElement<T[K]>;
|
|
@@ -533,10 +538,9 @@ declare module 'mongoose' {
|
|
|
533
538
|
// This way, the conditional type is distributive over union types.
|
|
534
539
|
// This is required for PopulatedDoc.
|
|
535
540
|
export type LeanDocumentElement<T> =
|
|
536
|
-
|
|
537
|
-
T extends
|
|
538
|
-
T
|
|
539
|
-
T;
|
|
541
|
+
T extends unknown[] ? LeanArray<T> : // Array
|
|
542
|
+
T extends Document ? LeanDocument<T> : // Subdocument
|
|
543
|
+
T;
|
|
540
544
|
|
|
541
545
|
export type SchemaDefinitionType<T> = T extends Document ? Omit<T, Exclude<keyof Document, '_id' | 'id' | '__v'>> : T;
|
|
542
546
|
|
|
@@ -571,8 +575,7 @@ declare module 'mongoose' {
|
|
|
571
575
|
* Plain old JavaScript object documents (POJO).
|
|
572
576
|
* @see https://mongoosejs.com/docs/tutorials/lean.html
|
|
573
577
|
*/
|
|
574
|
-
export type LeanDocument<T> =
|
|
575
|
-
Omit<_LeanDocument<T>, Exclude<keyof Document, '_id' | 'id' | '__v'> | '$isSingleNested'>;
|
|
578
|
+
export type LeanDocument<T> = Omit<_LeanDocument<T>, Exclude<keyof Document, '_id' | 'id' | '__v'> | '$isSingleNested'>;
|
|
576
579
|
|
|
577
580
|
export type LeanDocumentOrArray<T> = 0 extends (1 & T) ? T :
|
|
578
581
|
T extends unknown[] ? LeanDocument<T[number]>[] :
|
|
@@ -11,7 +11,8 @@ import {
|
|
|
11
11
|
DateSchemaDefinition,
|
|
12
12
|
ObtainDocumentType,
|
|
13
13
|
DefaultTypeKey,
|
|
14
|
-
ObjectIdSchemaDefinition
|
|
14
|
+
ObjectIdSchemaDefinition,
|
|
15
|
+
IfEquals
|
|
15
16
|
} from 'mongoose';
|
|
16
17
|
|
|
17
18
|
declare module 'mongoose' {
|
|
@@ -37,7 +38,7 @@ declare module 'mongoose' {
|
|
|
37
38
|
* // result
|
|
38
39
|
* type UserType = {userName?: string}
|
|
39
40
|
*/
|
|
40
|
-
type InferSchemaType<SchemaType> = ObtainSchemaGeneric<SchemaType, 'DocType'
|
|
41
|
+
type InferSchemaType<SchemaType> = ObtainSchemaGeneric<SchemaType, 'DocType'>;
|
|
41
42
|
|
|
42
43
|
/**
|
|
43
44
|
* @summary Obtains schema Generic type by using generic alias.
|
|
@@ -58,24 +59,6 @@ declare module 'mongoose' {
|
|
|
58
59
|
}[alias]
|
|
59
60
|
: unknown;
|
|
60
61
|
}
|
|
61
|
-
/**
|
|
62
|
-
* @summary Checks if a type is "Record" or "any".
|
|
63
|
-
* @description It Helps to check if user has provided schema type "EnforcedDocType"
|
|
64
|
-
* @param {T} T A generic type to be checked.
|
|
65
|
-
* @returns true if {@link T} is Record OR false if {@link T} is of any type.
|
|
66
|
-
*/
|
|
67
|
-
type IsItRecordAndNotAny<T> = IfEquals<T, any, false, T extends Record<any, any> ? true : false>;
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* @summary Checks if two types are identical.
|
|
71
|
-
* @param {T} T The first type to be compared with {@link U}.
|
|
72
|
-
* @param {U} U The seconde type to be compared with {@link T}.
|
|
73
|
-
* @param {Y} Y A type to be returned if {@link T} & {@link U} are identical.
|
|
74
|
-
* @param {N} N A type to be returned if {@link T} & {@link U} are not identical.
|
|
75
|
-
*/
|
|
76
|
-
type IfEquals<T, U, Y = true, N = false> =
|
|
77
|
-
(<G>() => G extends T ? 1 : 0) extends
|
|
78
|
-
(<G>() => G extends U ? 1 : 0) ? Y : N;
|
|
79
62
|
|
|
80
63
|
/**
|
|
81
64
|
* @summary Checks if a document path is required or optional.
|
package/types/models.d.ts
CHANGED
|
@@ -119,7 +119,8 @@ declare module 'mongoose' {
|
|
|
119
119
|
AcceptsDiscriminator,
|
|
120
120
|
IndexManager,
|
|
121
121
|
SessionStarter {
|
|
122
|
-
new <DocType = T>(doc?: DocType, fields?: any | null, options?: boolean | AnyObject): HydratedDocument<T, TMethodsAndOverrides,
|
|
122
|
+
new <DocType = T>(doc?: DocType, fields?: any | null, options?: boolean | AnyObject): HydratedDocument<T, TMethodsAndOverrides,
|
|
123
|
+
IfEquals<TVirtuals, {}, ObtainSchemaGeneric<TSchema, 'TVirtuals'>, TVirtuals>> & ObtainSchemaGeneric<TSchema, 'TStaticMethods'>;
|
|
123
124
|
|
|
124
125
|
aggregate<R = any>(pipeline?: PipelineStage[], options?: mongodb.AggregateOptions, callback?: Callback<R[]>): Aggregate<Array<R>>;
|
|
125
126
|
aggregate<R = any>(pipeline: PipelineStage[], callback?: Callback<R[]>): Aggregate<Array<R>>;
|
|
@@ -128,28 +129,31 @@ declare module 'mongoose' {
|
|
|
128
129
|
base: Mongoose;
|
|
129
130
|
|
|
130
131
|
/**
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
132
|
+
* If this is a discriminator model, `baseModelName` is the name of
|
|
133
|
+
* the base model.
|
|
134
|
+
*/
|
|
134
135
|
baseModelName: string | undefined;
|
|
135
136
|
|
|
137
|
+
/* Cast the given POJO to the model's schema */
|
|
138
|
+
castObject(obj: AnyObject): T;
|
|
139
|
+
|
|
136
140
|
/**
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
bulkWrite(writes: Array<mongodb.AnyBulkWriteOperation
|
|
144
|
-
bulkWrite(writes: Array<mongodb.AnyBulkWriteOperation
|
|
145
|
-
bulkWrite(writes: Array<mongodb.AnyBulkWriteOperation
|
|
141
|
+
* Sends multiple `insertOne`, `updateOne`, `updateMany`, `replaceOne`,
|
|
142
|
+
* `deleteOne`, and/or `deleteMany` operations to the MongoDB server in one
|
|
143
|
+
* command. This is faster than sending multiple independent operations (e.g.
|
|
144
|
+
* if you use `create()`) because with `bulkWrite()` there is only one network
|
|
145
|
+
* round trip to the MongoDB server.
|
|
146
|
+
*/
|
|
147
|
+
bulkWrite(writes: Array<mongodb.AnyBulkWriteOperation<T extends {} ? T : any>>, options: mongodb.BulkWriteOptions & MongooseBulkWriteOptions, callback: Callback<mongodb.BulkWriteResult>): void;
|
|
148
|
+
bulkWrite(writes: Array<mongodb.AnyBulkWriteOperation<T extends {} ? T : any>>, callback: Callback<mongodb.BulkWriteResult>): void;
|
|
149
|
+
bulkWrite(writes: Array<mongodb.AnyBulkWriteOperation<T extends {} ? T : any>>, options?: mongodb.BulkWriteOptions & MongooseBulkWriteOptions): Promise<mongodb.BulkWriteResult>;
|
|
146
150
|
|
|
147
151
|
/**
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
bulkSave(documents: Array<Document>, options?: mongodb.BulkWriteOptions): Promise<mongodb.BulkWriteResult>;
|
|
152
|
+
* Sends multiple `save()` calls in a single `bulkWrite()`. This is faster than
|
|
153
|
+
* sending multiple `save()` calls because with `bulkSave()` there is only one
|
|
154
|
+
* network round trip to the MongoDB server.
|
|
155
|
+
*/
|
|
156
|
+
bulkSave(documents: Array<Document>, options?: mongodb.BulkWriteOptions & { timestamps?: boolean }): Promise<mongodb.BulkWriteResult>;
|
|
153
157
|
|
|
154
158
|
/** Collection the model uses. */
|
|
155
159
|
collection: Collection;
|
|
@@ -170,10 +174,10 @@ declare module 'mongoose' {
|
|
|
170
174
|
create<DocContents = AnyKeys<T>>(doc: T | DocContents, callback: Callback<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>): void;
|
|
171
175
|
|
|
172
176
|
/**
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
+
* Create the collection for this model. By default, if no indexes are specified,
|
|
178
|
+
* mongoose will not create the collection for the model until any documents are
|
|
179
|
+
* created. Use this method to create the collection explicitly.
|
|
180
|
+
*/
|
|
177
181
|
createCollection<T extends mongodb.Document>(options: mongodb.CreateCollectionOptions & Pick<SchemaOptions, 'expires'> | null, callback: Callback<mongodb.Collection<T>>): void;
|
|
178
182
|
createCollection<T extends mongodb.Document>(callback: Callback<mongodb.Collection<T>>): void;
|
|
179
183
|
createCollection<T extends mongodb.Document>(options?: mongodb.CreateCollectionOptions & Pick<SchemaOptions, 'expires'>): Promise<mongodb.Collection<T>>;
|
|
@@ -182,34 +186,34 @@ declare module 'mongoose' {
|
|
|
182
186
|
db: Connection;
|
|
183
187
|
|
|
184
188
|
/**
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
+
* Deletes all of the documents that match `conditions` from the collection.
|
|
190
|
+
* Behaves like `remove()`, but deletes all documents that match `conditions`
|
|
191
|
+
* regardless of the `single` option.
|
|
192
|
+
*/
|
|
189
193
|
deleteMany(filter?: FilterQuery<T>, options?: QueryOptions<T>, callback?: CallbackWithoutResult): QueryWithHelpers<mongodb.DeleteResult, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
|
|
190
194
|
deleteMany(filter: FilterQuery<T>, callback: CallbackWithoutResult): QueryWithHelpers<mongodb.DeleteResult, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
|
|
191
195
|
deleteMany(callback: CallbackWithoutResult): QueryWithHelpers<mongodb.DeleteResult, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
|
|
192
196
|
|
|
193
197
|
/**
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
+
* Deletes the first document that matches `conditions` from the collection.
|
|
199
|
+
* Behaves like `remove()`, but deletes at most one document regardless of the
|
|
200
|
+
* `single` option.
|
|
201
|
+
*/
|
|
198
202
|
deleteOne(filter?: FilterQuery<T>, options?: QueryOptions<T>, callback?: CallbackWithoutResult): QueryWithHelpers<mongodb.DeleteResult, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
|
|
199
203
|
deleteOne(filter: FilterQuery<T>, callback: CallbackWithoutResult): QueryWithHelpers<mongodb.DeleteResult, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
|
|
200
204
|
deleteOne(callback: CallbackWithoutResult): QueryWithHelpers<mongodb.DeleteResult, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
|
|
201
205
|
|
|
202
206
|
/**
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
207
|
+
* Event emitter that reports any errors that occurred. Useful for global error
|
|
208
|
+
* handling.
|
|
209
|
+
*/
|
|
206
210
|
events: NodeJS.EventEmitter;
|
|
207
211
|
|
|
208
212
|
/**
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
+
* Finds a single document by its _id field. `findById(id)` is almost*
|
|
214
|
+
* equivalent to `findOne({ _id: id })`. If you want to query by a document's
|
|
215
|
+
* `_id`, use `findById()` instead of `findOne()`.
|
|
216
|
+
*/
|
|
213
217
|
findById<ResultDoc = HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>(
|
|
214
218
|
id: any,
|
|
215
219
|
projection?: ProjectionType<T> | null,
|
|
@@ -240,19 +244,19 @@ declare module 'mongoose' {
|
|
|
240
244
|
): QueryWithHelpers<ResultDoc | null, ResultDoc, TQueryHelpers, T>;
|
|
241
245
|
|
|
242
246
|
/**
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
hydrate(obj: any): HydratedDocument<T, TMethodsAndOverrides, TVirtuals>;
|
|
247
|
+
* Shortcut for creating a new Document from existing raw data, pre-saved in the DB.
|
|
248
|
+
* The document returned has no paths marked as modified initially.
|
|
249
|
+
*/
|
|
250
|
+
hydrate(obj: any, projection?: AnyObject, options?: { setters?: boolean }): HydratedDocument<T, TMethodsAndOverrides, TVirtuals>;
|
|
247
251
|
|
|
248
252
|
/**
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
253
|
+
* This function is responsible for building [indexes](https://docs.mongodb.com/manual/indexes/),
|
|
254
|
+
* unless [`autoIndex`](http://mongoosejs.com/docs/guide.html#autoIndex) is turned off.
|
|
255
|
+
* Mongoose calls this function automatically when a model is created using
|
|
256
|
+
* [`mongoose.model()`](/docs/api.html#mongoose_Mongoose-model) or
|
|
257
|
+
* [`connection.model()`](/docs/api.html#connection_Connection-model), so you
|
|
258
|
+
* don't need to call it.
|
|
259
|
+
*/
|
|
256
260
|
init(callback?: CallbackWithoutResult): Promise<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>;
|
|
257
261
|
|
|
258
262
|
/** Inserts one or more new documents as a single `insertMany` call to the MongoDB server. */
|
|
@@ -288,7 +292,7 @@ declare module 'mongoose' {
|
|
|
288
292
|
validate(optional: any, pathsToValidate: PathsToValidate, callback?: CallbackWithoutResult): Promise<void>;
|
|
289
293
|
|
|
290
294
|
/** Watches the underlying collection for changes using [MongoDB change streams](https://docs.mongodb.com/manual/changeStreams/). */
|
|
291
|
-
watch<ResultType extends mongodb.Document = any>(pipeline?: Array<Record<string, unknown>>, options?: mongodb.ChangeStreamOptions): mongodb.ChangeStream<ResultType>;
|
|
295
|
+
watch<ResultType extends mongodb.Document = any>(pipeline?: Array<Record<string, unknown>>, options?: mongodb.ChangeStreamOptions & { hydrate?: boolean }): mongodb.ChangeStream<ResultType>;
|
|
292
296
|
|
|
293
297
|
/** Adds a `$where` clause to this query */
|
|
294
298
|
$where(argument: string | Function): QueryWithHelpers<Array<HydratedDocument<T, TMethodsAndOverrides, TVirtuals>>, HydratedDocument<T, TMethodsAndOverrides, TVirtuals>, TQueryHelpers, T>;
|
|
@@ -79,6 +79,12 @@ declare module 'mongoose' {
|
|
|
79
79
|
| stream.Writable
|
|
80
80
|
| ((collectionName: string, methodName: string, ...methodArgs: any[]) => void);
|
|
81
81
|
|
|
82
|
+
/**
|
|
83
|
+
* If `true`, adds a `id` virtual to all schemas unless overwritten on a per-schema basis.
|
|
84
|
+
* @defaultValue true
|
|
85
|
+
*/
|
|
86
|
+
id?: boolean;
|
|
87
|
+
|
|
82
88
|
/**
|
|
83
89
|
* If `false`, it will change the `createdAt` field to be [`immutable: false`](https://mongoosejs.com/docs/api/schematype.html#schematype_SchemaType-immutable)
|
|
84
90
|
* which means you can update the `createdAt`.
|
package/types/schemaoptions.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ declare module 'mongoose' {
|
|
|
10
10
|
type TypeKeyBaseType = string;
|
|
11
11
|
|
|
12
12
|
type DefaultTypeKey = 'type';
|
|
13
|
-
interface SchemaOptions<PathTypeKey extends TypeKeyBaseType = DefaultTypeKey, DocType = unknown,
|
|
13
|
+
interface SchemaOptions<PathTypeKey extends TypeKeyBaseType = DefaultTypeKey, DocType = unknown, TInstanceMethods = {}, QueryHelpers = {}, TStaticMethods = {}, TVirtuals = {}> {
|
|
14
14
|
/**
|
|
15
15
|
* By default, Mongoose's init() function creates all the indexes defined in your model's schema by
|
|
16
16
|
* calling Model.createIndexes() after you successfully connect to MongoDB. If you want to disable
|
|
@@ -191,16 +191,27 @@ declare module 'mongoose' {
|
|
|
191
191
|
/**
|
|
192
192
|
* Model Statics methods.
|
|
193
193
|
*/
|
|
194
|
-
statics?: Record<any, (this: Model<DocType>, ...args: any) => unknown> |
|
|
194
|
+
statics?: Record<any, (this: Model<DocType>, ...args: any) => unknown> | TStaticMethods,
|
|
195
195
|
|
|
196
196
|
/**
|
|
197
197
|
* Document instance methods.
|
|
198
198
|
*/
|
|
199
|
-
methods?: Record<any, (this: HydratedDocument<DocType>, ...args: any) => unknown> |
|
|
199
|
+
methods?: Record<any, (this: HydratedDocument<DocType>, ...args: any) => unknown> | TInstanceMethods,
|
|
200
200
|
|
|
201
201
|
/**
|
|
202
|
-
* Query helper functions
|
|
202
|
+
* Query helper functions.
|
|
203
203
|
*/
|
|
204
204
|
query?: Record<any, <T extends QueryWithHelpers<unknown, DocType>>(this: T, ...args: any) => T> | QueryHelpers,
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Set whether to cast non-array values to arrays.
|
|
208
|
+
* @default true
|
|
209
|
+
*/
|
|
210
|
+
castNonArrays?: boolean;
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Virtual paths.
|
|
214
|
+
*/
|
|
215
|
+
virtuals?: SchemaOptionsVirtualsPropertyType<DocType, TVirtuals, TInstanceMethods>,
|
|
205
216
|
}
|
|
206
217
|
}
|
package/types/utility.d.ts
CHANGED
|
@@ -19,4 +19,23 @@ declare module 'mongoose' {
|
|
|
19
19
|
*/
|
|
20
20
|
type FlatRecord<T> = { [K in keyof T]: T[K] };
|
|
21
21
|
|
|
22
|
+
/**
|
|
23
|
+
* @summary Checks if a type is "Record" or "any".
|
|
24
|
+
* @description It Helps to check if user has provided schema type "EnforcedDocType"
|
|
25
|
+
* @param {T} T A generic type to be checked.
|
|
26
|
+
* @returns true if {@link T} is Record OR false if {@link T} is of any type.
|
|
27
|
+
*/
|
|
28
|
+
type IsItRecordAndNotAny<T> = IfEquals<T, any, false, T extends Record<any, any> ? true : false>;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @summary Checks if two types are identical.
|
|
32
|
+
* @param {T} T The first type to be compared with {@link U}.
|
|
33
|
+
* @param {U} U The seconde type to be compared with {@link T}.
|
|
34
|
+
* @param {Y} Y A type to be returned if {@link T} & {@link U} are identical.
|
|
35
|
+
* @param {N} N A type to be returned if {@link T} & {@link U} are not identical.
|
|
36
|
+
*/
|
|
37
|
+
type IfEquals<T, U, Y = true, N = false> =
|
|
38
|
+
(<G>() => G extends T ? 1 : 0) extends
|
|
39
|
+
(<G>() => G extends U ? 1 : 0) ? Y : N;
|
|
40
|
+
|
|
22
41
|
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
declare module 'mongoose' {
|
|
2
|
+
type VirtualPathFunctions<DocType = {}, PathValueType = unknown, TInstanceMethods = {}> = {
|
|
3
|
+
get?: TVirtualPathFN<DocType, PathValueType, TInstanceMethods, PathValueType>;
|
|
4
|
+
set?: TVirtualPathFN<DocType, PathValueType, TInstanceMethods, void>;
|
|
5
|
+
options?: VirtualTypeOptions<HydratedDocument<DocType, TInstanceMethods>, DocType>;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
type TVirtualPathFN<DocType = {}, PathType = unknown, TInstanceMethods = {}, TReturn = unknown> =
|
|
9
|
+
<T = HydratedDocument<DocType, TInstanceMethods>>(this: Document<any, any, DocType> & DocType, value: PathType, virtual: VirtualType<T>, doc: Document<any, any, DocType> & DocType) => TReturn;
|
|
10
|
+
|
|
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
|
+
}
|