Skip to content

Instantly share code, notes, and snippets.

@spieden
Created December 18, 2015 20:04
Show Gist options
  • Save spieden/79255a3a0f27e2c372d8 to your computer and use it in GitHub Desktop.
Save spieden/79255a3a0f27e2c372d8 to your computer and use it in GitHub Desktop.
(ns cpdx.db.entity-patch
(:require [clojure.data :as data]
[slingshot.slingshot :refer [throw+]]))
(defn pair->assertion [verb id-ref [attr value]]
[verb id-ref attr value])
(defn make-assertions [verb id-ref pairs]
(map (partial pair->assertion verb id-ref)
pairs))
(defn resolve-patch [id-attr previous current]
(let [[removed added retained] (data/diff previous current)
id-ref [id-attr (current id-attr)]]
(when-not (contains? retained id-attr)
(throw+ :ids-disagree))
(into []
(comp cat (remove nil?))
[(make-assertions :db/retract id-ref removed)
(make-assertions :db/add id-ref added)])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment