繁体   English   中英

猫鼬和打字稿-将模型链接到特定的猫鼬连接

[英]Mongoose and Typescript - Link a model to a specific mongoose connection

我在ts文件中定义了一个模型。 我想对该模型使用特定的猫鼬连接(不是默认的)。 如何将我的模型与连接关联?

摘录我的TS文件:

export interface iSuppliers extends mongoose.Document {
suppliers: string[];
fields: number[];  
}

export const supplierSchema = new mongoose.Schema({
    suppliers: {type:[String], required: true},
    fields: [Number]})
.index({suppliers: 1}); // Additional index

export const supplier = mongoose.model<iSuppliers>('supplier', supplierSchema);

在我的server.ts文件中:

import {supplier} from '....';
....
let database_2 = mongoose.createConnection(....);

当我使用供应商模型查找数据时,没有任何反应。 显然,我需要将其绑定到我的database_2连接上。

我不确定这样做的方式。

我发现了自己的路...

我导出一个返回模型的函数,并使用我的连接作为参数...

export function importedGameDatabase(mongooseConnection: mongoose.connection) { return mongooseConnection.model<iImportedGames>('importedGame', importedGamesSchema); } 

暂无
暂无

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

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