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: {

0 commit comments

Comments
 (0)