Created
March 11, 2018 21:28
-
-
Save doug2k1/fdcfd6343ac5711bae2cb8fbbc6793f7 to your computer and use it in GitHub Desktop.
Test model associations
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 { Broker, Investment, Transaction, BalanceUpdate } = require('./models'); | |
const test = async () => { | |
// create broker | |
const broker = await Broker.create({ name: 'Fooinvest' }); | |
// create investment | |
const investment = await Investment.create({ | |
name: 'Tesouro Foo', | |
BrokerId: broker.get('id') | |
}); | |
// create transaction | |
await Transaction.create({ | |
amount: 500, | |
date: '2018-03-10', | |
InvestmentId: investment.get('id') | |
}); | |
// create balance update | |
await BalanceUpdate.create({ | |
amount: 501, | |
date: '2018-03-12', | |
InvestmentId: investment.get('id') | |
}); | |
// select all | |
const brokerWithDetails = await Broker.findOne({ | |
include: [ | |
{ | |
model: Investment, | |
include: [{ model: Transaction }, { model: BalanceUpdate }] | |
} | |
] | |
}); | |
console.log(JSON.stringify(brokerWithDetails)); | |
}; | |
test(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment