moved tx functions.
This commit is contained in:
@@ -671,99 +671,11 @@
|
||||
(defn pull-ref [db k id]
|
||||
(:db/id (pull-attr db k id)))
|
||||
|
||||
(declare upsert-entity)
|
||||
|
||||
(defn reset-rels [db e a vs]
|
||||
(assert (every? :db/id vs) (format "In order to reset attribute %s, every value must have :db/id" a))
|
||||
(let [ids (when-not (string? e)
|
||||
(->> (dc/q '[:find ?z
|
||||
:in $ ?e ?a
|
||||
:where [?e ?a ?z]]
|
||||
db e a)
|
||||
(map first)))
|
||||
new-id-set (set (map :db/id vs))
|
||||
retract-ids (filter (complement new-id-set) ids)
|
||||
{is-component? :db/isComponent} (dc/pull db [:db/isComponent] a)
|
||||
new-rels (filter (complement (set ids)) (map :db/id vs))]
|
||||
(-> []
|
||||
(into (map (fn [i] (if is-component?
|
||||
[:db/retractEntity i]
|
||||
[:db/retract e a i ])) retract-ids))
|
||||
(into (map (fn [i] [:db/add e a i]) new-rels))
|
||||
(into (mapcat (fn [i] (upsert-entity db i)) vs)))))
|
||||
|
||||
(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)))))
|
||||
|
||||
|
||||
;; TODO unit test this
|
||||
(defn upsert-entity [db entity]
|
||||
(assert (:db/id entity) "Cannot upsert without :db/id")
|
||||
(let [e (:db/id entity)
|
||||
is-new? (string? e)
|
||||
extant-entity (when-not is-new?
|
||||
(dc/pull db (keys entity) (:db/id entity)))
|
||||
ident->value-type (by :db/ident (comp :db/ident
|
||||
:db/valueType)
|
||||
(pull-many
|
||||
db
|
||||
[:db/valueType :db/ident]
|
||||
(keys entity)))
|
||||
ops (->> entity
|
||||
(reduce
|
||||
(fn [ops [a v]]
|
||||
(cond
|
||||
(= :db/id a)
|
||||
ops
|
||||
|
||||
(or (= v (a extant-entity))
|
||||
(= v (:db/ident (a extant-entity) :nope))
|
||||
(= v (:db/id (a extant-entity)) :nope))
|
||||
ops
|
||||
|
||||
(and (nil? v)
|
||||
(not (nil? (a extant-entity))))
|
||||
(conj ops [:db/retract e a (cond-> (a extant-entity)
|
||||
(:db/id (a extant-entity)) :db/id)])
|
||||
|
||||
(nil? v)
|
||||
ops
|
||||
|
||||
;; reset relationships if it's refs, and not a lookup (i.e., seq of maps, or empty seq)
|
||||
(and (sequential? v) (= :db.type/ref (ident->value-type a)) (every? map? v))
|
||||
(into ops (reset-rels db e a v))
|
||||
|
||||
(and (sequential? v) (not= :db.type/ref (ident->value-type a)))
|
||||
(into ops (reset-scalars db e a v))
|
||||
|
||||
(and (map? v)
|
||||
(= :db.type/ref (ident->value-type a)))
|
||||
(let [id (or (:db/id v) (random-tempid))]
|
||||
(-> ops
|
||||
(conj [:db/add e a id])
|
||||
(into (upsert-entity db (assoc v :db/id id)))))
|
||||
|
||||
:else
|
||||
(conj ops [:db/add e a v])
|
||||
))
|
||||
[]))]
|
||||
ops))
|
||||
|
||||
(defn plus [db e a amount]
|
||||
[[:db/add e a (-> (dc/pull db [a] e) a (+ amount))]])
|
||||
|
||||
(comment
|
||||
#_(comment
|
||||
(dc/pull (dc/db conn) '[*] 175921860633685)
|
||||
|
||||
(upsert-entity (dc/db conn) {:db/id 175921860633685 :invoice/invoice-number nil :invoice/date #inst "2021-01-01" :invoice/expense-accounts [:reset-rels [{:db/id "new" :invoice-expense-account/amount 1}]]})
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
(ns auto-ap.graphql.accounts
|
||||
(:require
|
||||
[auto-ap.datomic :refer [audit-transact conn upsert-entity]]
|
||||
[auto-ap.datomic :refer [audit-transact conn]]
|
||||
[iol-ion.tx :refer [upsert-entity]]
|
||||
[auto-ap.datomic.accounts :as d-accounts]
|
||||
[auto-ap.graphql.utils
|
||||
:refer [->graphql
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
(ns auto-ap.graphql.clients
|
||||
(:require
|
||||
[amazonica.aws.s3 :as s3]
|
||||
[auto-ap.datomic :refer [audit-transact conn upsert-entity random-tempid]]
|
||||
[iol-ion.tx :refer [upsert-entity random-tempid]]
|
||||
[auto-ap.datomic :refer [audit-transact conn]]
|
||||
[auto-ap.datomic.clients :as d-clients]
|
||||
[auto-ap.square.core :as square]
|
||||
[auto-ap.graphql.utils
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
(ns auto-ap.graphql.transaction-rules
|
||||
(:require
|
||||
[auto-ap.datomic :refer [audit-transact conn merge-query upsert-entity random-tempid]]
|
||||
[auto-ap.datomic :refer [audit-transact conn merge-query]]
|
||||
[iol-ion.tx :refer [upsert-entity random-tempid]]
|
||||
[auto-ap.datomic.transaction-rules :as tr]
|
||||
[auto-ap.datomic.transactions :as d-transactions]
|
||||
[auto-ap.graphql.utils
|
||||
|
||||
@@ -7,8 +7,8 @@
|
||||
pull-attr
|
||||
pull-many
|
||||
pull-ref
|
||||
remove-nils
|
||||
upsert-entity]]
|
||||
remove-nils]]
|
||||
[iol-ion.tx :refer [upsert-entity]]
|
||||
[auto-ap.datomic.accounts :as a]
|
||||
[auto-ap.datomic.checks :as d-checks]
|
||||
[auto-ap.datomic.invoices :as d-invoices]
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
(ns auto-ap.routes.queries
|
||||
(:require
|
||||
[amazonica.aws.s3 :as s3]
|
||||
[auto-ap.datomic :refer [conn pull-attr upsert-entity]]
|
||||
[auto-ap.datomic :refer [conn pull-attr]]
|
||||
[iol-ion.tx :refer [upsert-entity]]
|
||||
[auto-ap.graphql.utils :refer [assert-admin]]
|
||||
[clojure.data.csv :as csv]
|
||||
[clojure.edn :as edn]
|
||||
|
||||
Reference in New Issue
Block a user