mongoose 8.17.1 → 8.18.0
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/deno.lock +53 -0
- package/dist/browser.umd.js +1 -1
- package/lib/document.js +10 -8
- package/lib/error/messages.js +2 -2
- package/lib/model.js +13 -5
- package/lib/options/schemaUnionOptions.js +32 -0
- package/lib/schema/array.js +2 -0
- package/lib/schema/bigint.js +3 -1
- package/lib/schema/boolean.js +3 -1
- package/lib/schema/buffer.js +2 -0
- package/lib/schema/date.js +3 -1
- package/lib/schema/decimal128.js +2 -0
- package/lib/schema/documentArray.js +2 -0
- package/lib/schema/double.js +3 -1
- package/lib/schema/index.js +3 -2
- package/lib/schema/int32.js +3 -1
- package/lib/schema/number.js +3 -1
- package/lib/schema/objectId.js +4 -1
- package/lib/schema/string.js +3 -1
- package/lib/schema/subdocument.js +2 -0
- package/lib/schema/union.js +105 -0
- package/lib/schema/uuid.js +2 -0
- package/lib/schema.js +5 -3
- package/lib/schemaType.js +14 -0
- package/package.json +1 -1
- package/types/collection.d.ts +21 -18
- package/types/connection.d.ts +1 -2
- package/types/document.d.ts +20 -9
- package/types/index.d.ts +14 -2
- package/types/inferrawdoctype.d.ts +12 -7
- package/types/inferschematype.d.ts +13 -11
- package/types/schematypes.d.ts +8 -1
package/types/index.d.ts
CHANGED
|
@@ -140,9 +140,21 @@ declare module 'mongoose' {
|
|
|
140
140
|
? IfAny<U, T & { _id: Types.ObjectId }, T & Required<{ _id: U }>>
|
|
141
141
|
: T & { _id: Types.ObjectId };
|
|
142
142
|
|
|
143
|
-
export type Default__v<T, TSchemaOptions = {}> = TSchemaOptions extends { versionKey: false }
|
|
143
|
+
export type Default__v<T, TSchemaOptions = {}> = TSchemaOptions extends { versionKey: false }
|
|
144
144
|
? T
|
|
145
|
-
:
|
|
145
|
+
: TSchemaOptions extends { versionKey: infer VK }
|
|
146
|
+
? (
|
|
147
|
+
// If VK is a *literal* string, add that property
|
|
148
|
+
T & {
|
|
149
|
+
[K in VK as K extends string
|
|
150
|
+
? (string extends K ? never : K) // drop if wide string
|
|
151
|
+
: never
|
|
152
|
+
]: number
|
|
153
|
+
}
|
|
154
|
+
)
|
|
155
|
+
: T extends { __v?: infer U }
|
|
156
|
+
? T
|
|
157
|
+
: T & { __v: number };
|
|
146
158
|
|
|
147
159
|
/** Helper type for getting the hydrated document type from the raw document type. The hydrated document type is what `new MyModel()` returns. */
|
|
148
160
|
export type HydratedDocument<
|
|
@@ -35,6 +35,10 @@ declare module 'mongoose' {
|
|
|
35
35
|
TypeKey
|
|
36
36
|
>;
|
|
37
37
|
|
|
38
|
+
type UnionToRawPathType<T extends readonly any[]> = T[number] extends infer U
|
|
39
|
+
? ResolveRawPathType<U>
|
|
40
|
+
: never;
|
|
41
|
+
|
|
38
42
|
/**
|
|
39
43
|
* Same as inferSchemaType, except:
|
|
40
44
|
*
|
|
@@ -109,11 +113,12 @@ declare module 'mongoose' {
|
|
|
109
113
|
IfEquals<PathValueType, Schema.Types.UUID> extends true ? Buffer :
|
|
110
114
|
PathValueType extends MapConstructor | 'Map' ? Map<string, ResolveRawPathType<Options['of']>> :
|
|
111
115
|
IfEquals<PathValueType, typeof Schema.Types.Map> extends true ? Map<string, ResolveRawPathType<Options['of']>> :
|
|
112
|
-
PathValueType extends
|
|
113
|
-
PathValueType extends
|
|
114
|
-
|
|
115
|
-
IfEquals<PathValueType,
|
|
116
|
-
PathValueType extends
|
|
117
|
-
PathValueType extends
|
|
118
|
-
|
|
116
|
+
PathValueType extends 'Union' | 'union' | typeof Schema.Types.Union ? Options['of'] extends readonly any[] ? UnionToRawPathType<Options['of']> : never :
|
|
117
|
+
PathValueType extends ArrayConstructor ? any[] :
|
|
118
|
+
PathValueType extends typeof Schema.Types.Mixed ? any:
|
|
119
|
+
IfEquals<PathValueType, ObjectConstructor> extends true ? any:
|
|
120
|
+
IfEquals<PathValueType, {}> extends true ? any:
|
|
121
|
+
PathValueType extends typeof SchemaType ? PathValueType['prototype'] :
|
|
122
|
+
PathValueType extends Record<string, any> ? InferRawDocType<PathValueType> :
|
|
123
|
+
unknown;
|
|
119
124
|
}
|
|
@@ -75,10 +75,7 @@ declare module 'mongoose' {
|
|
|
75
75
|
|
|
76
76
|
type ApplySchemaOptions<T, O = DefaultSchemaOptions> = ResolveTimestamps<T, O>;
|
|
77
77
|
|
|
78
|
-
type ResolveTimestamps<T, O> = O extends {
|
|
79
|
-
// For some reason, TypeScript sets all the document properties to unknown
|
|
80
|
-
// if we use methods, statics, or virtuals. So avoid inferring timestamps
|
|
81
|
-
// if any of these are set for now. See gh-12807
|
|
78
|
+
type ResolveTimestamps<T, O> = O extends { timestamps?: false } ? T
|
|
82
79
|
: O extends { timestamps: infer TimestampOptions } ? TimestampOptions extends true
|
|
83
80
|
? { createdAt: NativeDate; updatedAt: NativeDate; } & T
|
|
84
81
|
: TimestampOptions extends SchemaTimestampsConfig
|
|
@@ -206,6 +203,10 @@ TypeHint<PathValueType>
|
|
|
206
203
|
*/
|
|
207
204
|
type PathEnumOrString<T extends SchemaTypeOptions<string>['enum']> = T extends ReadonlyArray<infer E> ? E : T extends { values: any } ? PathEnumOrString<T['values']> : T extends Record<string, infer V> ? V : string;
|
|
208
205
|
|
|
206
|
+
type UnionToType<T extends readonly any[]> = T[number] extends infer U
|
|
207
|
+
? ResolvePathType<U>
|
|
208
|
+
: never;
|
|
209
|
+
|
|
209
210
|
type IsSchemaTypeFromBuiltinClass<T> = T extends (typeof String)
|
|
210
211
|
? true
|
|
211
212
|
: T extends (typeof Number)
|
|
@@ -317,11 +318,12 @@ type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<PathValueT
|
|
|
317
318
|
IfEquals<PathValueType, Schema.Types.UUID> extends true ? Buffer :
|
|
318
319
|
PathValueType extends MapConstructor | 'Map' ? Map<string, ResolvePathType<Options['of']>> :
|
|
319
320
|
IfEquals<PathValueType, typeof Schema.Types.Map> extends true ? Map<string, ResolvePathType<Options['of']>> :
|
|
320
|
-
PathValueType extends
|
|
321
|
-
PathValueType extends
|
|
322
|
-
|
|
323
|
-
IfEquals<PathValueType,
|
|
324
|
-
PathValueType extends
|
|
325
|
-
PathValueType extends
|
|
326
|
-
|
|
321
|
+
PathValueType extends 'Union' | 'union' | typeof Schema.Types.Union ? Options['of'] extends readonly any[] ? UnionToType<Options['of']> : never :
|
|
322
|
+
PathValueType extends ArrayConstructor ? any[] :
|
|
323
|
+
PathValueType extends typeof Schema.Types.Mixed ? any:
|
|
324
|
+
IfEquals<PathValueType, ObjectConstructor> extends true ? any:
|
|
325
|
+
IfEquals<PathValueType, {}> extends true ? any:
|
|
326
|
+
PathValueType extends typeof SchemaType ? PathValueType['prototype'] :
|
|
327
|
+
PathValueType extends Record<string, any> ? ObtainDocumentType<PathValueType, any, { typeKey: TypeKey }> :
|
|
328
|
+
unknown,
|
|
327
329
|
TypeHint>;
|
package/types/schematypes.d.ts
CHANGED
|
@@ -188,7 +188,7 @@ declare module 'mongoose' {
|
|
|
188
188
|
_id?: boolean;
|
|
189
189
|
|
|
190
190
|
/** If set, specifies the type of this map's values. Mongoose will cast this map's values to the given type. */
|
|
191
|
-
of?: Function | SchemaDefinitionProperty<any
|
|
191
|
+
of?: Function | SchemaDefinitionProperty<any> | (Function | SchemaDefinitionProperty<any>)[];
|
|
192
192
|
|
|
193
193
|
/** If true, uses Mongoose's default `_id` settings. Only allowed for ObjectIds */
|
|
194
194
|
auto?: boolean;
|
|
@@ -569,6 +569,13 @@ declare module 'mongoose' {
|
|
|
569
569
|
static defaultOptions: Record<string, any>;
|
|
570
570
|
}
|
|
571
571
|
|
|
572
|
+
class Union extends SchemaType {
|
|
573
|
+
static schemaName: 'Union';
|
|
574
|
+
|
|
575
|
+
/** Default options for this SchemaType */
|
|
576
|
+
static defaultOptions: Record<string, any>;
|
|
577
|
+
}
|
|
578
|
+
|
|
572
579
|
class UUID extends SchemaType {
|
|
573
580
|
/** This schema type's name, to defend against minifiers that mangle function names. */
|
|
574
581
|
static schemaName: 'UUID';
|