task-svr/src/decorators/dbconn.ts
2024-01-18 19:13:11 +08:00

20 lines
590 B
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { mongoose } from '@typegoose/typegoose'
/**
* 为model指定数据库连接
* @param {string} name 数据库连接名字, 在config中必须要有对应的配置 比如main 则必须要有 db_main
* */
export function dbconn(name?: string) {
return target => {
name = name || 'main'
const dbName = ('db_' + name).toUpperCase()
const url = process.env[dbName]
target['db'] = mongoose.createConnection(url, {
// useNewUrlParser: true,
// useCreateIndex: true,
// useFindAndModify: false,
// useUnifiedTopology: true,
})
}
}