(ns iol-ion.tx.reset-scalars
  (:require [datomic.api :as dc]))

(defn reset-scalars [db e a vs]
  
  (let [extant   (when-not (string? e)
                   (->> (dc/q '[:find  ?z
                                :in    $ ?e ?a
                                :where [?e ?a ?z]]
                              db e a)
                        (map first)))
        retracts (filter (complement (set vs)) extant)
        new      (filter (complement (set extant)) vs)]
    (-> []
        (into (map (fn [i] [:db/retract e a i ]) retracts))
        (into (map (fn [i] [:db/add e a i]) new)))))
