Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/bitten/db.clj
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,18 @@

(def changed-and-retracted (juxt :changed :retracted))

(defn- ensure-entity [{:db/keys [entity] :as record}]
(cond-> record (not entity) (assoc :db/entity (str (random-uuid)))))

(defn upsert!
"Writes only changed facts from records to backend. Returns tx-id or nil.
(upsert! db [{:db/entity \"user/1\" :name \"Alice\"}])
:missing-keys — :ignore (default) leaves absent attributes untouched;
:retract removes them."
([backend records] (upsert! backend records nil))
([backend records {:keys [missing-keys] :or {missing-keys :ignore}}]
(let [entities (into #{} (map :db/entity records))
(let [records (map ensure-entity records)
entities (into #{} (map :db/entity records))
current-idx (->> {:entities entities} (query backend) index-by-entity)
facts (for [{:db/keys [entity] :as record} records
:let [existing (get current-idx entity {})
Expand Down
Loading