diff --git a/README.md b/README.md index bbbcf96..73b1c29 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,8 @@ exports.mongoose = { options: {}, // mongoose global plugins, expected a function or an array of function and options plugins: [createdPlugin, [updatedPlugin, pluginOptions]], + delegate: 'model', + baseDir: 'model', }; // recommended exports.mongoose = { @@ -57,6 +59,8 @@ exports.mongoose = { options: {}, // mongoose global plugins, expected a function or an array of function and options plugins: [createdPlugin, [updatedPlugin, pluginOptions]], + delegate: 'model', + baseDir: 'model', }, }; ``` @@ -96,11 +100,15 @@ exports.mongoose = { url: 'mongodb://127.0.0.1/example1', options: {}, // client scope plugin array - plugins: [] + plugins: [], + delegate: 'model1', + baseDir: 'model1', }, db2: { url: 'mongodb://127.0.0.1/example2', options: {}, + delegate: 'model2', + baseDir: 'model2', }, }, // public scope plugin array diff --git a/app/extend/context.js b/app/extend/context.js deleted file mode 100644 index 0bf2077..0000000 --- a/app/extend/context.js +++ /dev/null @@ -1,7 +0,0 @@ -'use strict'; - -module.exports = { - get model() { - return this.app.model; - }, -}; diff --git a/index.d.ts b/index.d.ts index e069a6f..979dd07 100644 --- a/index.d.ts +++ b/index.d.ts @@ -13,7 +13,9 @@ declare module 'egg' { type MongooseConfig = { url: string, - options?: mongoose.ConnectionOptions + options?: mongoose.ConnectionOptions, + delegate: string, + baseDir: string }; // extend app @@ -36,8 +38,10 @@ declare module 'egg' { client?: MongooseConfig, clients?: { [key: string]: MongooseConfig - } + }, + delegate: string, + baseDir: string }; } -} \ No newline at end of file +} diff --git a/lib/mongoose.js b/lib/mongoose.js index d47d220..1e7054d 100644 --- a/lib/mongoose.js +++ b/lib/mongoose.js @@ -11,13 +11,15 @@ let count = 0; const globalPlugins = []; module.exports = app => { - const { client, clients, url, options, defaultDB, customPromise, loadModel, plugins } = app.config.mongoose; + const { client, clients, url, options, defaultDB, customPromise, loadModel, plugins, delegate, baseDir } = app.config.mongoose; // compatibility if (!client && !clients && url) { app.config.mongoose.client = { url, options, + delegate, + baseDir, }; } @@ -46,11 +48,11 @@ module.exports = app => { /* deprecated, next primary version remove */ app.__mongoose = mongoose; - app.mongoose.loadModel = () => loadModelToApp(app); + app.mongoose.loadModel = config => loadDatabase(app, config); if (loadModel) { app.beforeStart(() => { - loadModelToApp(app); + loadDatabase(app, app.config.mongoose); }); } }; @@ -119,13 +121,49 @@ function createOneClient(config, app) { return db; } -function loadModelToApp(app) { - const dir = path.join(app.config.baseDir, 'app/model'); - app.loader.loadToApp(dir, 'model', { - inject: app, - caseStyle: 'upper', - filter(model) { - return typeof model === 'function' && model.prototype instanceof app.mongoose.Model; - }, - }); +function loadDatabase(app, config = {}) { + const { client, clients } = config; + + if (!client && !clients) { + loadModelToApp(app, config); + } else if (!clients) { + loadModelToApp(app, client); + } else { + for (const i in clients) { + loadModelToApp(app, clients[i]); + } + } +} + +function loadModelToApp(app, config = {}) { + const context = app.context; + const delegate = config.delegate || 'model'; + const baseDir = config.baseDir || 'model'; + + if (!context[delegate]) { + const DELEGATE = Symbol(`context#mongoose_${delegate}`); + Object.defineProperty(context, delegate, { + get() { + // context.model is different with app.model + // so we can change the properties of ctx.model.xxx + if (!this[DELEGATE]) { + this[DELEGATE] = Object.create(app[delegate]); + this[DELEGATE].ctx = this; + } + return this[DELEGATE]; + }, + configurable: true, + }); + } + + if (!app[delegate]) { + const dir = path.join(app.baseDir, 'app', baseDir); + app.loader.loadToApp(dir, delegate, { + inject: app, + caseStyle: 'upper', + filter(model) { + return typeof model === 'function' && model.prototype instanceof app.mongoose.Model; + }, + }); + } } diff --git a/test/fixtures/apps/mongoose-loadModel/app.js b/test/fixtures/apps/mongoose-loadModel/app.js index 8d0c1bb..7e7c253 100644 --- a/test/fixtures/apps/mongoose-loadModel/app.js +++ b/test/fixtures/apps/mongoose-loadModel/app.js @@ -2,5 +2,5 @@ module.exports = app => { app.mymongoose = app.mongooseDB.createInstance(app.config.mymongoose); - app.mongoose.loadModel(); + app.mongoose.loadModel(app.config.mymongoose); };