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

Support to define mode by class style #35

Open
wants to merge 3 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
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict';

const sequelize = require('sequelize');

module.exports = sequelize;

12 changes: 12 additions & 0 deletions lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand Down
35 changes: 35 additions & 0 deletions test/fixtures/apps/model-app/app/model/monkey_es6.js
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 2 additions & 0 deletions test/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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* () {
Expand All @@ -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');
});
});

Expand Down