egg.js如何在不使用egg-mongoose情况下写model层
发现现有的例子都是像这样:
// {app_root}/app/model/user.js
module.exports = app => {
const mongoose = app.mongoose;
const Schema = mongoose.Schema;
const conn = app.mongooseDB.get('db1');
const UserSchema = new Schema({
userName: { type: String },
password: { type: String },
});
return conn.model('User', UserSchema);
}
当我不使用egg-mongoose
时,直接用egg-mongo
时,我想在model
层封装一些数据库操作方法:
// model/topic.js
module.exports = app => {
const mongo = app.mongo;
return {
async queryAllTopics(){
try {
let result = await mongo.topic.find();
} catch(err){
console.error(err);
}
},
async addTopic(topic){
try {
let result = await mongo.topic.insert(topic);
} catch(err){
console.error(err);
}
}
}
};
然后我在service
里去取
let result = await this.ctx.model.topic.addTopic(topic); return result;
发现是取不到这个this.ctx.model.topic
的,请问这种应该怎么处理呢?