Skip to content

Instantly share code, notes, and snippets.

@doug2k1
Created March 11, 2018 21:28
Show Gist options
  • Save doug2k1/fdcfd6343ac5711bae2cb8fbbc6793f7 to your computer and use it in GitHub Desktop.
Save doug2k1/fdcfd6343ac5711bae2cb8fbbc6793f7 to your computer and use it in GitHub Desktop.
Test model associations
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