Skip to content

Upsert

The service provides an upsert method using ON CONFLICT (PostgreSQL/SQLite) or ON DUPLICATE KEY UPDATE (MySQL).

ts
const result = await app.service("users").upsert(
  { name: "Alice", age: 31 },
  {
    onConflictFields: ["name"],
    onConflictAction: "merge", // 'merge' (default) or 'ignore'
  },
);

Options

OptionTypeDefaultDescription
onConflictFieldsstring[]requiredFields to use in the ON CONFLICT clause
onConflictActionstring'merge''merge' or 'ignore'
onConflictMergeFieldsstring[]Specific fields to update on conflict
onConflictExcludeFieldsstring[]Fields to exclude from update

Merge vs Ignore

  • merge — updates the existing row with the new values (ON CONFLICT DO UPDATE)
  • ignore — keeps the existing row unchanged (ON CONFLICT DO NOTHING)

Controlling Merged Fields

ts
// Only update the age field on conflict
await app.service("users").upsert(
  { name: "Alice", age: 31 },
  {
    onConflictFields: ["name"],
    onConflictMergeFields: ["age"],
  },
);

// Update everything except the name field
await app.service("users").upsert(
  { name: "Alice", age: 31 },
  {
    onConflictFields: ["name"],
    onConflictExcludeFields: ["name"],
  },
);