【问题标题】:Mongoose and Typescript - Link a model to a specific mongoose connectionMongoose 和 Typescript - 将模型链接到特定的 mongoose 连接
【发布时间】:2016-10-03 19:25:26
【问题描述】:

我在 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 连接...

我不知道该怎么做......

【问题讨论】:

标签: javascript node.js mongoose typescript


【解决方案1】:

我找到了自己的路……

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

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

【讨论】:

    猜你喜欢
    • 2018-07-10
    • 2018-11-30
    • 2019-03-14
    • 2016-01-28
    • 2018-11-05
    • 1970-01-01
    • 2018-08-01
    • 2022-08-08
    • 2016-11-27
    相关资源
    最近更新 更多