Upsert operations
Set options.upsert to true to perform an "upsert" (update or insert). If the SDK finds a record matching your where clause, it updates that record. If no match is found, it inserts a new record.
Basic upsert
Use conflictKeys to specify which fields the database should use to identify an existing record during the insert phase.
- Manta data table
- PostgreSQL
TypeScript
await manta.updateRecords({
table: "products",
data: { name: "New Product", sku: "SKU123" },
where: { sku: "SKU123" }, // Attempts to find/update by this field
options: {
upsert: true,
conflictKeys: ["sku"],
},
});
TypeScript
await manta.updateRecords({
db: "inventory_pg",
table: "products",
data: { name: "New Product", sku: "SKU123" },
where: { sku: "SKU123" }, // Attempts to find/update by this field
options: {
upsert: true,
conflictKeys: ["sku"], // Maps to PostgreSQL ON CONFLICT (sku)
},
});
Upsert with OR logic
By default, conflictKeys are combined using AND logic. Set conflictKeysLogic to 'or' to trigger an update if any of the specified keys match an existing record.
- Manta data table
- PostgreSQL
TypeScript
await manta.updateRecords({
table: "products",
data: { name: "Product X", sku: "SKU123", barcode: "BAR456" },
where: { sku: "SKU123" },
options: {
upsert: true,
conflictKeys: ["sku", "barcode"],
conflictKeysLogic: "or",
},
});
TypeScript
await manta.updateRecords({
db: "inventory_pg",
table: "products",
data: { name: "Product X", sku: "SKU123", barcode: "BAR456" },
where: { sku: "SKU123" },
options: {
upsert: true,
conflictKeys: ["sku", "barcode"],
conflictKeysLogic: "or",
},
});