docs: add a section in the book on how to retrieve a relationship (#8594)

This commit is contained in:
Shahed Nasser
2024-08-15 16:30:00 +03:00
committed by GitHub
parent 3bdfb66238
commit 198d7c9927
2 changed files with 27 additions and 2 deletions

View File

@@ -4,7 +4,7 @@ export const metadata = {
# {metadata.title}
In this chapter, you'll learn how to manage relationships between data models when creating or updating their records using the module's main service.
In this chapter, you'll learn how to manage relationships between data models when creating, updating, or retrieving records using the module's main service.
<Note type="soon" title="Important">
@@ -69,3 +69,28 @@ const order = await helloModuleService.updateOrder({
```
In the example above, you pass the `order_ids` property when you create (or update) a product, and you pass the `product_ids` property when you update (or create) an order.
---
## Retrieve Records of Relation
The `list`, `listAndCount`, and `retrieve` methods of a module's main service accept as a second parameter an object of options.
To retrieve the records associated with a data model's records through a relationship, pass in the second parameter object a `relations` property whose value is an array of relationship names.
For example, assuming you have the [Order and Product data models from the previous chapter](../relationships/page.mdx#many-to-many-relationship):
export const retrieveHighlights = [
["4", `"orders"`, "Retrieve the records associated with the product\nthrough the `orders` relationship."]
]
```ts highlights={retrieveHighlights}
const product = await helloModuleService.retrieveProduct(
"123",
{
relations: ["orders"]
}
)
```
In the example above, the retrieved product has an `orders` property, whose value is an array of orders associated with the product.

View File

@@ -125,7 +125,7 @@ const Order = model.define("order", {
const Product = model.define("product", {
id: model.id().primaryKey(),
order: model.manyToMany(() => Order),
orders: model.manyToMany(() => Order),
})
```