From 457c93c6b9c31620debbe0d843f1106e46cca836 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=8B=E5=AF=92=E5=86=AC?= Date: Thu, 3 Dec 2020 01:02:05 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8F=AF=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E9=A1=B9=EF=BC=9A=E6=8C=82=E8=BD=BD=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=20baseDir=20=E5=8F=8A=20model=E5=90=8D=E7=A7=B0=20delegate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/extend/context.js | 7 --- index.d.ts | 10 +++- lib/mongoose.js | 62 ++++++++++++++++---- test/fixtures/apps/mongoose-loadModel/app.js | 2 +- 4 files changed, 58 insertions(+), 23 deletions(-) delete mode 100644 app/extend/context.js 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); }; From 65dee876268bfc0efb78029aa51e1efb4d24261e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=8B=E5=AF=92=E5=86=AC?= Date: Thu, 3 Dec 2020 01:37:37 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=96=87=E6=A1=A3=E6=9B=B4=E6=96=B0=20base?= =?UTF-8?q?Dir=20=E4=B8=8E=20delegate=20=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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