Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 添加挂载目录 baseDir 及 model名称 delegate 配置项 #44

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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',
},
};
```
Expand Down Expand Up @@ -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
Expand Down
7 changes: 0 additions & 7 deletions app/extend/context.js

This file was deleted.

10 changes: 7 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ declare module 'egg' {

type MongooseConfig = {
url: string,
options?: mongoose.ConnectionOptions
options?: mongoose.ConnectionOptions,
delegate: string,
baseDir: string
};

// extend app
Expand All @@ -36,8 +38,10 @@ declare module 'egg' {
client?: MongooseConfig,
clients?: {
[key: string]: MongooseConfig
}
},
delegate: string,
baseDir: string
};
}

}
}
62 changes: 50 additions & 12 deletions lib/mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}

Expand Down Expand Up @@ -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);
});
}
};
Expand Down Expand Up @@ -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;
},
});
}
}
2 changes: 1 addition & 1 deletion test/fixtures/apps/mongoose-loadModel/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

module.exports = app => {
app.mymongoose = app.mongooseDB.createInstance(app.config.mymongoose);
app.mongoose.loadModel();
app.mongoose.loadModel(app.config.mymongoose);
};