简体   繁体   English

Mongoose typescript model 创建多个连接

[英]Mongoose typescript model creation with multiple connections

I'm trying to create a model while using multiple connections ( following the docs ) but I get the following error:我正在尝试在使用多个连接( 遵循文档)时创建 model 但我收到以下错误:

TS2345: Argument of type 'Schema<Document<any>, Model<Document<any>>>' is not assignable to parameter of
type 'Schema<UserInterface, UserModelInterface>'.
Types of property 'methods' are incompatible.
... (a bunch of 'is not assignable - is missing the following properties' errors follow) ...

My code looks like this:我的代码如下所示:

database.ts数据库.ts

import mongoose from "mongoose";

// const mongo = mongoose.connection(process.env.DB_URI!, {
const mongo = mongoose.createConnection(process.env.DB_URI!, {
    useUnifiedTopology: true,
    useNewUrlParser: true,
    useFindAndModify: false,
    useCreateIndex: true
})

export default mongo

UserModel.ts用户模型.ts

import mongoose, {Model} from 'mongoose';
import bcrypt from 'bcrypt'
import database from "../database";

interface UserDocumentInterface extends mongoose.Document {
    firstname: string
    lastname: string
    ...
}
    
interface UserInterface extends UserDocumentInterface {
    // Instance methods
    hashPassword(password: string): Promise<string>
    ...
}
    
export interface UserModelInterface extends Model<UserInterface> {
    // Static methods
    // Currently empty
}

const UserSchema = new mongoose.Schema({
    firstname: {
        type: String,
        required: true,
        trim: true
    },
    lastname: {
        type: String,
        required: true,
        index: true,
        trim: true,
    },
    ...
})

// Methods definitions
UserSchema.methods.hashPassword = async function(password) {
    return await bcrypt.hash(password, 10);
}
...

// const User: UserModelInterface = mongoose.model<UserInterface, UserModelInterface>('User', UserSchema)
const User = database.model<UserInterface, UserModelInterface>('User', UserSchema)
export default User

The error appears on UserSchema in const User = database.model<UserInterface, UserModelInterface>('User', UserSchema)错误出现在const User = database.model<UserInterface, UserModelInterface>('User', UserSchema) UserSchema

What I don't understand is why this line works without issues我不明白的是为什么这条线没有问题

const User: UserModelInterface = mongoose.model<UserInterface, UserModelInterface>('User', UserSchema)

but this one throws an error但这会引发错误

const User = database.model<UserInterface, UserModelInterface>('User', UserSchema)

when the only thing that changes is the connection type, mongoose.createConnection() as opposed to mongoose default connection当唯一改变的是连接类型时, mongoose.createConnection()而不是 mongoose 默认连接

Turns out that as of version mongoose 5.11.11 there are some conflicts with @types/mongoose that break class inheritance when using typescript ( see this issue ). Turns out that as of version mongoose 5.11.11 there are some conflicts with @types/mongoose mongoose that break class inheritance when using typescript ( see this issue ). I also found the solution to my problem in this discussion , it revolves around moving the Document and Model interface from the model to the schema constructor.我还在这个讨论中找到了我的问题的解决方案,它围绕将 Document 和 Model 接口从 model 移动到模式构造函数。

In practice it means turning this在实践中,这意味着转动这个

const UserSchema = new mongoose.Schema({
    firstname: {
        type: String,
        required: true,
        trim: true
    },
    lastname: {
        type: String,
        required: true,
        index: true,
        trim: true,
    },
    ...
})
    
const User = database.model<UserInterface, UserModelInterface>('User', UserSchema)
export default User

into this进入这个

const UserSchema = new mongoose.Schema<UserInterface, UserModelInterface>({
    firstname: {                            // INTERFACES GO HERE 
        type: String,
        required: true,
        trim: true
    },
    lastname: {
        type: String,
        required: true,
        index: true,
        trim: true,
    },
    ...
})
                // INSTEAD OF HERE
const User = database.model('User', UserSchema)
export default User

To me this looks like a temporary fix, the issue will probably affect only v5.11.11 as it's being patched in 5.11.12 so I suggest visiting the links above to see the issue status before using this suggestion.对我来说,这看起来像是一个临时修复,该问题可能只会影响 v5.11.11 ,因为它正在 5.11.12 中进行修补,因此我建议在使用此建议之前访问上面的链接以查看问题状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM