Skip to content

Commit

Permalink
fix: ctx.model extends app.model
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse committed Aug 17, 2018
1 parent 3a13efd commit a3ace2b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,13 @@ module.exports = app => {
configurable: false,
});

const DELEGATE = Symbol(`context#sequelize_${config.delegate}`);
Object.defineProperty(app.context, config.delegate, {
get() {
return app[config.delegate];
// 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[config.delegate]);
return this[DELEGATE];
},
configurable: false,
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"autod": "^3.0.1",
"egg": "^2.10.0",
"egg-bin": "^4.8.1",
"egg-mock": "^3.19.2",
"egg-mock": "^3.19.3",
"eslint": "^5.3.0",
"eslint-config-egg": "^7.0.0",
"mysql2": "^1.6.1"
Expand Down
1 change: 1 addition & 0 deletions test/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe('test/plugin.test.js', () => {
assert.ok(ctx.model.User);
assert.ok(ctx.model.Monkey);
assert.ok(ctx.model.Person);
assert.ok(ctx.model !== app.model);
});

it('model not load non Sequelize files', async function() {
Expand Down

0 comments on commit a3ace2b

Please sign in to comment.