Reverse links typescript
Hi there. I'm wondering if it's possible to
read
a ShoppingCart
by it's id
and also get all it's ShoppingCartLineItems
in one query. I'm trying to avoid making multiple queries to the database.
I've had a look at https://xata.io/blog/navigating-many-to-one and https://xata.io/docs/concepts/data-model#links-and-relationships but I'm still not sure how to do this.
Schema:
What seems to work is:
However, record
doesn't have a lineItems
prop. 🤔Reduce query round trips with improved one-to-many relations
Addressing the N+1 problem and navigating one-to-many relationships with Xata's new approach.
Data model
A relational data model with schemaful flexibility and JSON support
5 Replies
Right, that's because the response record type does not include the
lineItems
field which is introduced ad-hoc at query time.
Try referencing it as record?.lineItems
So is record.lineItems not strongly typed?
Yes, it seems the return type does not include aliases. I added a bug report to the client repo: https://github.com/xataio/client-ts/issues/1524
GitHub
Reverse link Aliases are not typed · Issue #1524 · xataio/client-ts
Reported on client version: 0.29.5 When aliasing a reverse link result with as, the alias is not typed in the returned object. const record = await xata.db.ShoppingCarts.select([{ name: "<-...
Thanks @kostas. 🙏 Doesn't seem to work without the
as
as well. Is this expected?It is the same issue, reverse link results are not typed. I extended the description of the issue for more clarity. Thanks for pointing it out