Changelog - 2026-11-08
A relational repository upserts on a declared unique key
New FeatureIn one line. repository.upsert({ data, on }) writes a row whether or not it exists yet, in one statement, on Postgres, PGlite and SQLite, without giving up the row scope.
What changed
UniqueKeys.of({ table, index })in@venizia/ignis-connectors/relational. It returns a typed handle on a unique index, unique constraint or.unique()column of the table. It throws when the module loads if the name is missing, the index is not unique, or an entry is an expression. A partial index keeps itswhere; write it with literals, not a bound value, which SQLite cannot match.upsert({ data, on?, update?, options? })on every relational repository.onis a handle fromUniqueKeys.of; omitted, the primary key.updatenames the columns refreshed when the row exists; by default, every columndatasets except the key's and the primary key; a primary key column inupdateis refused. It returns{ count, data }likecreate.- Checked by the compiler. A key of another table, an unknown
updatecolumn,createdAt/createdByinupdate, anddatamissing a required column do not compile. - The rules of
createandupdatehold. The new row passes the write guard as a create (assignfills the scope column), the updated columns pass it as an update, and the existing row must be in your scope and default filter. A row outside them is left as it is and the call fails with the write-denied 403. - A null in a key column is refused (400), unless the key is a
nullsNotDistinct()constraint: a null never conflicts, so the upsert would insert a duplicate each time. - Defaults apply on insert,
$onUpdatecolumns such asmodifiedAton the update branch; hidden fields are written and never returned.
ts
export class SelfOrderCache extends ModelFactory.defineEntity({ table: selfOrderCacheTable }) {
static readonly keys = {
merchantCacheKey: UniqueKeys.of({ table: selfOrderCacheTable, index: 'self_order_cache_merchant_key' }),
};
}
await cache.upsert({ data: { cacheKey, value, expiresAt }, on: SelfOrderCache.keys.merchantCacheKey });Who is affected
- Code that reads a row, then creates or updates it. It can use
upsert: one statement, no race between the read and the write. Optional. - Everyone else. No action needed.
See Upsert and Unique Keys.