1.0.10 • Published 8 years ago
@gongt/egg-super-mongo v1.0.10
@gongt/egg-super-mongo
Egg's MongoDB plugin.
Install
$ npm i @gongt/egg-super-mongo --save
Usage
// {app_root}/config/plugin.js
exports.superMongo = {
enable: true,
package: '@gongt/egg-super-mongo',
};
Configuration
// {app_root}/config/config.default.js
exports.mongoose = {
client: {
url: 'mongodb://127.0.0.1/example',
},
clients: {
connection_a: {
url: 'mongodb://172.0.0.1/a',
},
connection_b: {
url: 'mongodb://192.168.1.1/b',
}
},
app: true,
agent: false,
allowOverwride: true,
};
see config/config.default.ts for more detail.
Example
// app/super-model/user.js
import {EggMongoose} from "@gongt/egg-super-mongo";
import {Schema} from "mongoose";
export class UserModel extends EggMongoose {
get connection() {
return "connection_a";
}
get schema() {
return new Schema({
userName: { type: String },
password: { type: String }
});
}
getUserById(id){
this.__model.findOne({ _id: id }); // ...
}
}
Example (es5)
// app/super-model/user.js
module.exports = app => {
const EggMongoose = app.SuperMongo;
return class UserModel extends EggMongoose {
/* ...... */
};
}
// app/controller/user.js
exports.index = async function (ctx) {
ctx.body = await ctx.model.user.getUserById(""); // you should use camel case to access mongoose model
}