Nested updates
Use the with parameter to update records in one or more related tables as part of the primary update operation. This ensures data consistency across related entities in both Manta data tables and external PostgreSQL databases.
Basic nested update
This example updates a user's name and simultaneously updates the associated profile record.
- Manta data table
- PostgreSQL
TypeScript
await manta.updateRecords({
table: "users",
data: { name: "Alice Updated" },
where: { id: 1 },
with: {
profile: {
data: { bio: "Updated bio", avatar: "new-avatar.jpg" },
on: { from: "id", to: "userId" },
updateMatch: "all" // Optional: 'all' (default), 'first', or 'last'
},
},
});
TypeScript
await manta.updateRecords({
db: "user_db",
table: "users",
data: { name: "Alice Updated" },
where: { id: 1 },
with: {
profile: {
data: { bio: "Updated bio", avatar: "new-avatar.jpg" },
on: { from: "id", to: "userId" },
updateMatch: "all"
},
},
});
Conditional nested update
Use the when clause inside the nested update to apply the update only if the related record meets a specific condition.
- Manta data table
- PostgreSQL
TypeScript
await manta.updateRecords({
table: "users",
data: { name: "Bob Updated" },
where: { id: 2 },
with: {
profile: {
data: { tier: "premium" },
on: { from: "id", to: "userId" },
// Only update the profile if the status is currently 'active'
when: { and: { status: { equals: "active" } } },
},
},
});
TypeScript
await manta.updateRecords({
db: "user_db",
table: "users",
data: { name: "Bob Updated" },
where: { id: 2 },
with: {
profile: {
data: { tier: "premium" },
on: { from: "id", to: "userId" },
when: { and: { status: { equals: "active" } } },
},
},
});
Multiple nested updates
You can target multiple related tables simultaneously by adding them as keys within the with object.
- Manta data table
- PostgreSQL
TypeScript
await manta.updateRecords({
table: "users",
data: { name: "Charlie Updated" },
where: { id: 3 },
with: {
profile: {
data: { bio: "Updated profile" },
on: { from: "id", to: "userId" },
},
orders: {
data: { status: "processed" },
on: { from: "id", to: "customerId" },
when: { and: { orderStatus: { equals: "pending" } } },
},
},
});
TypeScript
await manta.updateRecords({
db: "user_db",
table: "users",
data: { name: "Charlie Updated" },
where: { id: 3 },
with: {
profile: {
data: { bio: "Updated profile" },
on: { from: "id", to: "userId" },
},
orders: {
data: { status: "processed" },
on: { from: "id", to: "customerId" },
when: { and: { orderStatus: { equals: "pending" } } },
},
},
});