Relationships
You can include related data from other tables using the with parameter.
Relationship parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
fields | string[] | Yes | The specific fields to return from the related table. |
on | object | Yes | The condition used to link the source table to the related table. |
on.from | string | Yes | The field in the source table used for the join condition. |
on.to | string | Yes | The field in the related table used for the join condition. |
filter | Record<string, any> | No | An optional filter object to apply to the related records. |
with | Record<string, RelationshipOptions> | No | An object specifying additional related tables to include from this relationship. |
page | number | No | The page number for paginating the related data. |
list | number | No | The number of records to return per page for the related data. |
orderBy | string | No | The field to sort the related data by. |
order | 'asc' or 'desc' | No | The sort direction for the related data. |
Include related data
This example fetches support tickets and includes the associated user data for each ticket.
- Manta data table
- PostgreSQL
TypeScript
const tickets = await manta.fetchAllRecords({
table: 'support-tickets',
fields: ['ticket_id', 'subject'],
with: {
users: {
fields: ['first_name', 'last_name'],
on: { from: 'user_id', to: 'user_id' }
}
}
});
TypeScript
const tickets = await manta.fetchAllRecords({
db: 'analytics_pg',
table: 'support-tickets',
fields: ['ticket_id', 'subject'],
with: {
users: {
fields: ['first_name', 'last_name'],
on: { from: 'user_id', to: 'user_id' }
}
}
});
Multiple relationships
This example fetches support tickets and includes the users and the orders associated with each ticket.
- Manta data table
- PostgreSQL
TypeScript
const tickets = await manta.fetchAllRecords({
table: "support-tickets",
fields: ["ticket_id", "subject", "status"],
with: {
users: {
fields: ["first_name", "last_name"],
// from (source table => support-tickets), to (related table => users)
on: { from: "user_id", to: "user_id" },
},
orders: {
fields: ["product_name", "amount"],
// from (source table => support-tickets), to (related table => orders)
on: { from: "order_id", to: "order_id" },
},
},
page: 1,
list: 10,
});
TypeScript
const tickets = await manta.fetchAllRecords({
db: 'analytics_pg',
table: "support-tickets",
fields: ["ticket_id", "subject", "status"],
with: {
users: {
fields: ["first_name", "last_name"],
// from (source table => support-tickets), to (related table => users)
on: { from: "user_id", to: "user_id" },
},
orders: {
fields: ["product_name", "amount"],
// from (source table => support-tickets), to (related table => orders)
on: { from: "order_id", to: "order_id" },
},
},
page: 1,
list: 10,
});