Skip to main content

Examples

The following examples demonstrate common use cases for the updateRecords() method across internal Manta tables and external PostgreSQL databases.

💡PostgreSQL Support

For all examples below, if you are updating an external PostgreSQL database instead of an internal Manta table, simply add the db parameter to the request object:

await manta.updateRecords({
db: "your_connection_name",
table: "products",
// ... rest of the payload
});

Update products by category​

This example updates all products that belong to the 'electronics' category.

TypeScript
// Update all electronics products
await manta.updateRecords({
table: "products",
data: { featured: true },
where: { category_id: "electronics" },
});

Update with validation​

This example updates a user record and applies specific per-row validation rules to ensure the new email and username fields are valid.

TypeScript
// Update user with validation
await manta.updateRecords({
table: "users",
data: {
email: "new@example.com",
username: "newuser",
},
where: { id: 1 },
options: {
validationRule: {
email: { required: true, format: "email" },
username: { required: true, minLength: 3, regex: /^[a-z0-9_]+$/ },
},
},
});

Complex conditional update​

This query uses nested and/or logic in the where clause to target products that are electronics and under $100, OR any product that costs over $500.

TypeScript
// Update products that are electronics AND under $100 OR over $500
await manta.updateRecords({
table: "products",
data: { status: "reviewed" },
where: {
or: [
{
and: [{ category_id: "electronics" }, { price: { lessThan: 100 } }],
},
{ price: { greaterThan: 500 } },
],
},
});

Transactional nested update​

This example updates both the primary record (users) and a related record (profile) as a single, guaranteed transaction.

TypeScript
// Update user and related profile atomically
await manta.updateRecords({
table: "users",
data: { name: "Alice" },
where: { id: 1 },
with: {
profile: {
data: { bio: "Updated bio" },
on: { from: "id", to: "userId" },
},
},
options: {
transactional: true,
},
});

Chunked batch update​

Use chunkSize to process a large number of updates in smaller batches, which can prevent timeouts and improve performance for massive operations.

TypeScript
// Update large dataset in chunks
await manta.updateRecords({
table: "products",
data: massiveArrayOfProducts, // Assume this is a very large array
where: { category_id: "electronics" },
options: {
chunkSize: 1000,
},
});