Skip to content

Commit 5b8a93a

Browse files
committed
WIP: experiment with switching Schema -> AutoInferredSchema re: #14962 #14954
1 parent f6c7a6a commit 5b8a93a

16 files changed

+100
-121
lines changed

test/types/connection.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ expectType<Connection>(createConnection('mongodb://127.0.0.1:27017/test', { appN
99

1010
const conn = createConnection();
1111

12-
expectAssignable<Model<{ name: string }, any, any, any>>(conn.model('Test', new Schema<{ name: string }>({ name: { type: String } })));
12+
expectAssignable<Model<{ name: string }, any, any, any>>(conn.model('Test', new Schema<unknown, { name: string }>({ name: { type: String } })));
1313
expectType<Model<{ name: string }>>(conn.model<{ name: string }>('Test', new Schema({ name: { type: String } })));
1414

1515
expectType<Promise<Connection>>(conn.openUri('mongodb://127.0.0.1:27017/test'));
@@ -132,7 +132,7 @@ function schemaInstanceMethodsAndQueryHelpersOnConnection() {
132132
}
133133
type UserModel = Model<User, UserQueryHelpers, UserInstanceMethods> & UserStaticMethods;
134134

135-
const userSchema = new Schema<User, UserModel, UserInstanceMethods, UserQueryHelpers, any, UserStaticMethods>({
135+
const userSchema = new Schema<unknown, User, UserModel, UserInstanceMethods, UserQueryHelpers, any, UserStaticMethods>({
136136
name: String
137137
}, {
138138
statics: {

test/types/docArray.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ async function gh10293() {
77
arrayOfArray: string[][]; // <-- Array of Array
88
}
99

10-
const testSchema = new Schema<ITest>({
10+
const testSchema = new Schema<unknown, ITest>({
1111
name: {
1212
type: String,
1313
required: true

test/types/inferrawdoctype.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { InferRawDocType, Schema, AutoInferredSchema } from 'mongoose';
1+
import { InferRawDocType, Schema } from 'mongoose';
22
import { expectType, expectError } from 'tsd';
33

44
function gh14839() {
@@ -18,10 +18,10 @@ function gh14839() {
1818
type: Date,
1919
required: true
2020
},
21-
subdoc: new AutoInferredSchema({
21+
subdoc: new Schema({
2222
name: { type: String, required: true }
2323
}),
24-
docArr: [new AutoInferredSchema({ test: { type: String, required: true } })]
24+
docArr: [new Schema({ test: { type: String, required: true } })]
2525
};
2626

2727
type UserType = InferRawDocType<typeof schemaDefinition>;
@@ -51,13 +51,13 @@ function gh14954() {
5151
type: Date,
5252
required: true
5353
},
54-
subdoc: new AutoInferredSchema({
54+
subdoc: new Schema({
5555
name: { type: String, required: true },
56-
l2: new AutoInferredSchema({
56+
l2: new Schema({
5757
myProp: { type: Number, required: true }
5858
})
5959
}),
60-
docArr: [new AutoInferredSchema({ test: { type: String, required: true } })]
60+
docArr: [new Schema({ test: { type: String, required: true } })]
6161
};
6262

6363
type UserType = InferRawDocType<typeof schemaDefinition>;

test/types/maps.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ interface ITest {
77
map3: Map<string, number>
88
}
99

10-
const schema: Schema = new Schema<ITest>({
10+
const schema = new Schema<any, ITest>({
1111
map1: {
1212
type: Map,
1313
of: Number
@@ -50,7 +50,7 @@ function gh10575() {
5050
property3: string;
5151
}
5252

53-
const BaseSchema: Schema<IBase> = new Schema({ prop1: String, prop2: String });
53+
const BaseSchema: Schema<any, IBase> = new Schema<any, IBase>({ prop1: String, prop2: String });
5454

5555
const Model1Schema: Schema<IModel1> = BaseSchema.clone() as any;
5656
Model1Schema.add({ property1: Number, property2: Number });

test/types/methods.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ interface ITestMethods {
1010

1111
type ITestModel = Model<ITest, {}, ITestMethods>;
1212

13-
const TestSchema = new Schema<ITest, ITestModel, ITestMethods>({
13+
const TestSchema = new Schema<any, ITest, ITestModel, ITestMethods>({
1414
foo: { type: String, required: true }
1515
});
1616

test/types/middleware.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ function gh11480(): void {
137137
name: string;
138138
};
139139

140-
const UserSchema = new Schema<IUserSchema>({ name: { type: String } });
140+
const UserSchema = new Schema<any, IUserSchema>({ name: { type: String } });
141141

142142
UserSchema.pre('save', function(next) {
143143
expectNotType<any>(this);
@@ -152,7 +152,7 @@ function gh12583() {
152152
avatar?: string;
153153
}
154154

155-
const userSchema = new Schema<IUser>({
155+
const userSchema = new Schema<any, IUser>({
156156
name: { type: String, required: true },
157157
email: { type: String, required: true },
158158
avatar: String
@@ -172,7 +172,7 @@ function gh11257() {
172172
avatar?: string;
173173
}
174174

175-
const schema = new Schema<User>({
175+
const schema = new Schema<any, User>({
176176
name: { type: String, required: true },
177177
email: { type: String, required: true },
178178
avatar: String
@@ -211,7 +211,7 @@ function gh15242() {
211211
type DocumentValidatorThis = HydratedDocument<PostPersisted>;
212212
type QueryValidatorThis = Query<unknown, PostRecord>;
213213

214-
const PostSchema = new Schema<PostPersisted>({
214+
const PostSchema = new Schema<unknown, PostPersisted>({
215215
title: { type: String, required: true },
216216
postTime: {
217217
type: Date,

test/types/plugin.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { connection, HydratedDocument, Model, Query, Schema } from 'mongoose';
22

3-
function pluginVirtuals(schema: Schema<Test, any, any, any, TestVirtuals>): void {
3+
function pluginVirtuals(schema: Schema<any, Test, any, any, any, TestVirtuals>): void {
44
schema.virtual('fullName').get(function(this: TestDocument) {
55
return `${this.firstName} ${this.lastName}`;
66
});
@@ -9,19 +9,19 @@ function pluginVirtuals(schema: Schema<Test, any, any, any, TestVirtuals>): void
99
});
1010
}
1111

12-
function pluginQueryHelpers(schema: Schema<Test, any, any, TestQueryHelpers>): void {
12+
function pluginQueryHelpers(schema: Schema<any, Test, any, any, TestQueryHelpers>): void {
1313
schema.query.whereSomething = function() {
1414
return this.where({ name: 'something' });
1515
};
1616
}
1717

18-
function pluginMethods(schema: Schema<Test, any, TestInstanceMethods>): void {
18+
function pluginMethods(schema: Schema<any, Test, any, TestInstanceMethods>): void {
1919
schema.methods.doSomething = function() {
2020
return 'test';
2121
};
2222
}
2323

24-
function pluginStatics(schema: Schema<Test, TestModel, any, TestQueryHelpers, any, TestStaticMethods>): void {
24+
function pluginStatics(schema: Schema<any, Test, TestModel, any, TestQueryHelpers, any, TestStaticMethods>): void {
2525
schema.statics.findSomething = function() {
2626
return this.findOne().orFail().exec();
2727
};
@@ -54,7 +54,7 @@ interface TestQueryHelpers {
5454
whereSomething(this: TestQuery): this
5555
}
5656
type TestModel = Model<Test, TestQueryHelpers, TestInstanceMethods, TestVirtuals> & TestStaticMethods;
57-
const testSchema = new Schema<Test, TestModel, TestInstanceMethods, TestQueryHelpers, TestVirtuals, TestStaticMethods>({
57+
const testSchema = new Schema<any, Test, TestModel, TestInstanceMethods, TestQueryHelpers, TestVirtuals, TestStaticMethods>({
5858
firstName: { type: String, required: true },
5959
lastName: { type: String, required: true }
6060
});

test/types/querycursor.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Schema, model, Model, Types } from 'mongoose';
1+
import { Schema, model, Model, Types, InferRawDocTypeFromSchema, InferRawDocType } from 'mongoose';
22
import { expectType } from 'tsd';
33

4-
const schema = new Schema({ name: { type: 'String' } });
4+
const schema = new Schema({ name: { type: String } });
55

66
const Test = model('Test', schema);
77

test/types/queryhelpers.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ interface ProjectQueryHelpers {
1313
byName(this: ProjectModelQuery, name: string): ProjectModelQuery;
1414
}
1515

16-
const schema = new Schema<Project, ProjectModelType, {}, ProjectQueryHelpers>({
16+
const schema = new Schema<any, Project, ProjectModelType, {}, ProjectQueryHelpers>({
1717
name: { type: String, required: true },
1818
stars: { type: Number, required: true }
1919
});

test/types/subdocuments.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ async function gh10597(): Promise<void> {
4747
name: string;
4848
events: IGameEventDocument[]
4949
}
50-
const schema = new Schema<IGameDocument>({ name: String, events: [{ description: String }] });
50+
const schema = new Schema<any, IGameDocument>({ name: String, events: [{ description: String }] });
5151

5252
const GameModel = model<IGameDocument>('Game', schema);
5353

@@ -66,7 +66,7 @@ function gh10674() {
6666

6767
type FooModel = Model<Foo>;
6868

69-
const FooSchema = new Schema<Foo, FooModel, Foo>(
69+
const FooSchema = new Schema<any, Foo, FooModel, Foo>(
7070
{
7171
bar: { type: String },
7272
schedule: {

test/types/virtuals.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ interface PetVirtuals {
2121
owner: IPerson;
2222
}
2323

24-
const personSchema = new Schema<IPerson & Document, Model<IPerson & Document>, IPerson>({
24+
const personSchema = new Schema<unknown, IPerson & Document, Model<IPerson & Document>, IPerson>({
2525
_id: { type: Number, required: true },
2626
firstName: { type: String, required: true },
2727
lastName: { type: String, required: true }
2828
});
2929

30-
const petSchema = new Schema<IPet & Document, Model<IPet & Document>, IPet>({
30+
const petSchema = new Schema<unknown, IPet & Document, Model<IPet & Document>, IPet>({
3131
name: { type: String, required: true },
3232
ownerId: { type: Number, required: true },
3333
isDeleted: { type: Boolean, default: false }
@@ -78,7 +78,7 @@ const Pet = model<IPet>('Pet', petSchema);
7878
})();
7979

8080
function gh11543() {
81-
const personSchema = new Schema<IPerson, Model<IPerson, {}, {}, PetVirtuals>, {}, {}, PetVirtuals>({
81+
const personSchema = new Schema<unknown, IPerson, Model<IPerson, {}, {}, PetVirtuals>, {}, {}, PetVirtuals>({
8282
_id: { type: Number, required: true },
8383
firstName: { type: String, required: true },
8484
lastName: { type: String, required: true }

types/connection.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,11 @@ declare module 'mongoose' {
194194
TSchema> & ObtainSchemaGeneric<TSchema, 'TStaticMethods'>;
195195
model<T, U, TQueryHelpers = {}>(
196196
name: string,
197-
schema?: Schema<T, any, any, TQueryHelpers, any, any, any>,
197+
schema?: Schema<any, T, any, any, TQueryHelpers, any, any, any>,
198198
collection?: string,
199199
options?: CompileModelOptions
200200
): U;
201-
model<T>(name: string, schema?: Schema<T, any, any>, collection?: string, options?: CompileModelOptions): Model<T>;
201+
model<T>(name: string, schema?: Schema<any, T, any, any>, collection?: string, options?: CompileModelOptions): Model<T>;
202202

203203
/** Returns an array of model names created on this connection. */
204204
modelNames(): Array<string>;

types/index.d.ts

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ declare module 'mongoose' {
9797
TSchema
9898
> & ObtainSchemaGeneric<TSchema, 'TStaticMethods'>;
9999

100-
export function model<T>(name: string, schema?: Schema<T, any, any> | Schema<T & Document, any, any>, collection?: string, options?: CompileModelOptions): Model<T>;
100+
export function model<T>(name: string, schema?: Schema<any, T, any, any>, collection?: string, options?: CompileModelOptions): Model<T>;
101101

102102
export function model<T, U, TQueryHelpers = {}>(
103103
name: string,
104-
schema?: Schema<T, any, any, TQueryHelpers, any, any, any>,
104+
schema?: Schema<any, T, U, any, TQueryHelpers, any, any, any>,
105105
collection?: string,
106106
options?: CompileModelOptions
107107
): U;
@@ -256,9 +256,9 @@ declare module 'mongoose' {
256256
TInstanceMethods,
257257
TQueryHelpers,
258258
TVirtuals,
259-
TStaticMethods> = (schema: Schema<DocType, M, TInstanceMethods, TQueryHelpers, TVirtuals, TStaticMethods>, opts?: any) => void;
259+
TStaticMethods> = (schema: Schema<any, DocType, M, TInstanceMethods, TQueryHelpers, TVirtuals, TStaticMethods>, opts?: any) => void;
260260

261-
export class AutoInferredSchema<
261+
export class Schema<
262262
SchemaDef = unknown,
263263
RawDocType = InferRawDocType<SchemaDef>,
264264
TModelType = Model<RawDocType, any, any, any>,
@@ -275,35 +275,15 @@ declare module 'mongoose' {
275275
ResolveSchemaOptions<TSchemaOptions>
276276
>,
277277
THydratedDocumentType = HydratedDocument<FlatRecord<DocType>, TVirtuals & TInstanceMethods>
278-
> extends Schema<RawDocType, TModelType, TInstanceMethods, TQueryHelpers, TVirtuals, TStaticMethods, TSchemaOptions, DocType, THydratedDocumentType> {
279-
constructor(definition: SchemaDef, options?: SchemaOptions<FlatRecord<DocType>, TInstanceMethods, TQueryHelpers, TStaticMethods, TVirtuals, THydratedDocumentType> | ResolveSchemaOptions<TSchemaOptions>);
280-
}
281-
282-
export class Schema<
283-
RawDocType = any,
284-
TModelType = Model<RawDocType, any, any, any>,
285-
TInstanceMethods = {},
286-
TQueryHelpers = {},
287-
TVirtuals = {},
288-
TStaticMethods = {},
289-
TSchemaOptions = DefaultSchemaOptions,
290-
DocType extends ApplySchemaOptions<
291-
ObtainDocumentType<DocType, RawDocType, ResolveSchemaOptions<TSchemaOptions>>,
292-
ResolveSchemaOptions<TSchemaOptions>
293-
> = ApplySchemaOptions<
294-
ObtainDocumentType<any, RawDocType, ResolveSchemaOptions<TSchemaOptions>>,
295-
ResolveSchemaOptions<TSchemaOptions>
296-
>,
297-
THydratedDocumentType = HydratedDocument<FlatRecord<DocType>, TVirtuals & TInstanceMethods, {}, TVirtuals>
298278
>
299279
extends events.EventEmitter {
300280
/**
301281
* Create a new schema
302282
*/
303-
constructor(definition?: SchemaDefinition<SchemaDefinitionType<RawDocType>, RawDocType, THydratedDocumentType> | DocType, options?: SchemaOptions<FlatRecord<DocType>, TInstanceMethods, TQueryHelpers, TStaticMethods, TVirtuals, THydratedDocumentType> | ResolveSchemaOptions<TSchemaOptions>);
283+
constructor(definition: SchemaDef, options?: SchemaOptions<FlatRecord<DocType>, TInstanceMethods, TQueryHelpers, TStaticMethods, TVirtuals, THydratedDocumentType> | ResolveSchemaOptions<TSchemaOptions>);
304284

305285
/** Adds key path / schema type pairs to this schema. */
306-
add(obj: SchemaDefinition<SchemaDefinitionType<RawDocType>, RawDocType> | Schema, prefix?: string): this;
286+
add(obj: AnyObject, prefix?: string): this;
307287

308288
/**
309289
* Add an alias for `path`. This means getting or setting the `alias`
@@ -371,7 +351,7 @@ declare module 'mongoose' {
371351
methods: AddThisParameter<TInstanceMethods, THydratedDocumentType> & AnyObject;
372352

373353
/** The original object passed to the schema constructor */
374-
obj: SchemaDefinition<SchemaDefinitionType<RawDocType>, RawDocType>;
354+
obj: SchemaDef;
375355

376356
/** Returns a new schema that has the `paths` from the original schema, minus the omitted ones. */
377357
omit<T = this>(paths: string[], options?: SchemaOptions): T;

0 commit comments

Comments
 (0)