Skip to content

Instantly share code, notes, and snippets.

@david-mart
Created June 11, 2019 21:26
Show Gist options
  • Save david-mart/c288302297c1dd43c85dbb779c416587 to your computer and use it in GitHub Desktop.
Save david-mart/c288302297c1dd43c85dbb779c416587 to your computer and use it in GitHub Desktop.
await UsersDb.models.user.findOne({
where: { id: 1 },
include: {
model: UsersDb.models.order,
include: {
model: ProductsDb.models.product,
on: {
order_id: Sequelize.literal("`orders`.`id` = `orders->products`.`order_id`")
// you can always refer the generated query to get
// the exact column names for the literal string
}
}
}
});
// output
{
"id": 1,
"name": "Jamie Lannister",
"orders": [
{
"id": 1,
"user_id": 1,
"order_date": "2019-05-10T07:00:00.000Z",
"products": [
{
"id": 1,
"order_id": 1,
"name": "oathkeeper"
},
{
"id": 2,
"order_id": 1,
"name": "longclaw"
},
{
"id": 3,
"order_id": 1,
"name": "needle"
}
]
},
{
"id": 2,
"user_id": 1,
"order_date": "2019-05-11T07:00:00.000Z",
"products": []
},
{
"id": 3,
"user_id": 1,
"order_date": "2019-05-12T07:00:00.000Z",
"products": []
},
{
"id": 4,
"user_id": 1,
"order_date": "2019-05-13T07:00:00.000Z",
"products": []
},
{
"id": 5,
"user_id": 1,
"order_date": "2019-06-01T07:00:00.000Z",
"products": []
},
{
"id": 6,
"user_id": 1,
"order_date": "2019-06-03T07:00:00.000Z",
"products": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment