From fbbbce6fff139f6fc45778caa89648e79a0d514f Mon Sep 17 00:00:00 2001 From: jiangq Date: Fri, 22 Dec 2017 16:15:34 +0800 Subject: [PATCH 1/3] support to define models by es6 class style --- README.md | 34 ++++++++++++++++++ index.js | 6 ++++ lib/loader.js | 11 ++++++ .../apps/model-app/app/model/monkey_es6.js | 36 +++++++++++++++++++ test/plugin.test.js | 2 ++ 5 files changed, 89 insertions(+) create mode 100644 index.js create mode 100644 test/fixtures/apps/model-app/app/model/monkey_es6.js diff --git a/README.md b/README.md index b94bc40..7450703 100644 --- a/README.md +++ b/README.md @@ -123,6 +123,40 @@ module.exports = app => { return User; }; +// or with es6 + +const { Sequelize, Model } = require('egg-sequelize'); +const { STRING, INTEGER, DATE } = Sequelize; + +class User extends Model { + static get modelOptions() { + const modelName = 'user'; + const attributes = { + login: STRING, + name: STRING(30), + password: STRING(32), + age: INTEGER, + last_sign_in_at: DATE, + created_at: DATE, + updated_at: DATE, + }; + const options = {}; + return { modelName, attributes, options }; + } + + async logSignin() { + await this.update({ last_sign_in_at: new Date() }); + } + + static findByLogin(login) { + return this.findOne({ login }); + } +} + +module.exports = User; + + + ``` Now you can use it in your controller: diff --git a/index.js b/index.js new file mode 100644 index 0000000..bed55dd --- /dev/null +++ b/index.js @@ -0,0 +1,6 @@ +'use strict'; + +const sequelize = require('sequelize'); + +module.exports = sequelize; + diff --git a/lib/loader.js b/lib/loader.js index 735449e..ef56d31 100644 --- a/lib/loader.js +++ b/lib/loader.js @@ -64,6 +64,17 @@ function loadModel(app) { see: http://docs.sequelizejs.com/manual/tutorial/models-definition.html#expansion-of-models`); } } + + if ('modelOptions' in klass) { + const attributes = klass.modelOptions.attributes || {}; + const options = klass.modelOptions.options || {}; + options.sequelize = app.model; + options.modelName = klass.modelOptions.modelName || name; + klass.init(attributes, options); + if ('sequelize' in klass) { + app.model[name] = klass; + } + } } for (const name of Object.keys(app[MODELS])) { diff --git a/test/fixtures/apps/model-app/app/model/monkey_es6.js b/test/fixtures/apps/model-app/app/model/monkey_es6.js new file mode 100644 index 0000000..1298276 --- /dev/null +++ b/test/fixtures/apps/model-app/app/model/monkey_es6.js @@ -0,0 +1,36 @@ +'use strict'; +const { Sequelize, Model } = require('../../../../../../index'); +const { STRING, INTEGER, DATE } = Sequelize; + +class MonkeyEs6 extends Model { + static get modelOptions() { + return { + modelName: 'monkey', + attributes: { + name: { + type: STRING, + allowNull: false, + }, + user_id: INTEGER, + created_at: DATE, + updated_at: DATE, + }, + options: { + tableName: 'the_monkeys_se6', + + classMethods: { + }, + + instanceMethods: { + }, + }, + }; + } + + static async findUser() { + const user = await this.app.model.User.find({ id: 1 }); + return user; + } +} + +module.exports = MonkeyEs6; diff --git a/test/plugin.test.js b/test/plugin.test.js index c5953f3..ffc8a0b 100644 --- a/test/plugin.test.js +++ b/test/plugin.test.js @@ -28,6 +28,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.MonkeyEs6); }); it('model not load non Sequelize files', function* () { @@ -41,6 +42,7 @@ describe('test/plugin.test.js', () => { assert(app.model.Person.tableName === 'people'); assert(app.model.User.tableName === 'users'); assert(app.model.Monkey.tableName === 'the_monkeys'); + assert(app.model.MonkeyEs6.tableName === 'the_monkeys_se6'); }); }); From bbcc2e7613f0217f665512a19d66cd9a45298fef Mon Sep 17 00:00:00 2001 From: jiangq Date: Fri, 22 Dec 2017 16:36:50 +0800 Subject: [PATCH 2/3] add app instance to mode class --- lib/loader.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/loader.js b/lib/loader.js index ef56d31..46f5826 100644 --- a/lib/loader.js +++ b/lib/loader.js @@ -72,6 +72,7 @@ see: http://docs.sequelizejs.com/manual/tutorial/models-definition.html#expansio options.modelName = klass.modelOptions.modelName || name; klass.init(attributes, options); if ('sequelize' in klass) { + klass.app = app; app.model[name] = klass; } } From f61358d78982e79947c944b46a8c2fc917aa3e0b Mon Sep 17 00:00:00 2001 From: jiangq Date: Fri, 22 Dec 2017 17:18:07 +0800 Subject: [PATCH 3/3] remove async for test --- test/fixtures/apps/model-app/app/model/monkey_es6.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/fixtures/apps/model-app/app/model/monkey_es6.js b/test/fixtures/apps/model-app/app/model/monkey_es6.js index 1298276..b1021bb 100644 --- a/test/fixtures/apps/model-app/app/model/monkey_es6.js +++ b/test/fixtures/apps/model-app/app/model/monkey_es6.js @@ -27,9 +27,8 @@ class MonkeyEs6 extends Model { }; } - static async findUser() { - const user = await this.app.model.User.find({ id: 1 }); - return user; + static findUser() { + return this.app.model.User.find({ id: 1 }); } }