From 4c11761a4af639f6dd103604a5679349407adaff Mon Sep 17 00:00:00 2001 From: oneWalker Date: Sat, 30 Oct 2021 15:04:25 +0800 Subject: [PATCH 1/3] feature:support two customized features --- app/extend/context.js | 7 ------- config/config.default.js | 2 ++ lib/mongoose.js | 22 +++++++++++++++++++--- 3 files changed, 21 insertions(+), 10 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/config/config.default.js b/config/config.default.js index 26ea6e2..fc805ef 100644 --- a/config/config.default.js +++ b/config/config.default.js @@ -14,4 +14,6 @@ exports.mongoose = { loadModel: true, app: true, agent: false, + baseDir:'model', + delegate:'model' }; diff --git a/lib/mongoose.js b/lib/mongoose.js index d47d220..83ae54d 100644 --- a/lib/mongoose.js +++ b/lib/mongoose.js @@ -11,7 +11,7 @@ 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} = app.config.mongoose; // compatibility if (!client && !clients && url) { @@ -48,6 +48,21 @@ module.exports = app => { app.mongoose.loadModel = () => loadModelToApp(app); + let ctx = app.context; + const DELEGATE = Symbol(`context#mongoose_${delegate}`); + Object.defineProperty(ctx,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 (loadModel) { app.beforeStart(() => { loadModelToApp(app); @@ -120,8 +135,9 @@ function createOneClient(config, app) { } function loadModelToApp(app) { - const dir = path.join(app.config.baseDir, 'app/model'); - app.loader.loadToApp(dir, 'model', { + const config = app.config.mongoose; + const dir = path.join(app.config.baseDir, 'app', config.baseDir); + app.loader.loadToApp(dir, config.delegate, { inject: app, caseStyle: 'upper', filter(model) { From 09f6547f7f037bd7cdd98f939619f3fda8c27b6d Mon Sep 17 00:00:00 2001 From: oneWalker Date: Sat, 30 Oct 2021 15:12:29 +0800 Subject: [PATCH 2/3] docs:add the examples in the`config` for new features --- README.md | 2 ++ config/config.default.js | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bbbcf96..5482ba3 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,8 @@ exports.mongoose = { }; // recommended exports.mongoose = { + //baseDir:'model', //models in `app/${model}` + //delegate:'model' //lood to `app[delegate]` client: { url: 'mongodb://127.0.0.1/example', options: {}, diff --git a/config/config.default.js b/config/config.default.js index fc805ef..950eb44 100644 --- a/config/config.default.js +++ b/config/config.default.js @@ -14,6 +14,6 @@ exports.mongoose = { loadModel: true, app: true, agent: false, - baseDir:'model', - delegate:'model' + baseDir:'model', //models in `app/${model}`,the deafault is `app/model` + delegate:'model' //lood to `app[delegate]`,the deafault is app.model }; From 24cfe78b4ae197f7ce30587501e76fdc7e877c2e Mon Sep 17 00:00:00 2001 From: oneWalker Date: Sat, 30 Oct 2021 15:37:39 +0800 Subject: [PATCH 3/3] test:`npm test` passes --- config/config.default.js | 4 ++-- lib/mongoose.js | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/config/config.default.js b/config/config.default.js index 950eb44..a3b1ad3 100644 --- a/config/config.default.js +++ b/config/config.default.js @@ -14,6 +14,6 @@ exports.mongoose = { loadModel: true, app: true, agent: false, - baseDir:'model', //models in `app/${model}`,the deafault is `app/model` - delegate:'model' //lood to `app[delegate]`,the deafault is app.model + baseDir: 'model', // models in `app/${model}`,the deafault is `app/model` + delegate: 'model', // lood to `app[delegate]`,the deafault is app.model }; diff --git a/lib/mongoose.js b/lib/mongoose.js index 83ae54d..e440099 100644 --- a/lib/mongoose.js +++ b/lib/mongoose.js @@ -11,7 +11,7 @@ let count = 0; const globalPlugins = []; module.exports = app => { - const { client, clients, url, options, defaultDB, customPromise, loadModel, plugins, delegate} = app.config.mongoose; + const { client, clients, url, options, defaultDB, customPromise, loadModel, plugins, delegate } = app.config.mongoose; // compatibility if (!client && !clients && url) { @@ -48,9 +48,9 @@ module.exports = app => { app.mongoose.loadModel = () => loadModelToApp(app); - let ctx = app.context; + const ctx = app.context; const DELEGATE = Symbol(`context#mongoose_${delegate}`); - Object.defineProperty(ctx,delegate, + Object.defineProperty(ctx, delegate, { get() { // context.model is different with app.model