Created
January 21, 2019 03:53
-
-
Save Calvin-Huang/a513482f005c68bd8e98cb2fb8722413 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const { DataTypes } = require('sequelize'); | |
const ApplicationRecord = require('./application-record'); | |
const Foo = require('./foo'); | |
const Boo = ApplicationRecord.define('Boo', { | |
fooId: DataTypes.INTEGER, | |
booName: DataTypes.STRING | |
}, {}); | |
// Boo.belongsTo(Foo); <= 這樣會噴掉, 因為跟 Foo 有循環引用, boo 就變成 defefined | |
Boo.belongsTo(ApplicationRecord.define('foo'), { as: 'foo' }); | |
module.exports = Boo; |
This file contains hidden or 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
const { DataTypes } = require('sequelize'); | |
const ApplicationRecord = require('./application-record'); | |
const Boo = require('./boo'); | |
const Definetion = ApplicationRecord.define('Foo', { | |
firstName: DataTypes.STRING, | |
lastName: DataTypes.STRING, | |
email: DataTypes.STRING | |
}, {}); | |
class Foo extends Definetion { | |
toJSON() { | |
const data = this.get(); | |
return { | |
...data, | |
email: undefined | |
}; | |
} | |
getBooName() { | |
console.log('======'); | |
} | |
} | |
Foo.hasMany(Boo); // <= include 的時候會變成 Boos | |
// Foo.hasMany(Boo, { as: 'boos' }) <= include 的時候會變成 boos | |
module.exports = Foo; |
This file contains hidden or 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
const Koa = require('koa'); | |
const Router = require('koa-router'); | |
const Foo = require('./models/foo'); | |
const Boo = require('./models/boo'); | |
const app = new Koa(); | |
const router = new Router(); | |
router.get('/foo', async (ctx) => { | |
const foo = await Foo.create({ firstName: 'Calvin', lastName: 'Huang' }); | |
ctx.body = foo; | |
}); | |
router.get('/boo', async (ctx) => { | |
const boo = await Boo.create({ fooId: 1, booName: 'Kevin' }); | |
ctx.body = boo; | |
}); | |
router.get('/', async (ctx) => { | |
const foo = await Foo.find({ where: 1, include: [Boo] }); | |
foo.getBooName(); | |
ctx.body = foo; | |
}); | |
app.use(router.routes()); | |
app.listen('3003', () => { | |
console.log('> Listen on port 3003'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment