|
Documenting this here because it comes up often — if your situation is different, please reply in this thread. Right now I load the parents, collect their ids, then issue a second query per relation to group the children back myself. It works, but it is a lot of glue code that has to be repeated for every relation. |
Answered by
coenddt
Sep 14, 2026
Replies: 1 comment
|
Declare the relation in the schema and select it in the GQL body — you never hand-write store.register({
name: 'Order', collection: 'orders',
fields: { code: { type: 'string' } },
relations: {
items: { model: 'OrderItem', type: 'many', localField: '_id', foreignField: 'orderId' },
},
});
const rows = await store.query(
'Order($condition:@c0,$limit:@l) { code, items($sort:@s1,$limit:@l2) { sku, qty } }',
{ c0: { status: 'paid' }, l: 50, s1: { qty: -1 }, l2: 5 },
);Relations compile to Two limits worth knowing before you design around it:
Walkthrough: https://coenddt.github.io/nodejs-store/use-cases/05-avoiding-n-plus-one-reads.html |
0 replies
Answer selected by
coenddt
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Declare the relation in the schema and select it in the GQL body — you never hand-write
$lookupor a JOIN.Relations compile to
$lookup/$addFieldson MongoDB, and to a JOIN when the backend is relational. You build the tree once and the engine resolves it as part of one plan.Two limits worth kno…