简体   繁体   English

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

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

I defined a Model in a ts file. 我在ts文件中定义了一个模型。 I would like to use a specific mongoose connection (not the default one) with that model. 我想对该模型使用特定的猫鼬连接(不是默认的)。 How can I associate my model to the connection ? 如何将我的模型与连接关联?

Excerpt form my TS file : 摘录我的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);

In my server.ts file : 在我的server.ts文件中:

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

Nothing happens when I use my supplier model to find data. 当我使用供应商模型查找数据时,没有任何反应。 Obviously, I need to bind it to my database_2 connection ... 显然,我需要将其绑定到我的database_2连接上。

I am not sure about the way to that.... 我不确定这样做的方式。

I found out my way... 我发现了自己的路...

I export a function that returns the model and use my connection as a parameter... 我导出一个返回模型的函数,并使用我的连接作为参数...

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