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..46f5826 100644 --- a/lib/loader.js +++ b/lib/loader.js @@ -64,6 +64,18 @@ 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) { + klass.app = app; + 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..b1021bb --- /dev/null +++ b/test/fixtures/apps/model-app/app/model/monkey_es6.js @@ -0,0 +1,35 @@ +'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 findUser() { + return this.app.model.User.find({ id: 1 }); + } +} + +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'); }); });