making everyone do queries the same way.

This commit is contained in:
Bryce Covert
2019-05-04 19:40:29 -07:00
parent e6a36b190a
commit 1232033160
3 changed files with 74 additions and 95 deletions

View File

@@ -1,6 +1,6 @@
(ns auto-ap.datomic.checks
(:require [datomic.api :as d]
[auto-ap.datomic :refer [uri merge-query apply-sort-2 apply-pagination]]
[auto-ap.datomic :refer [uri merge-query apply-sort-2 apply-pagination add-sorter-field]]
[auto-ap.graphql.utils :refer [limited-clients]]
[clojure.set :refer [rename-keys]]
[clj-time.coerce :as c]))
@@ -12,13 +12,6 @@
(update :payment/type :db/ident)
(rename-keys {:invoice-payment/_payment :payment/invoices})))
(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)))
(def default-read '[*
{:invoice-payment/_payment [* {:invoice-payment/invoice [*]}]}
{:payment/client [:client/name :db/id :client/code]}
@@ -34,6 +27,19 @@
:in ['$]
:where []}
:args [db]}
(:sort-by args) (add-sorter-field {"client" ['[?e :payment/client ?c]
'[?c :client/name ?sorter]]
"vendor" ['[?e :payment/vendor ?v]
'[?v :vendor/name ?sorter]]
"bank-account" ['[?e :payment/bank-account ?c]
'[?c :bank-account/name ?sorter]]
"check-number" ['(or-join [?e ?sorter]
[?e :payment/check-number ?sorter]
[(ground "" ?sorter)])]
"date" ['[?e :payment/date ?sorter]]
"amount" ['[?e :payment/amount ?sorter]]
"status" ['[?e :payment/status ?sorter]]}
args)
(limited-clients (:id args))
(merge-query {:query {:in ['[?xx ...]]
@@ -110,17 +116,6 @@
true (apply-sort-2 args)
true (apply-pagination args))))
(defn sort-fn [args]
(cond
(= "client" (:sort-by args))
#(-> % :payment/client :client/name)
(= "vendor" (:sort-by args))
#(-> % :payment/vendor :vendor/name (or "") (.toLowerCase ))
:else
(keyword "payment" (:sort-by args))))
(defn graphql-results [ids db args]
(let [results (->> (d/pull-many db default-read ids)
(group-by :db/id))

View File

@@ -1,44 +1,42 @@
(ns auto-ap.datomic.invoices
(:require [datomic.api :as d]
[auto-ap.datomic :refer [uri remove-nils merge-query]]
[auto-ap.datomic :refer [uri remove-nils merge-query apply-pagination apply-sort-2 add-sorter-field]]
[auto-ap.graphql.utils :refer [limited-clients]]
[auto-ap.parse :as parse]
[clj-time.coerce :as c]
[clojure.set :refer [rename-keys]]
[clojure.string :as str]))
(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)))
(def default-read '(pull ?e [*
{:invoice/client [:client/name :db/id :client/locations :client/code]}
{:invoice/vendor [* {:vendor/address [*]}]}
{:invoice/status [:db/ident]}
{:invoice/expense-accounts [* {:invoice-expense-account/account [*]}]}
{:invoice-payment/_invoice [* {:invoice-payment/payment [* {:payment/status [*]}
{:payment/bank-account [*]}
{:transaction/_payment [*]}]}]}]))
(def default-read '[*
{:invoice/client [:client/name :db/id :client/locations :client/code]}
{:invoice/vendor [* {:vendor/address [*]}]}
{:invoice/status [:db/ident]}
{:invoice/expense-accounts [* {:invoice-expense-account/account [*]}]}
{:invoice-payment/_invoice [* {:invoice-payment/payment [* {:payment/status [*]}
{:payment/bank-account [*]}
{:transaction/_payment [*]}]}]}])
(defn <-datomic [x]
(->> x
(map #(update % :invoice/date c/from-date))
(map #(update % :invoice/status :db/ident))
(map #(rename-keys % {:invoice-payment/_invoice :invoice/payments}))))
(-> x
(update :invoice/date c/from-date)
(update :invoice/status :db/ident)
(rename-keys {:invoice-payment/_invoice :invoice/payments})))
(defn raw-graphql [args]
(->> (d/query
(cond-> {:query {:find [default-read]
(defn raw-graphql-ids [db args]
(->> (cond-> {:query {:find []
:in ['$]
:where ['[?e :invoice/invoice-number]]}
:args [(d/db (d/connect uri))]}
(:sort-by args) (add-sorter-field {"client" ['[?e :invoice/client ?c]
'[?c :client/name ?sorter]]
"vendor" ['[?e :invoice/vendor ?v]
'[?v :vendor/name ?sorter]]
"description-original" ['[?e :transaction/description-original ?sorter]]
"date" ['[?e :invoice/date ?sorter]]
"invoice-number" ['[?e :invoice/invoice-number ?sorter]]
"total" ['[?e :invoice/total ?sorter]]
"outstanding" ['[?e :invoice/outstanding-balance ?sorter]]}
args)
(limited-clients (:id args))
(merge-query {:query {:in ['[?xx ...]]
:where ['[?e :invoice/client ?xx]]}
@@ -89,67 +87,53 @@
(merge-query {:query {:in ['?invoice-number-like]
:where ['[?e :invoice/invoice-number ?invoice-number]
'[(.contains ^String ?invoice-number ?invoice-number-like)]]}
:args [(:invoice-number-like args)]})))
(map first)
(<-datomic)))
:args [(:invoice-number-like args)]})
true
(merge-query {:query {:find ['?base-date '?e]
:where ['[?e :invoice/date ?base-date]]}}) )
(d/query)
(apply-sort-2 args)
(apply-pagination args)))
(defn sort-fn [args]
(cond
(= "client" (:sort-by args))
#(-> % :invoice/client :client/name .toLowerCase)
(= "vendor" (:sort-by args))
#(-> % :invoice/vendor :vendor/name .toLowerCase)
:else
(keyword "invoice" (:sort-by args))))
(defn graphql-results [ids db args]
(let [results (->> (d/pull-many db default-read ids)
(group-by :db/id))
invoices (->> ids
(map results)
(map first)
(mapv <-datomic))]
invoices))
(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 100)))))
(let [db (d/db (d/connect uri))
{ids-to-retrieve :ids matching-count :count} (raw-graphql-ids db args)]
(defn count-graphql [args]
(->> (raw-graphql args)
(count)))
[(->> (graphql-results ids-to-retrieve db args))
matching-count]))
(defn get-by-id [id]
(->>
(d/query (-> {:query {:find [default-read]
:in ['$]
:where []}
:args [(d/db (d/connect uri))]}
(add-arg '?e id ['?e])))
(map first)
(<-datomic)
(first)))
(-> (d/db (d/connect uri))
(d/pull default-read id)
(<-datomic)))
(defn update [update]
@(d/transact (d/connect uri) [update])
(get-by-id (:db/id update) ))
(defn get-multi [ids]
(->>
(d/query {:query {:find [[default-read '...]]
:in ['$ ['?e '...]]
:where [['?e]]}
:args [(d/db (d/connect uri))
ids] }
)
(<-datomic)))
(map <-datomic
(-> (d/db (d/connect uri))
(d/pull-many default-read ids))))
(defn find-conflicting [{:keys [:invoice/invoice-number :invoice/vendor :invoice/client :db/id]}]
(->> (d/query
(cond-> {:query {:find [default-read]
(cond-> {:query {:find [(list 'pull '?e default-read)]
:in ['$ '?invoice-number '?vendor '?client '?invoice-id]
:where '[[?e :invoice/invoice-number ?invoice-number]
[?e :invoice/vendor ?vendor]
@@ -157,9 +141,10 @@
[(not= ?e ?invoice-id)]
]}
:args [(d/db (d/connect uri)) invoice-number vendor client (or id 0)]}))
:args [(d/db (d/connect uri)) invoice-number vendor client (or id 0)]}
true (doto println)))
(map first)
(<-datomic)))
(map <-datomic)))

View File

@@ -14,12 +14,11 @@
[clojure.set :as set]))
(defn get-invoice-page [context args value]
(println "HI")
(let [args (assoc args :id (:id context))
invoices (map
->graphql
(d-invoices/get-graphql (<-graphql (assoc args :id (:id context)))))
invoice-count (d-invoices/count-graphql (<-graphql args))]
[{:invoices invoices
[invoices invoice-count] (d-invoices/get-graphql (<-graphql (assoc args :id (:id context))))]
(println "HELLO" (take 1 invoices ) invoice-count)
[{:invoices (map ->graphql invoices)
:total invoice-count
:count (count invoices)
:start (:start args 0)