Skip to content

:insert-or-update

The :insert-or-update transact form allow you to insert rows, but on :unique key conflict, apply a function (or SQL style update map) to the old row instead.

(def db (rel/mat {} [[:from :Customer] [:unique :id]]))

;; if there is no conflict, its just insert
(def db (rel/transact db [:insert-or-update :Customer {:updates [inc [:or :updates 0]]} {:id 42, :name "bob", :age 33}}]))
;; => 
{:Customer #{{:id 42, :name "bob", :age 33}}}

;; with a conflict, the function (or set map) will be applied to the OLD row, and the new row will be discarded.
(rel/transact db [:insert-or-replace :Customer {:updates [inc [:or :updates 0]]} {:id 42, :name "alice"}])
;; =>
{:Customer #{{:id 42, :name "bob", :age 33, :updates 1}}}

See also :insert-or-merge, :insert-or-replace, :update.