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

Config baseDir support array #89

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
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface EggSequelizeOptions extends sequelize.Options {
/**
* load models from `app/model/*.js`
*/
baseDir?: string;
baseDir?: string | Array<string>;

/**
* ignore `app/${baseDir}/index.js` when load models, support glob and array
Expand Down
6 changes: 5 additions & 1 deletion lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ module.exports = app => {
configurable: true,
});

const modelDir = path.join(app.baseDir, 'app', config.baseDir);
if (!Array.isArray(config.baseDir)) {
config.baseDir = [ config.baseDir ];
}

const modelDir = config.baseDir.map(dir => path.join(app.baseDir, 'app', dir));

const models = [];
const target = Symbol(config.delegate);
Expand Down
45 changes: 45 additions & 0 deletions test/base-dir.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';

const assert = require('assert');
const mm = require('egg-mock');

describe('test/base-dir.test.js', () => {
let app;

before(() => {
app = mm.app({
baseDir: 'apps/base-dir',
});
return app.ready();
});
before(() => app.model.sync({ force: true }));

after(mm.restore);

describe('Base', () => {
it('sequelize init success', () => {
assert.ok(app.model);
assert.ok(app.model.User);
assert.ok(app.model.Post);
});

it('ctx model property getter', () => {
const ctx = app.mockContext();
assert.ok(ctx.model);
assert.ok(ctx.model.User);
assert.ok(ctx.model.Post);
});
});

describe('Associate', () => {
it('ctx model associate init success', () => {
const ctx = app.mockContext();
assert.ok(ctx.model);
assert.ok(ctx.model.User);
assert.ok(ctx.model.User.prototype.hasPosts);
assert.ok(ctx.model.Post);
console.log(ctx.model.Post);
assert.ok(ctx.model.Post.prototype.getUser);
});
});
});
19 changes: 19 additions & 0 deletions test/fixtures/apps/base-dir/app/model/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

const assert = require('assert');

module.exports = app => {
const { STRING, INTEGER } = app.Sequelize;
const User = app.model.define('user', {
name: STRING(30),
age: INTEGER,
});

User.associate = function() {
assert.ok(app.model.User);
assert.ok(app.model.Post);
app.model.User.hasMany(app.model.Post, { as: 'posts', foreignKey: 'user_id' });
};

return User;
};
19 changes: 19 additions & 0 deletions test/fixtures/apps/base-dir/app/other-model/post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict';

const assert = require('assert');

module.exports = app => {
const { INTEGER, STRING } = app.Sequelize;
const Post = app.model.define('post', {
user_id: INTEGER,
name: STRING(30),
});

Post.associate = function() {
assert.ok(app.model.User);
assert.ok(app.model.Post);
app.model.Post.belongsTo(app.model.User, { as: 'user', foreignKey: 'user_id' });
};

return Post;
};
20 changes: 20 additions & 0 deletions test/fixtures/apps/base-dir/config/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

exports.sequelize = {
port: '3306',
baseDir: ['model', 'other-model'],
host: '127.0.0.1',
username: 'root',
password: '',
database: 'test',
dialect: 'mysql',
pool: {
max: 5,
min: 0,
idle: 10000,
},
storage: 'db/test-foo.sqlite',
timezone: '+08:01',
};

exports.keys = '0jN4Fw7ZBjo4xtrLklDg4g==';
3 changes: 3 additions & 0 deletions test/fixtures/apps/base-dir/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "base-dir"
}