-
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: support config.datasources to define more database
- Loading branch information
1 parent
bfd0c26
commit 86d660d
Showing
18 changed files
with
367 additions
and
77 deletions.
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 was deleted.
Oops, something went wrong.
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,83 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const mm = require('egg-mock'); | ||
|
||
describe('test/datasources.test.js', () => { | ||
let app; | ||
|
||
before(() => { | ||
app = mm.app({ | ||
baseDir: 'apps/datasources', | ||
}); | ||
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.ok(ctx.model); | ||
assert.ok(ctx.model.User); | ||
assert.ok(ctx.model.Monkey); | ||
assert.ok(ctx.model.Person); | ||
assert.ok(ctx.sequelize); | ||
assert.ok(ctx.sequelize.User); | ||
assert.ok(ctx.sequelize.Monkey); | ||
assert.ok(ctx.sequelize.Person); | ||
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'); | ||
}); | ||
}); | ||
|
||
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); | ||
}); | ||
}); | ||
}); |
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 => { | ||
const { STRING } = app.Sequelize; | ||
const Person = app.model.define('person', { | ||
name: STRING(30), | ||
}); | ||
|
||
return Person; | ||
}; |
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 => { | ||
const { STRING, INTEGER, DATE } = app.Sequelize; | ||
const Monkey = app.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 app.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 => { | ||
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; | ||
}; |
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 => { | ||
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' }); | ||
}; | ||
|
||
User.test = async function() { | ||
assert(app.config); | ||
assert(app.model.User === this); | ||
const monkey = await app.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,10 @@ | ||
'use strict'; | ||
|
||
module.exports = app => { | ||
const { STRING } = app.Sequelize; | ||
const Person = app.sequelize.define('person', { | ||
name: STRING(30), | ||
}); | ||
|
||
return Person; | ||
}; |
Oops, something went wrong.