问题描述
egg-mongoose应该在哪一步与数据库建立连接?
相关代码
我下边的代码如果不调用connect方法,就没法进行数据库的相关操作。找了一些demo想学习下,发现代码中并没有在model里调用connect方法,我也没找到具体是在哪里跟数据库连接的,所以比较困惑,想请教下各位大佬egg-mongoose的正确使用方法
app/model/Test.js
const mongoose = app.mongoose
//mongoose.connect('mongodb://127.0.0.1:27017/test')
const Schema = mongoose.Schema
const UserSchema = new Schema({
name: {
type: String,
},
})
return mongoose.model('Test', UserSchema, 'test')
config.default.js
config.mongoose = {
client: {
url: 'mongodb://127.0.0.1/test',
options: {},
}
}
plugin.js
exports.mongoose = {
enable: true,
package: 'egg-mongoose',
}
在
egg
中,一切皆配置。egg-mongose
是在mongose
基础上做了封装,将它封装成了egg
的插件其实我有点没理解你这句话的意思。
connect
方法你得在mongoose
包里面找,在node-modules
中找到egg-mongoose/lib/mongoose
,然后再往下找