X
Xata3mo ago
maxfi

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:
{
"tables": [
{
"name": "ShoppingCarts",
"columns": [
{
"name": "customerId",
"type": "string"
}
],
"revLinks": [
{
"column": "shoppingCart",
"table": "Users"
},
{
"column": "shoppingCart",
"table": "ShoppingCartLineItems"
}
]
},
{
"name": "ShoppingCartLineItems",
"columns": [
{
"name": "shoppingCart",
"type": "link",
"link": {
"table": "ShoppingCarts"
}
},
{
"name": "productId",
"type": "string"
},
{
"name": "quantity",
"type": "int"
},
{
"name": "type",
"type": "string",
"defaultValue": "product"
}
]
}
]
}
{
"tables": [
{
"name": "ShoppingCarts",
"columns": [
{
"name": "customerId",
"type": "string"
}
],
"revLinks": [
{
"column": "shoppingCart",
"table": "Users"
},
{
"column": "shoppingCart",
"table": "ShoppingCartLineItems"
}
]
},
{
"name": "ShoppingCartLineItems",
"columns": [
{
"name": "shoppingCart",
"type": "link",
"link": {
"table": "ShoppingCarts"
}
},
{
"name": "productId",
"type": "string"
},
{
"name": "quantity",
"type": "int"
},
{
"name": "type",
"type": "string",
"defaultValue": "product"
}
]
}
]
}
What seems to work is:
const record = await xata.db.ShoppingCarts.select([{
name: "<-ShoppingCartLineItems.shoppingCart",
columns: ["productId", "quantity", "type"],
as: "lineItems"
}]).filter({ id: "rec_cno400v3e8os6tplq4jg"}).getFirstOrThrow()
const record = await xata.db.ShoppingCarts.select([{
name: "<-ShoppingCartLineItems.shoppingCart",
columns: ["productId", "quantity", "type"],
as: "lineItems"
}]).filter({ id: "rec_cno400v3e8os6tplq4jg"}).getFirstOrThrow()
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
kostas
kostas3mo ago
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
maxfi
maxfi3mo ago
So is record.lineItems not strongly typed?
No description
kostas
kostas3mo ago
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: "<-...
maxfi
maxfi3mo ago
Thanks @kostas. 🙏 Doesn't seem to work without the as as well. Is this expected?
kostas
kostas3mo ago
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
Want results from more Discord servers?
Add your server