-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: multi datasource support load same models (#63)
- Loading branch information
1 parent
b41598e
commit c4998e1
Showing
10 changed files
with
265 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const mm = require('egg-mock'); | ||
|
||
describe('test/datasources_same_dir.test.js', () => { | ||
let app; | ||
|
||
before(() => { | ||
app = mm.app({ | ||
baseDir: 'apps/datasources-same-dir', | ||
}); | ||
return app.ready(); | ||
}); | ||
before(async () => { | ||
await app.model.sync({ force: true }); | ||
await app.sequelize.sync({ force: true }); | ||
}); | ||
|
||
after(mm.restore); | ||
|
||
describe('Base', () => { | ||
it('sequelize init success', () => { | ||
assert(app.model); | ||
assert(app.sequelize); | ||
assert(app.Sequelize); | ||
}); | ||
|
||
it('ctx model property getter', () => { | ||
const ctx = app.mockContext(); | ||
assert(ctx.model); | ||
assert(ctx.model.User); | ||
assert(ctx.model.Monkey); | ||
assert(ctx.model.Person); | ||
assert(ctx.sequelize); | ||
assert(ctx.sequelize.User); | ||
assert(ctx.sequelize.Monkey); | ||
assert(!ctx.sequelize.Person); // ignored | ||
assert(ctx.model.User !== ctx.sequelize.User); | ||
}); | ||
|
||
it('has right tableName', () => { | ||
assert(app.model.Person.tableName === 'people'); | ||
assert(app.model.User.tableName === 'users'); | ||
assert(app.model.Monkey.tableName === 'the_monkeys'); | ||
assert(app.sequelize.User.tableName === 'users'); | ||
assert(app.sequelize.Monkey.tableName === 'the_monkeys'); | ||
}); | ||
}); | ||
|
||
describe('Test model', () => { | ||
it('User.test method work', async function() { | ||
await app.model.User.test(); | ||
}); | ||
|
||
it('should work timestramp', async function() { | ||
let user = await app.model.User.create({ name: 'huacnlee' }); | ||
assert(user.isNewRecord === false); | ||
assert(user.name === 'huacnlee'); | ||
assert(user.created_at !== null); | ||
assert(user.updated_at !== null); | ||
|
||
user = await app.sequelize.User.create({ name: 'huacnlee' }); | ||
assert(user.isNewRecord === false); | ||
assert(user.name === 'huacnlee'); | ||
assert(user.created_at !== null); | ||
assert(user.updated_at !== null); | ||
}); | ||
}); | ||
|
||
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); | ||
|
||
assert.ok(ctx.sequelize); | ||
assert.ok(ctx.sequelize.User); | ||
assert.ok(ctx.sequelize.User.prototype.hasPosts); | ||
assert.ok(ctx.sequelize.Post); | ||
}); | ||
}); | ||
}); |
10 changes: 10 additions & 0 deletions
10
test/fixtures/apps/datasources-same-dir/app/model/Person.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict'; | ||
|
||
module.exports = (app, model) => { | ||
const { STRING } = app.Sequelize; | ||
const Person = model.define('person', { | ||
name: STRING(30), | ||
}); | ||
|
||
return Person; | ||
}; |
28 changes: 28 additions & 0 deletions
28
test/fixtures/apps/datasources-same-dir/app/model/monkey.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict'; | ||
|
||
module.exports = (app, model) => { | ||
const { STRING, INTEGER, DATE } = app.Sequelize; | ||
const Monkey = model.define('monkey', { | ||
name: { | ||
type: STRING, | ||
allowNull: false, | ||
}, | ||
user_id: INTEGER, | ||
created_at: DATE, | ||
updated_at: DATE, | ||
}, { | ||
tableName: 'the_monkeys', | ||
|
||
classMethods: { | ||
}, | ||
|
||
instanceMethods: { | ||
}, | ||
}); | ||
|
||
Monkey.findUser = async function() { | ||
return model.User.find({ id: 1 }); | ||
}; | ||
|
||
return Monkey; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
|
||
module.exports = (app, model) => { | ||
const { INTEGER, STRING } = app.Sequelize; | ||
const Post = model.define('post', { | ||
user_id: INTEGER, | ||
name: STRING(30), | ||
}); | ||
|
||
Post.associate = function() { | ||
assert.ok(model.User); | ||
assert.ok(model.Post); | ||
model.Post.belongsTo(model.User, { as: 'user', foreignKey: 'user_id' }); | ||
}; | ||
|
||
return Post; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
|
||
module.exports = (app, model) => { | ||
const { STRING, INTEGER } = app.Sequelize; | ||
const User = model.define('user', { | ||
name: STRING(30), | ||
age: INTEGER, | ||
}); | ||
|
||
User.associate = function() { | ||
assert.ok(model.User); | ||
assert.ok(model.Post); | ||
model.User.hasMany(model.Post, { as: 'posts', foreignKey: 'user_id' }); | ||
}; | ||
|
||
User.test = async function() { | ||
assert(app.config); | ||
assert(model.User === this); | ||
const monkey = await model.Monkey.create({ name: 'The Monkey' }); | ||
assert(monkey.id); | ||
assert(monkey.isNewRecord === false); | ||
assert(monkey.name === 'The Monkey'); | ||
}; | ||
|
||
return User; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
|
||
exports.sequelize = { | ||
datasources: [ | ||
{ | ||
delegate: 'model', | ||
baseDir: 'model', | ||
database: 'test', | ||
dialect: 'mysql', | ||
}, | ||
{ | ||
delegate: 'sequelize', | ||
baseDir: 'model', | ||
database: 'test1', | ||
dialect: 'mysql', | ||
exclude: 'Person.js', | ||
}, | ||
], | ||
}; | ||
|
||
exports.keys = '0jN4Fw7ZBjo4xtrLklDg4g=='; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "datasources-same-dir" | ||
} |