Skip to content

AutoInferredSchema class #14962

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,15 @@ Mongoose.prototype.Mongoose = Mongoose;

Mongoose.prototype.Schema = Schema;

/**
* Identical to `Schema` at runtime. Exists purely to work around some TypeScript compatibility issues.
*
* @method AutoInferredSchema
* @api public
*/

Mongoose.prototype.AutoInferredSchema = Schema;

/**
* The Mongoose [SchemaType](https://mongoosejs.com/docs/api/schematype.html#SchemaType()) constructor
*
Expand Down
54 changes: 50 additions & 4 deletions test/types/inferrawdoctype.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { InferRawDocType } from 'mongoose';
import { InferRawDocType, Schema, AutoInferredSchema } from 'mongoose';
import { expectType, expectError } from 'tsd';

function gh14839() {
Expand All @@ -17,9 +17,55 @@ function gh14839() {
dateOfBirth: {
type: Date,
required: true
}
},
subdoc: new AutoInferredSchema({
name: { type: String, required: true }
}),
docArr: [new AutoInferredSchema({ test: { type: String, required: true } })]
};

type UserType = InferRawDocType<typeof schemaDefinition>;
expectType<{
email: string,
password: string,
dateOfBirth: Date,
subdoc?: { name: string } | null,
docArr: { test: string }[]
}>({} as UserType);
}

function gh14954() {
const schemaDefinition = {
email: {
type: String,
trim: true,
required: true,
unique: true,
lowercase: true
},
password: {
type: String,
required: true
},
dateOfBirth: {
type: Date,
required: true
},
subdoc: new AutoInferredSchema({
name: { type: String, required: true },
l2: new AutoInferredSchema({
myProp: { type: Number, required: true }
})
}),
docArr: [new AutoInferredSchema({ test: { type: String, required: true } })]
};

type UserType = InferRawDocType< typeof schemaDefinition>;
expectType<{ email: string, password: string, dateOfBirth: Date }>({} as UserType);
type UserType = InferRawDocType<typeof schemaDefinition>;
expectType<{
email: string,
password: string,
dateOfBirth: Date,
subdoc?: { name: string, l2?: { myProp: number } | null } | null,
docArr: { test: string }[]
}>({} as UserType);
}
21 changes: 21 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,27 @@ declare module 'mongoose' {
TVirtuals,
TStaticMethods> = (schema: Schema<DocType, M, TInstanceMethods, TQueryHelpers, TVirtuals, TStaticMethods>, opts?: any) => void;

export class AutoInferredSchema<
SchemaDef = unknown,
RawDocType = InferRawDocType<SchemaDef>,
TModelType = Model<RawDocType, any, any, any>,
TInstanceMethods = {},
TQueryHelpers = {},
TVirtuals = {},
TStaticMethods = {},
TSchemaOptions = DefaultSchemaOptions,
DocType extends ApplySchemaOptions<
ObtainDocumentType<DocType, RawDocType, ResolveSchemaOptions<TSchemaOptions>>,
ResolveSchemaOptions<TSchemaOptions>
> = ApplySchemaOptions<
ObtainDocumentType<any, RawDocType, ResolveSchemaOptions<TSchemaOptions>>,
ResolveSchemaOptions<TSchemaOptions>
>,
THydratedDocumentType = HydratedDocument<FlatRecord<DocType>, TVirtuals & TInstanceMethods>
> extends Schema<RawDocType, TModelType, TInstanceMethods, TQueryHelpers, TVirtuals, TStaticMethods, TSchemaOptions, DocType, THydratedDocumentType> {
constructor(definition: SchemaDef, options?: SchemaOptions<FlatRecord<DocType>, TInstanceMethods, TQueryHelpers, TStaticMethods, TVirtuals, THydratedDocumentType> | ResolveSchemaOptions<TSchemaOptions>);
}

export class Schema<
RawDocType = any,
TModelType = Model<RawDocType, any, any, any>,
Expand Down
117 changes: 62 additions & 55 deletions types/inferrawdoctype.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
PathWithTypePropertyBaseType,
PathEnumOrString
} from './inferschematype';
import { InferSchemaType } from 'mongoose';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldnt it import it relatively?


declare module 'mongoose' {
export type InferRawDocType<
Expand Down Expand Up @@ -35,6 +36,10 @@ declare module 'mongoose' {
TypeKey
>;

type InferRawDocTypeFromSchema<TSchema extends Schema> = TSchema extends AutoInferredSchema<infer SchemaDef>
? InferRawDocType<SchemaDef>
: InferSchemaType<TSchema>;

/**
* Same as inferSchemaType, except:
*
Expand All @@ -49,35 +54,22 @@ declare module 'mongoose' {
* @returns Number, "Number" or "number" will be resolved to number type.
*/
type ResolveRawPathType<PathValueType, Options extends SchemaTypeOptions<PathValueType> = {}, TypeKey extends string = DefaultSchemaOptions['typeKey']> =
PathValueType extends Schema ?
InferSchemaType<PathValueType> :
PathValueType extends (infer Item)[] ?
IfEquals<Item, never, any[], Item extends Schema ?
// If Item is a schema, infer its type.
Array<InferSchemaType<Item>> :
Item extends Record<TypeKey, any> ?
Item[TypeKey] extends Function | String ?
// If Item has a type key that's a string or a callable, it must be a scalar,
// so we can directly obtain its path type.
ObtainRawDocumentPathType<Item, TypeKey>[] :
// If the type key isn't callable, then this is an array of objects, in which case
// we need to call InferRawDocType to correctly infer its type.
Array<InferRawDocType<Item>> :
IsSchemaTypeFromBuiltinClass<Item> extends true ?
ObtainRawDocumentPathType<Item, TypeKey>[] :
IsItRecordAndNotAny<Item> extends true ?
Item extends Record<string, never> ?
ObtainRawDocumentPathType<Item, TypeKey>[] :
Array<InferRawDocType<Item>> :
ObtainRawDocumentPathType<Item, TypeKey>[]
>:
PathValueType extends ReadonlyArray<infer Item> ?
PathValueType extends AutoInferredSchema<infer SchemaDef> ?
InferRawDocType<SchemaDef> :
PathValueType extends Schema ?
InferSchemaType<PathValueType> :
PathValueType extends (infer Item)[] ?
IfEquals<Item, never, any[], Item extends Schema ?
Array<InferSchemaType<Item>> :
// If Item is a schema, infer its type.
Array<InferRawDocTypeFromSchema<Item>> :
Item extends Record<TypeKey, any> ?
Item[TypeKey] extends Function | String ?
// If Item has a type key that's a string or a callable, it must be a scalar,
// so we can directly obtain its path type.
ObtainRawDocumentPathType<Item, TypeKey>[] :
InferRawDocType<Item>[]:
// If the type key isn't callable, then this is an array of objects, in which case
// we need to call InferRawDocType to correctly infer its type.
Array<InferRawDocType<Item>> :
IsSchemaTypeFromBuiltinClass<Item> extends true ?
ObtainRawDocumentPathType<Item, TypeKey>[] :
IsItRecordAndNotAny<Item> extends true ?
Expand All @@ -86,34 +78,49 @@ declare module 'mongoose' {
Array<InferRawDocType<Item>> :
ObtainRawDocumentPathType<Item, TypeKey>[]
>:
PathValueType extends StringSchemaDefinition ? PathEnumOrString<Options['enum']> :
IfEquals<PathValueType, Schema.Types.String> extends true ? PathEnumOrString<Options['enum']> :
IfEquals<PathValueType, String> extends true ? PathEnumOrString<Options['enum']> :
PathValueType extends NumberSchemaDefinition ? Options['enum'] extends ReadonlyArray<any> ? Options['enum'][number] : number :
IfEquals<PathValueType, Schema.Types.Number> extends true ? number :
PathValueType extends DateSchemaDefinition ? NativeDate :
IfEquals<PathValueType, Schema.Types.Date> extends true ? NativeDate :
PathValueType extends typeof Buffer | 'buffer' | 'Buffer' | typeof Schema.Types.Buffer ? Buffer :
PathValueType extends BooleanSchemaDefinition ? boolean :
IfEquals<PathValueType, Schema.Types.Boolean> extends true ? boolean :
PathValueType extends ObjectIdSchemaDefinition ? Types.ObjectId :
IfEquals<PathValueType, Types.ObjectId> extends true ? Types.ObjectId :
IfEquals<PathValueType, Schema.Types.ObjectId> extends true ? Types.ObjectId :
PathValueType extends 'decimal128' | 'Decimal128' | typeof Schema.Types.Decimal128 ? Types.Decimal128 :
IfEquals<PathValueType, Schema.Types.Decimal128> extends true ? Types.Decimal128 :
IfEquals<PathValueType, Types.Decimal128> extends true ? Types.Decimal128 :
IfEquals<PathValueType, Schema.Types.BigInt> extends true ? bigint :
IfEquals<PathValueType, BigInt> extends true ? bigint :
PathValueType extends 'bigint' | 'BigInt' | typeof Schema.Types.BigInt | typeof BigInt ? bigint :
PathValueType extends 'uuid' | 'UUID' | typeof Schema.Types.UUID ? Buffer :
IfEquals<PathValueType, Schema.Types.UUID> extends true ? Buffer :
PathValueType extends MapConstructor | 'Map' ? Map<string, ResolveRawPathType<Options['of']>> :
IfEquals<PathValueType, typeof Schema.Types.Map> extends true ? Map<string, ResolveRawPathType<Options['of']>> :
PathValueType extends ArrayConstructor ? any[] :
PathValueType extends typeof Schema.Types.Mixed ? any:
IfEquals<PathValueType, ObjectConstructor> extends true ? any:
IfEquals<PathValueType, {}> extends true ? any:
PathValueType extends typeof SchemaType ? PathValueType['prototype'] :
PathValueType extends Record<string, any> ? InferRawDocType<PathValueType> :
unknown;
PathValueType extends ReadonlyArray<infer Item> ?
IfEquals<Item, never, any[], Item extends Schema ?
Array<InferRawDocTypeFromSchema<Item>> :
Item extends Record<TypeKey, any> ?
Item[TypeKey] extends Function | String ?
ObtainRawDocumentPathType<Item, TypeKey>[] :
InferRawDocType<Item>[]:
IsSchemaTypeFromBuiltinClass<Item> extends true ?
ObtainRawDocumentPathType<Item, TypeKey>[] :
IsItRecordAndNotAny<Item> extends true ?
Item extends Record<string, never> ?
ObtainRawDocumentPathType<Item, TypeKey>[] :
Array<InferRawDocType<Item>> :
ObtainRawDocumentPathType<Item, TypeKey>[]
>:
PathValueType extends StringSchemaDefinition ? PathEnumOrString<Options['enum']> :
IfEquals<PathValueType, Schema.Types.String> extends true ? PathEnumOrString<Options['enum']> :
IfEquals<PathValueType, String> extends true ? PathEnumOrString<Options['enum']> :
PathValueType extends NumberSchemaDefinition ? Options['enum'] extends ReadonlyArray<any> ? Options['enum'][number] : number :
IfEquals<PathValueType, Schema.Types.Number> extends true ? number :
PathValueType extends DateSchemaDefinition ? NativeDate :
IfEquals<PathValueType, Schema.Types.Date> extends true ? NativeDate :
PathValueType extends typeof Buffer | 'buffer' | 'Buffer' | typeof Schema.Types.Buffer ? Buffer :
PathValueType extends BooleanSchemaDefinition ? boolean :
IfEquals<PathValueType, Schema.Types.Boolean> extends true ? boolean :
PathValueType extends ObjectIdSchemaDefinition ? Types.ObjectId :
IfEquals<PathValueType, Types.ObjectId> extends true ? Types.ObjectId :
IfEquals<PathValueType, Schema.Types.ObjectId> extends true ? Types.ObjectId :
PathValueType extends 'decimal128' | 'Decimal128' | typeof Schema.Types.Decimal128 ? Types.Decimal128 :
IfEquals<PathValueType, Schema.Types.Decimal128> extends true ? Types.Decimal128 :
IfEquals<PathValueType, Types.Decimal128> extends true ? Types.Decimal128 :
IfEquals<PathValueType, Schema.Types.BigInt> extends true ? bigint :
IfEquals<PathValueType, BigInt> extends true ? bigint :
PathValueType extends 'bigint' | 'BigInt' | typeof Schema.Types.BigInt | typeof BigInt ? bigint :
PathValueType extends 'uuid' | 'UUID' | typeof Schema.Types.UUID ? Buffer :
IfEquals<PathValueType, Schema.Types.UUID> extends true ? Buffer :
PathValueType extends MapConstructor | 'Map' ? Map<string, ResolveRawPathType<Options['of']>> :
IfEquals<PathValueType, typeof Schema.Types.Map> extends true ? Map<string, ResolveRawPathType<Options['of']>> :
PathValueType extends ArrayConstructor ? any[] :
PathValueType extends typeof Schema.Types.Mixed ? any:
IfEquals<PathValueType, ObjectConstructor> extends true ? any:
IfEquals<PathValueType, {}> extends true ? any:
PathValueType extends typeof SchemaType ? PathValueType['prototype'] :
PathValueType extends Record<string, any> ? InferRawDocType<PathValueType> :
unknown;
}