diff --git a/index.d.ts b/index.d.ts index b1c57b3..f0c64be 100644 --- a/index.d.ts +++ b/index.d.ts @@ -9,7 +9,7 @@ interface EggSequelizeOptions extends sequelize.Options { /** * load models from `app/model/*.js` */ - baseDir?: string; + baseDir?: string | Array; /** * ignore `app/${baseDir}/index.js` when load models, support glob and array diff --git a/lib/loader.js b/lib/loader.js index cc3e83a..caf5b96 100644 --- a/lib/loader.js +++ b/lib/loader.js @@ -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); diff --git a/test/base-dir.test.js b/test/base-dir.test.js new file mode 100644 index 0000000..f62d6f3 --- /dev/null +++ b/test/base-dir.test.js @@ -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); + }); + }); +}); diff --git a/test/fixtures/apps/base-dir/app/model/user.js b/test/fixtures/apps/base-dir/app/model/user.js new file mode 100644 index 0000000..d3ad957 --- /dev/null +++ b/test/fixtures/apps/base-dir/app/model/user.js @@ -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; +}; diff --git a/test/fixtures/apps/base-dir/app/other-model/post.js b/test/fixtures/apps/base-dir/app/other-model/post.js new file mode 100644 index 0000000..e5bd80c --- /dev/null +++ b/test/fixtures/apps/base-dir/app/other-model/post.js @@ -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; +}; diff --git a/test/fixtures/apps/base-dir/config/config.js b/test/fixtures/apps/base-dir/config/config.js new file mode 100644 index 0000000..8423b9c --- /dev/null +++ b/test/fixtures/apps/base-dir/config/config.js @@ -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=='; diff --git a/test/fixtures/apps/base-dir/package.json b/test/fixtures/apps/base-dir/package.json new file mode 100644 index 0000000..5e484b4 --- /dev/null +++ b/test/fixtures/apps/base-dir/package.json @@ -0,0 +1,3 @@ +{ + "name": "base-dir" +}