more code trimming for datomic.

This commit is contained in:
Bryce Covert
2018-08-16 08:38:01 -07:00
parent a4e3fe2327
commit ad2dd386ee
4 changed files with 64 additions and 45 deletions

View File

@@ -0,0 +1,46 @@
(ns auto-ap.datomic.users
(:require [datomic.api :as d]
[auto-ap.datomic :refer [uri]]
[clojure.set :refer [rename-keys]]
[clj-time.coerce :as c]))
(defn add-arg [query name value where & rest]
(let [query (-> query
(update :args conj value)
(update-in [:query :in] conj name)
(update-in [:query :where] conj where))]
(reduce #(update-in %1 [:query :where] conj %2) query rest)))
(defn raw-graphql [args]
(let [query (cond-> {:query {:find ['(pull ?e [*
{:user/clients [*]}
{:user/role [:db/ident]}])]
:in ['$]
:where ['[?e :user/original-id]]}
:args [(d/db (d/connect uri))]})]
(->> (d/query query)
(map first)
(map #(update % :user/role :db/ident))
)))
(defn sort-fn [args]
(cond
(= "client" (:sort-by args))
#(-> % :payment/client :client/name)
:else
(keyword "payment" (:sort-by args))))
(defn get-graphql [args]
(let [results (raw-graphql args)]
(cond->> results
(:sort-by args) (sort-by (sort-fn args))
(= (:asc args) false) (reverse)
true (drop (:start args 0))
true (take (:count args 20)))))
(defn count-graphql [args]
(->> (raw-graphql args)
(count)))