fork说明
mongoose 更新到 5.1.4
egg-mongoose
Egg's mongoose plugin.
Install
$ npm i egg-mongoose-hb --save
Configuration
Change {app_root}/config/plugin.js
to enable egg-mongoose
plugin:
exportsmongoose = enable: true package: 'egg-mongoose';
Simple connection
Config
// {app_root}/config/config.default.jsexportsmongoose = url: 'mongodb://127.0.0.1/example' options: {};// recommendedexportsmongoose = client: url: 'mongodb://127.0.0.1/example' options: {} ;
Example
// {app_root}/app/model/user.jsmodule { const mongoose = appmongoose; const Schema = mongooseSchema; const UserSchema = userName: type: String password: type: String ; return mongoose;} // {app_root}/app/controller/user.jsexports { ctxbody = ctxmodelUser;}
Multiple connections
Config
// {app_root}/config/config.default.jsexportsmongoose = clients: // clientId, access the client instance by app.mongooseDB.get('clientId') db1: url: 'mongodb://127.0.0.1/example1' options: {} db2: url: 'mongodb://127.0.0.1/example2' options: {} ;
Example
// {app_root}/app/model/user.jsmodule { const mongoose = appmongoose; const Schema = mongooseSchema; const conn = appmongooseDB; const UserSchema = userName: type: String password: type: String ; return conn;} // {app_root}/app/model/book.jsmodule { const mongoose = appmongoose; const Schema = mongooseSchema; const conn = appmongooseDB; const BookSchema = name: type: String ; return conn;} // app/controller/user.jsexports { ctxbody = ctxmodelUser; // get data from db1} // app/controller/book.jsexports { ctxbody = ctxmodelBook; // get data from db2}
Default config
see config/config.default.js for more detail.
Multi-mongos support
// {app_root}/config/config.default.jsexportsmongoose = client: url: 'mongodb://mongosA:27501,mongosB:27501' options: mongos: true ;
Questions & Suggestions
Please open an issue here.
Contribution
If you are a contributor, follow CONTRIBUTING.