Open
Description
Prerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the bug has not already been reported
Mongoose version
6.8.0
Node.js version
16.15.1
MongoDB server version
6.0
Typescript version (if applicable)
4.9.4
Description
In version 6.8.0, when I set the timestamps option to true in a schema, the created model has properties with unknown types that will cause a lot of problems in implementing the application. However, when I switch back to version 6.7.5 the problem disappears.
Version 6.8.0:
(property) Users: Model<{
createdAt: NativeDate;
updatedAt: NativeDate;
} & {
email?: unknown;
password?: unknown;
name?: unknown;
}, {}, {
// This is a custom method in the schema
comparePassword: (this: Document<unknown, any, FlatRecord<{
email: string;
password: string;
name: string;
...
When switched back to version 6.7.5 without any modification:
(property) Users: Model<{
email: string;
password: string;
name: string;
}, {}, {
// This is a custom method in the schema
comparePassword: (this: Document<unknown, any, FlatRecord<{
email: string;
password: string;
name: string;
...
Steps to Reproduce
Considering you are using typescript:
- Install version 6.8.0 : npm i mongoose@6.8.0
- Create a schema with some properties and set the timestamps option to true.
- Create a connection using mongoose.createConnection.
- Create a model for the schema using the connection.model.
- Hover over the created model to see the type of the model.
const userSchema = new Schema(
{
email: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
name: {
type: String,
required: true,
},
},
{
methods: {
comparePassword: function (candidatePassword: string): Promise<boolean> {
const user = this;
return new Promise((resolve, reject) => {
compare(candidatePassword, user.password, (err, isMatch) => {
if (err) return reject(err);
return resolve(isMatch);
});
});
},
},
timestamps: true,
}
);
const connection = createConnection(config.authDatabaseURL);
const Models = {
Users: connection.model("users", userSchema),
};
Expected Behavior
(property) Users: Model<{
createdAt: NativeDate;
updatedAt: NativeDate;
} & {
email: string;
password: string;
name: string;
}, {}, {
// This is a custom method in the schema
comparePassword: (this: Document<unknown, any, FlatRecord<{
email: string;
password: string;
name: string;
...
Or:
(property) Users: Model<{
email: string;
password: string;
name: string;
}, {}, {
// This is a custom method in the schema
comparePassword: (this: Document<unknown, any, FlatRecord<{
email: string;
password: string;
name: string;
...