mongoose 6.3.4 → 6.3.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.json +1 -1
- package/dist/browser.umd.js +1 -1
- package/lgtm.yml +12 -0
- package/lib/cast/objectid.js +3 -2
- package/lib/connection.js +3 -2
- package/lib/document.js +37 -24
- package/lib/error/index.js +2 -2
- package/lib/helpers/clone.js +7 -1
- package/lib/helpers/common.js +3 -4
- package/lib/helpers/discriminator/areDiscriminatorValuesEqual.js +2 -2
- package/lib/helpers/isBsonType.js +5 -3
- package/lib/helpers/path/setDottedPath.js +14 -1
- package/lib/helpers/schematype/handleImmutable.js +2 -1
- package/lib/helpers/topology/isAtlas.js +15 -2
- package/lib/helpers/update/applyTimestampsToChildren.js +4 -0
- package/lib/helpers/update/castArrayFilters.js +3 -0
- package/lib/index.js +4 -3
- package/lib/options/SchemaTypeOptions.js +13 -0
- package/lib/query.js +4 -1
- package/lib/schema/SubdocumentPath.js +8 -1
- package/lib/schema/decimal128.js +4 -4
- package/lib/schema/documentarray.js +2 -4
- package/lib/schema/map.js +8 -0
- package/lib/schema/objectid.js +4 -3
- package/lib/schema/string.js +2 -2
- package/lib/schema.js +33 -2
- package/lib/schematype.js +1 -0
- package/lib/types/DocumentArray/methods/index.js +3 -3
- package/lib/types/array/methods/index.js +3 -3
- package/lib/types/map.js +4 -4
- package/lib/utils.js +3 -4
- package/package.json +17 -15
- package/types/aggregate.d.ts +3 -4
- package/types/callback.d.ts +8 -0
- package/types/collection.d.ts +46 -0
- package/types/connection.d.ts +91 -71
- package/types/document.d.ts +1 -1
- package/types/error.d.ts +5 -1
- package/types/helpers.d.ts +32 -0
- package/types/index.d.ts +32 -1859
- package/types/indizes.d.ts +98 -0
- package/types/middlewares.d.ts +14 -0
- package/types/models.d.ts +419 -0
- package/types/populate.d.ts +40 -0
- package/types/query.d.ts +637 -0
- package/types/schematypes.d.ts +427 -0
- package/types/session.d.ts +36 -0
- package/types/types.d.ts +102 -0
- package/types/utility.d.ts +13 -0
- package/types/validation.d.ts +32 -0
- package/.lgtm.yml +0 -3
- package/CHANGELOG.md +0 -7300
- package/History.md +0 -1
|
@@ -0,0 +1,427 @@
|
|
|
1
|
+
declare module 'mongoose' {
|
|
2
|
+
|
|
3
|
+
/** The Mongoose Date [SchemaType](/docs/schematypes.html). */
|
|
4
|
+
type Date = Schema.Types.Date;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The Mongoose Decimal128 [SchemaType](/docs/schematypes.html). Used for
|
|
8
|
+
* declaring paths in your schema that should be
|
|
9
|
+
* [128-bit decimal floating points](http://thecodebarbarian.com/a-nodejs-perspective-on-mongodb-34-decimal.html).
|
|
10
|
+
* Do not use this to create a new Decimal128 instance, use `mongoose.Types.Decimal128`
|
|
11
|
+
* instead.
|
|
12
|
+
*/
|
|
13
|
+
type Decimal128 = Schema.Types.Decimal128;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The Mongoose Mixed [SchemaType](/docs/schematypes.html). Used for
|
|
17
|
+
* declaring paths in your schema that Mongoose's change tracking, casting,
|
|
18
|
+
* and validation should ignore.
|
|
19
|
+
*/
|
|
20
|
+
type Mixed = Schema.Types.Mixed;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The Mongoose Number [SchemaType](/docs/schematypes.html). Used for
|
|
24
|
+
* declaring paths in your schema that Mongoose should cast to numbers.
|
|
25
|
+
*/
|
|
26
|
+
type Number = Schema.Types.Number;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The Mongoose ObjectId [SchemaType](/docs/schematypes.html). Used for
|
|
30
|
+
* declaring paths in your schema that should be
|
|
31
|
+
* [MongoDB ObjectIds](https://docs.mongodb.com/manual/reference/method/ObjectId/).
|
|
32
|
+
* Do not use this to create a new ObjectId instance, use `mongoose.Types.ObjectId`
|
|
33
|
+
* instead.
|
|
34
|
+
*/
|
|
35
|
+
type ObjectId = Schema.Types.ObjectId;
|
|
36
|
+
|
|
37
|
+
/** The various Mongoose SchemaTypes. */
|
|
38
|
+
const SchemaTypes: typeof Schema.Types;
|
|
39
|
+
|
|
40
|
+
type DefaultType<T> = T extends Schema.Types.Mixed ? any : Partial<ExtractMongooseArray<T>>;
|
|
41
|
+
|
|
42
|
+
class SchemaTypeOptions<T> {
|
|
43
|
+
type?:
|
|
44
|
+
T extends string ? StringSchemaDefinition :
|
|
45
|
+
T extends number ? NumberSchemaDefinition :
|
|
46
|
+
T extends boolean ? BooleanSchemaDefinition :
|
|
47
|
+
T extends NativeDate ? DateSchemaDefinition :
|
|
48
|
+
T extends Map<any, any> ? SchemaDefinition<typeof Map> :
|
|
49
|
+
T extends Buffer ? SchemaDefinition<typeof Buffer> :
|
|
50
|
+
T extends Types.ObjectId ? ObjectIdSchemaDefinition :
|
|
51
|
+
T extends Types.ObjectId[] ? AnyArray<ObjectIdSchemaDefinition> | AnyArray<SchemaTypeOptions<ObjectId>> :
|
|
52
|
+
T extends object[] ? (AnyArray<Schema<any, any, any>> | AnyArray<SchemaDefinition<Unpacked<T>>> | AnyArray<SchemaTypeOptions<Unpacked<T>>>) :
|
|
53
|
+
T extends string[] ? AnyArray<StringSchemaDefinition> | AnyArray<SchemaTypeOptions<string>> :
|
|
54
|
+
T extends number[] ? AnyArray<NumberSchemaDefinition> | AnyArray<SchemaTypeOptions<number>> :
|
|
55
|
+
T extends boolean[] ? AnyArray<BooleanSchemaDefinition> | AnyArray<SchemaTypeOptions<boolean>> :
|
|
56
|
+
T extends Function[] ? AnyArray<Function | string> | AnyArray<SchemaTypeOptions<Unpacked<T>>> :
|
|
57
|
+
T | typeof SchemaType | Schema<any, any, any> | SchemaDefinition<T> | Function | AnyArray<Function>;
|
|
58
|
+
|
|
59
|
+
/** Defines a virtual with the given name that gets/sets this path. */
|
|
60
|
+
alias?: string;
|
|
61
|
+
|
|
62
|
+
/** Function or object describing how to validate this schematype. See [validation docs](https://mongoosejs.com/docs/validation.html). */
|
|
63
|
+
validate?: SchemaValidator<T> | AnyArray<SchemaValidator<T>>;
|
|
64
|
+
|
|
65
|
+
/** Allows overriding casting logic for this individual path. If a string, the given string overwrites Mongoose's default cast error message. */
|
|
66
|
+
cast?: string;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* If true, attach a required validator to this path, which ensures this path
|
|
70
|
+
* path cannot be set to a nullish value. If a function, Mongoose calls the
|
|
71
|
+
* function and only checks for nullish values if the function returns a truthy value.
|
|
72
|
+
*/
|
|
73
|
+
required?: boolean | (() => boolean) | [boolean, string] | [() => boolean, string];
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* The default value for this path. If a function, Mongoose executes the function
|
|
77
|
+
* and uses the return value as the default.
|
|
78
|
+
*/
|
|
79
|
+
default?: DefaultType<T> | ((this: any, doc: any) => DefaultType<T>) | null;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* The model that `populate()` should use if populating this path.
|
|
83
|
+
*/
|
|
84
|
+
ref?: string | Model<any> | ((this: any, doc: any) => string | Model<any>);
|
|
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
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Whether to include or exclude this path by default when loading documents
|
|
95
|
+
* using `find()`, `findOne()`, etc.
|
|
96
|
+
*/
|
|
97
|
+
select?: boolean | number;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose will
|
|
101
|
+
* build an index on this path when the model is compiled.
|
|
102
|
+
*/
|
|
103
|
+
index?: boolean | number | IndexOptions | '2d' | '2dsphere' | 'hashed' | 'text';
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose
|
|
107
|
+
* will build a unique index on this path when the
|
|
108
|
+
* model is compiled. [The `unique` option is **not** a validator](/docs/validation.html#the-unique-option-is-not-a-validator).
|
|
109
|
+
*/
|
|
110
|
+
unique?: boolean | number;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose will
|
|
114
|
+
* disallow changes to this path once the document is saved to the database for the first time. Read more
|
|
115
|
+
* about [immutability in Mongoose here](http://thecodebarbarian.com/whats-new-in-mongoose-5-6-immutable-properties.html).
|
|
116
|
+
*/
|
|
117
|
+
immutable?: boolean | ((this: any, doc: any) => boolean);
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose will
|
|
121
|
+
* build a sparse index on this path.
|
|
122
|
+
*/
|
|
123
|
+
sparse?: boolean | number;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose
|
|
127
|
+
* will build a text index on this path.
|
|
128
|
+
*/
|
|
129
|
+
text?: boolean | number | any;
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Define a transform function for this individual schema type.
|
|
133
|
+
* Only called when calling `toJSON()` or `toObject()`.
|
|
134
|
+
*/
|
|
135
|
+
transform?: (this: any, val: T) => any;
|
|
136
|
+
|
|
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). */
|
|
138
|
+
get?: (value: any, doc?: this) => T | undefined;
|
|
139
|
+
|
|
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). */
|
|
141
|
+
set?: (value: any, priorVal?: T, doc?: this) => any;
|
|
142
|
+
|
|
143
|
+
/** array of allowed values for this path. Allowed for strings, numbers, and arrays of strings */
|
|
144
|
+
enum?: Array<string | number | null> | ReadonlyArray<string | number | null> | { values: Array<string | number | null> | ReadonlyArray<string | number | null>, message?: string } | { [path: string]: string | number | null };
|
|
145
|
+
|
|
146
|
+
/** The default [subtype](http://bsonspec.org/spec.html) associated with this buffer when it is stored in MongoDB. Only allowed for buffer paths */
|
|
147
|
+
subtype?: number;
|
|
148
|
+
|
|
149
|
+
/** The minimum value allowed for this path. Only allowed for numbers and dates. */
|
|
150
|
+
min?: number | NativeDate | [number, string] | [NativeDate, string] | readonly [number, string] | readonly [NativeDate, string];
|
|
151
|
+
|
|
152
|
+
/** The maximum value allowed for this path. Only allowed for numbers and dates. */
|
|
153
|
+
max?: number | NativeDate | [number, string] | [NativeDate, string] | readonly [number, string] | readonly [NativeDate, string];
|
|
154
|
+
|
|
155
|
+
/** Defines a TTL index on this path. Only allowed for dates. */
|
|
156
|
+
expires?: string | number;
|
|
157
|
+
|
|
158
|
+
/** If `true`, Mongoose will skip gathering indexes on subpaths. Only allowed for subdocuments and subdocument arrays. */
|
|
159
|
+
excludeIndexes?: boolean;
|
|
160
|
+
|
|
161
|
+
/** If set, overrides the child schema's `_id` option. Only allowed for subdocuments and subdocument arrays. */
|
|
162
|
+
_id?: boolean;
|
|
163
|
+
|
|
164
|
+
/** If set, specifies the type of this map's values. Mongoose will cast this map's values to the given type. */
|
|
165
|
+
of?: Function | SchemaDefinitionProperty<any>;
|
|
166
|
+
|
|
167
|
+
/** If true, uses Mongoose's default `_id` settings. Only allowed for ObjectIds */
|
|
168
|
+
auto?: boolean;
|
|
169
|
+
|
|
170
|
+
/** Attaches a validator that succeeds if the data string matches the given regular expression, and fails otherwise. */
|
|
171
|
+
match?: RegExp | [RegExp, string] | readonly [RegExp, string];
|
|
172
|
+
|
|
173
|
+
/** If truthy, Mongoose will add a custom setter that lowercases this string using JavaScript's built-in `String#toLowerCase()`. */
|
|
174
|
+
lowercase?: boolean;
|
|
175
|
+
|
|
176
|
+
/** If truthy, Mongoose will add a custom setter that removes leading and trailing whitespace using JavaScript's built-in `String#trim()`. */
|
|
177
|
+
trim?: boolean;
|
|
178
|
+
|
|
179
|
+
/** If truthy, Mongoose will add a custom setter that uppercases this string using JavaScript's built-in `String#toUpperCase()`. */
|
|
180
|
+
uppercase?: boolean;
|
|
181
|
+
|
|
182
|
+
/** If set, Mongoose will add a custom validator that ensures the given string's `length` is at least the given number. */
|
|
183
|
+
minlength?: number | [number, string] | readonly [number, string];
|
|
184
|
+
|
|
185
|
+
/** If set, Mongoose will add a custom validator that ensures the given string's `length` is at most the given number. */
|
|
186
|
+
maxlength?: number | [number, string] | readonly [number, string];
|
|
187
|
+
|
|
188
|
+
[other: string]: any;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
class SchemaType<T = any> {
|
|
192
|
+
/** SchemaType constructor */
|
|
193
|
+
constructor(path: string, options?: AnyObject, instance?: string);
|
|
194
|
+
|
|
195
|
+
/** Get/set the function used to cast arbitrary values to this type. */
|
|
196
|
+
static cast(caster?: Function | boolean): Function;
|
|
197
|
+
|
|
198
|
+
static checkRequired(checkRequired?: (v: any) => boolean): (v: any) => boolean;
|
|
199
|
+
|
|
200
|
+
/** Sets a default option for this schema type. */
|
|
201
|
+
static set(option: string, value: any): void;
|
|
202
|
+
|
|
203
|
+
/** Attaches a getter for all instances of this schema type. */
|
|
204
|
+
static get(getter: (value: any) => any): void;
|
|
205
|
+
|
|
206
|
+
/** The class that Mongoose uses internally to instantiate this SchemaType's `options` property. */
|
|
207
|
+
OptionsConstructor: SchemaTypeOptions<T>;
|
|
208
|
+
|
|
209
|
+
/** Cast `val` to this schema type. Each class that inherits from schema type should implement this function. */
|
|
210
|
+
cast(val: any, doc: Document<any>, init: boolean, prev?: any, options?: any): any;
|
|
211
|
+
|
|
212
|
+
/** Sets a default value for this SchemaType. */
|
|
213
|
+
default(val: any): any;
|
|
214
|
+
|
|
215
|
+
/** Adds a getter to this schematype. */
|
|
216
|
+
get(fn: Function): this;
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Defines this path as immutable. Mongoose prevents you from changing
|
|
220
|
+
* immutable paths unless the parent document has [`isNew: true`](/docs/api.html#document_Document-isNew).
|
|
221
|
+
*/
|
|
222
|
+
immutable(bool: boolean): this;
|
|
223
|
+
|
|
224
|
+
/** Declares the index options for this schematype. */
|
|
225
|
+
index(options: any): this;
|
|
226
|
+
|
|
227
|
+
/** String representation of what type this is, like 'ObjectID' or 'Number' */
|
|
228
|
+
instance: string;
|
|
229
|
+
|
|
230
|
+
/** True if this SchemaType has a required validator. False otherwise. */
|
|
231
|
+
isRequired?: boolean;
|
|
232
|
+
|
|
233
|
+
/** The options this SchemaType was instantiated with */
|
|
234
|
+
options: AnyObject;
|
|
235
|
+
|
|
236
|
+
/** The path to this SchemaType in a Schema. */
|
|
237
|
+
path: string;
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Set the model that this path refers to. This is the option that [populate](https://mongoosejs.com/docs/populate.html)
|
|
241
|
+
* looks at to determine the foreign collection it should query.
|
|
242
|
+
*/
|
|
243
|
+
ref(ref: string | boolean | Model<any>): this;
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Adds a required validator to this SchemaType. The validator gets added
|
|
247
|
+
* to the front of this SchemaType's validators array using unshift().
|
|
248
|
+
*/
|
|
249
|
+
required(required: boolean, message?: string): this;
|
|
250
|
+
|
|
251
|
+
/** The schema this SchemaType instance is part of */
|
|
252
|
+
schema: Schema<any>;
|
|
253
|
+
|
|
254
|
+
/** Sets default select() behavior for this path. */
|
|
255
|
+
select(val: boolean): this;
|
|
256
|
+
|
|
257
|
+
/** Adds a setter to this schematype. */
|
|
258
|
+
set(fn: Function): this;
|
|
259
|
+
|
|
260
|
+
/** Declares a sparse index. */
|
|
261
|
+
sparse(bool: boolean): this;
|
|
262
|
+
|
|
263
|
+
/** Declares a full text index. */
|
|
264
|
+
text(bool: boolean): this;
|
|
265
|
+
|
|
266
|
+
/** Defines a custom function for transforming this path when converting a document to JSON. */
|
|
267
|
+
transform(fn: (value: any) => any): this;
|
|
268
|
+
|
|
269
|
+
/** Declares an unique index. */
|
|
270
|
+
unique(bool: boolean): this;
|
|
271
|
+
|
|
272
|
+
/** The validators that Mongoose should run to validate properties at this SchemaType's path. */
|
|
273
|
+
validators: { message?: string; type?: string; validator?: Function }[];
|
|
274
|
+
|
|
275
|
+
/** Adds validator(s) for this document path. */
|
|
276
|
+
validate(obj: RegExp | Function | any, errorMsg?: string, type?: string): this;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
namespace Schema {
|
|
280
|
+
namespace Types {
|
|
281
|
+
class Array extends SchemaType implements AcceptsDiscriminator {
|
|
282
|
+
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
283
|
+
static schemaName: 'Array';
|
|
284
|
+
|
|
285
|
+
static options: { castNonArrays: boolean; };
|
|
286
|
+
|
|
287
|
+
discriminator<T, U>(name: string | number, schema: Schema<T, U>, value?: string): U;
|
|
288
|
+
discriminator<D>(name: string | number, schema: Schema, value?: string): Model<D>;
|
|
289
|
+
|
|
290
|
+
/** The schematype embedded in this array */
|
|
291
|
+
caster?: SchemaType;
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Adds an enum validator if this is an array of strings or numbers. Equivalent to
|
|
295
|
+
* `SchemaString.prototype.enum()` or `SchemaNumber.prototype.enum()`
|
|
296
|
+
*/
|
|
297
|
+
enum(vals: string[] | number[]): this;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
class Boolean extends SchemaType {
|
|
301
|
+
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
302
|
+
static schemaName: 'Boolean';
|
|
303
|
+
|
|
304
|
+
/** Configure which values get casted to `true`. */
|
|
305
|
+
static convertToTrue: Set<any>;
|
|
306
|
+
|
|
307
|
+
/** Configure which values get casted to `false`. */
|
|
308
|
+
static convertToFalse: Set<any>;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
class Buffer extends SchemaType {
|
|
312
|
+
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
313
|
+
static schemaName: 'Buffer';
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Sets the default [subtype](https://studio3t.com/whats-new/best-practices-uuid-mongodb/)
|
|
317
|
+
* for this buffer. You can find a [list of allowed subtypes here](http://api.mongodb.com/python/current/api/bson/binary.html).
|
|
318
|
+
*/
|
|
319
|
+
subtype(subtype: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 128): this;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
class Date extends SchemaType {
|
|
323
|
+
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
324
|
+
static schemaName: 'Date';
|
|
325
|
+
|
|
326
|
+
/** Declares a TTL index (rounded to the nearest second) for _Date_ types only. */
|
|
327
|
+
expires(when: number | string): this;
|
|
328
|
+
|
|
329
|
+
/** Sets a maximum date validator. */
|
|
330
|
+
max(value: NativeDate, message: string): this;
|
|
331
|
+
|
|
332
|
+
/** Sets a minimum date validator. */
|
|
333
|
+
min(value: NativeDate, message: string): this;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
class Decimal128 extends SchemaType {
|
|
337
|
+
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
338
|
+
static schemaName: 'Decimal128';
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
class DocumentArray extends SchemaType implements AcceptsDiscriminator {
|
|
342
|
+
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
343
|
+
static schemaName: 'DocumentArray';
|
|
344
|
+
|
|
345
|
+
static options: { castNonArrays: boolean; };
|
|
346
|
+
|
|
347
|
+
discriminator<D>(name: string | number, schema: Schema, value?: string): Model<D>;
|
|
348
|
+
discriminator<T, U>(name: string | number, schema: Schema<T, U>, value?: string): U;
|
|
349
|
+
|
|
350
|
+
/** The schema used for documents in this array */
|
|
351
|
+
schema: Schema;
|
|
352
|
+
|
|
353
|
+
/** The constructor used for subdocuments in this array */
|
|
354
|
+
caster?: typeof Types.Subdocument;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
class Map extends SchemaType {
|
|
358
|
+
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
359
|
+
static schemaName: 'Map';
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
class Mixed extends SchemaType {
|
|
363
|
+
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
364
|
+
static schemaName: 'Mixed';
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
class Number extends SchemaType {
|
|
368
|
+
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
369
|
+
static schemaName: 'Number';
|
|
370
|
+
|
|
371
|
+
/** Sets a enum validator */
|
|
372
|
+
enum(vals: number[]): this;
|
|
373
|
+
|
|
374
|
+
/** Sets a maximum number validator. */
|
|
375
|
+
max(value: number, message: string): this;
|
|
376
|
+
|
|
377
|
+
/** Sets a minimum number validator. */
|
|
378
|
+
min(value: number, message: string): this;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
class ObjectId extends SchemaType {
|
|
382
|
+
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
383
|
+
static schemaName: 'ObjectId';
|
|
384
|
+
|
|
385
|
+
/** Adds an auto-generated ObjectId default if turnOn is true. */
|
|
386
|
+
auto(turnOn: boolean): this;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
class Subdocument extends SchemaType implements AcceptsDiscriminator {
|
|
390
|
+
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
391
|
+
static schemaName: string;
|
|
392
|
+
|
|
393
|
+
/** The document's schema */
|
|
394
|
+
schema: Schema;
|
|
395
|
+
|
|
396
|
+
discriminator<T, U>(name: string | number, schema: Schema<T, U>, value?: string): U;
|
|
397
|
+
discriminator<D>(name: string | number, schema: Schema, value?: string): Model<D>;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
class String extends SchemaType {
|
|
401
|
+
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
402
|
+
static schemaName: 'String';
|
|
403
|
+
|
|
404
|
+
/** Adds an enum validator */
|
|
405
|
+
enum(vals: string[] | any): this;
|
|
406
|
+
|
|
407
|
+
/** Adds a lowercase [setter](http://mongoosejs.com/docs/api.html#schematype_SchemaType-set). */
|
|
408
|
+
lowercase(shouldApply?: boolean): this;
|
|
409
|
+
|
|
410
|
+
/** Sets a regexp validator. */
|
|
411
|
+
match(value: RegExp, message: string): this;
|
|
412
|
+
|
|
413
|
+
/** Sets a maximum length validator. */
|
|
414
|
+
maxlength(value: number, message: string): this;
|
|
415
|
+
|
|
416
|
+
/** Sets a minimum length validator. */
|
|
417
|
+
minlength(value: number, message: string): this;
|
|
418
|
+
|
|
419
|
+
/** Adds a trim [setter](http://mongoosejs.com/docs/api.html#schematype_SchemaType-set). */
|
|
420
|
+
trim(shouldTrim?: boolean): this;
|
|
421
|
+
|
|
422
|
+
/** Adds an uppercase [setter](http://mongoosejs.com/docs/api.html#schematype_SchemaType-set). */
|
|
423
|
+
uppercase(shouldApply?: boolean): this;
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
declare module 'mongoose' {
|
|
2
|
+
import mongodb = require('mongodb');
|
|
3
|
+
|
|
4
|
+
type ClientSessionOptions = mongodb.ClientSessionOptions;
|
|
5
|
+
type ClientSession = mongodb.ClientSession;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* _Requires MongoDB >= 3.6.0._ Starts a [MongoDB session](https://docs.mongodb.com/manual/release-notes/3.6/#client-sessions)
|
|
9
|
+
* for benefits like causal consistency, [retryable writes](https://docs.mongodb.com/manual/core/retryable-writes/),
|
|
10
|
+
* and [transactions](http://thecodebarbarian.com/a-node-js-perspective-on-mongodb-4-transactions.html).
|
|
11
|
+
*/
|
|
12
|
+
function startSession(options: ClientSessionOptions | undefined | null, callback: Callback<ClientSession>): void;
|
|
13
|
+
function startSession(callback: Callback<ClientSession>): void;
|
|
14
|
+
function startSession(options?: ClientSessionOptions): Promise<ClientSession>;
|
|
15
|
+
|
|
16
|
+
interface SessionOperation {
|
|
17
|
+
/** Sets the session. Useful for [transactions](/docs/transactions.html). */
|
|
18
|
+
session(session: mongodb.ClientSession | null): this;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface SessionStarter {
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Starts a [MongoDB session](https://docs.mongodb.com/manual/release-notes/3.6/#client-sessions)
|
|
25
|
+
* for benefits like causal consistency, [retryable writes](https://docs.mongodb.com/manual/core/retryable-writes/),
|
|
26
|
+
* and [transactions](http://thecodebarbarian.com/a-node-js-perspective-on-mongodb-4-transactions.html).
|
|
27
|
+
*/
|
|
28
|
+
startSession(options: ClientSessionOptions | undefined | null, callback: Callback<ClientSession>): void;
|
|
29
|
+
startSession(callback: Callback<ClientSession>): void;
|
|
30
|
+
startSession(options?: ClientSessionOptions): Promise<ClientSession>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
interface SessionOption {
|
|
34
|
+
session?: ClientSession | null;
|
|
35
|
+
}
|
|
36
|
+
}
|
package/types/types.d.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
|
|
2
|
+
declare module 'mongoose' {
|
|
3
|
+
import mongodb = require('mongodb');
|
|
4
|
+
|
|
5
|
+
namespace Types {
|
|
6
|
+
class Array<T> extends global.Array<T> {
|
|
7
|
+
/** Pops the array atomically at most one time per document `save()`. */
|
|
8
|
+
$pop(): T;
|
|
9
|
+
|
|
10
|
+
/** Atomically shifts the array at most one time per document `save()`. */
|
|
11
|
+
$shift(): T;
|
|
12
|
+
|
|
13
|
+
/** Adds values to the array if not already present. */
|
|
14
|
+
addToSet(...args: any[]): any[];
|
|
15
|
+
|
|
16
|
+
isMongooseArray: true;
|
|
17
|
+
|
|
18
|
+
/** Pushes items to the array non-atomically. */
|
|
19
|
+
nonAtomicPush(...args: any[]): number;
|
|
20
|
+
|
|
21
|
+
/** Wraps [`Array#push`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/push) with proper change tracking. */
|
|
22
|
+
push(...args: any[]): number;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Pulls items from the array atomically. Equality is determined by casting
|
|
26
|
+
* the provided value to an embedded document and comparing using
|
|
27
|
+
* [the `Document.equals()` function.](./api.html#document_Document-equals)
|
|
28
|
+
*/
|
|
29
|
+
pull(...args: any[]): this;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Alias of [pull](#mongoosearray_MongooseArray-pull)
|
|
33
|
+
*/
|
|
34
|
+
remove(...args: any[]): this;
|
|
35
|
+
|
|
36
|
+
/** Sets the casted `val` at index `i` and marks the array modified. */
|
|
37
|
+
set(index: number, val: T): this;
|
|
38
|
+
|
|
39
|
+
/** Atomically shifts the array at most one time per document `save()`. */
|
|
40
|
+
shift(): T;
|
|
41
|
+
|
|
42
|
+
/** Returns a native js Array. */
|
|
43
|
+
toObject(options?: ToObjectOptions): any;
|
|
44
|
+
toObject<T>(options?: ToObjectOptions): T;
|
|
45
|
+
|
|
46
|
+
/** Wraps [`Array#unshift`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/unshift) with proper change tracking. */
|
|
47
|
+
unshift(...args: any[]): number;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
class Buffer extends global.Buffer {
|
|
51
|
+
/** Sets the subtype option and marks the buffer modified. */
|
|
52
|
+
subtype(subtype: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 128 | ToObjectOptions): void;
|
|
53
|
+
|
|
54
|
+
/** Converts this buffer to its Binary type representation. */
|
|
55
|
+
toObject(subtype?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 128): mongodb.Binary;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
class Decimal128 extends mongodb.Decimal128 { }
|
|
59
|
+
|
|
60
|
+
class DocumentArray<T> extends Types.Array<T extends Types.Subdocument ? T : Types.Subdocument<InferId<T>> & T> {
|
|
61
|
+
/** DocumentArray constructor */
|
|
62
|
+
constructor(values: any[]);
|
|
63
|
+
|
|
64
|
+
isMongooseDocumentArray: true;
|
|
65
|
+
|
|
66
|
+
/** Creates a subdocument casted to this schema. */
|
|
67
|
+
create(obj: any): T extends Types.Subdocument ? T : Types.Subdocument<InferId<T>> & T;
|
|
68
|
+
|
|
69
|
+
/** Searches array items for the first document with a matching _id. */
|
|
70
|
+
id(id: any): (T extends Types.Subdocument ? T : Types.Subdocument<InferId<T>> & T) | null;
|
|
71
|
+
|
|
72
|
+
push(...args: (AnyKeys<T> & AnyObject)[]): number;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
class Map<V> extends global.Map<string, V> {
|
|
76
|
+
/** Converts a Mongoose map into a vanilla JavaScript map. */
|
|
77
|
+
toObject(options?: ToObjectOptions & { flattenMaps?: boolean }): any;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
class ObjectId extends mongodb.ObjectId {
|
|
81
|
+
_id: this;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
class Subdocument<IdType = any> extends Document<IdType> {
|
|
85
|
+
$isSingleNested: true;
|
|
86
|
+
|
|
87
|
+
/** Returns the top level document of this sub-document. */
|
|
88
|
+
ownerDocument(): Document;
|
|
89
|
+
|
|
90
|
+
/** Returns this sub-documents parent document. */
|
|
91
|
+
parent(): Document;
|
|
92
|
+
|
|
93
|
+
/** Returns this sub-documents parent document. */
|
|
94
|
+
$parent(): Document;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
class ArraySubdocument<IdType = any> extends Subdocument<IdType> {
|
|
98
|
+
/** Returns this sub-documents parent array. */
|
|
99
|
+
parentArray(): Types.DocumentArray<unknown>;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
declare module 'mongoose' {
|
|
2
|
+
|
|
3
|
+
type Unpacked<T> = T extends (infer U)[] ?
|
|
4
|
+
U :
|
|
5
|
+
T extends ReadonlyArray<infer U> ? U : T;
|
|
6
|
+
|
|
7
|
+
type UnpackedIntersection<T, U> = T extends null ? null : T extends (infer A)[]
|
|
8
|
+
? (Omit<A, keyof U> & U)[]
|
|
9
|
+
: keyof U extends never
|
|
10
|
+
? T
|
|
11
|
+
: Omit<T, keyof U> & U;
|
|
12
|
+
|
|
13
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
declare module 'mongoose' {
|
|
2
|
+
|
|
3
|
+
type SchemaValidator<T> = RegExp | [RegExp, string] | Function | [Function, string] | ValidateOpts<T> | ValidateOpts<T>[];
|
|
4
|
+
|
|
5
|
+
interface ValidatorProps {
|
|
6
|
+
path: string;
|
|
7
|
+
value: any;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
interface ValidatorMessageFn {
|
|
11
|
+
(props: ValidatorProps): string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface ValidateFn<T> {
|
|
15
|
+
(value: T): boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface LegacyAsyncValidateFn<T> {
|
|
19
|
+
(value: T, done: (result: boolean) => void): void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface AsyncValidateFn<T> {
|
|
23
|
+
(value: any): Promise<boolean>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface ValidateOpts<T> {
|
|
27
|
+
msg?: string;
|
|
28
|
+
message?: string | ValidatorMessageFn;
|
|
29
|
+
type?: string;
|
|
30
|
+
validator: ValidateFn<T> | LegacyAsyncValidateFn<T> | AsyncValidateFn<T>;
|
|
31
|
+
}
|
|
32
|
+
}
|
package/.lgtm.yml
DELETED