-
Notifications
You must be signed in to change notification settings - Fork 0
/
datomic.clj
63 lines (56 loc) · 1.64 KB
/
datomic.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
(ns common-datomic.datomic
(:require [clojure.core]
[clojure.walk :as walk]
[common-datomic.config.datomic :as config]
[datomic.api :as d]
[schema.core :as s]))
(defn remove-datomic-meta
[entity]
(if (map? entity)
(-> entity
(dissoc :db/id)
(dissoc :db/ident)
(dissoc :db/doc)
(dissoc :db/cardinality)
(dissoc :db/valueType))
entity))
(defn entity->model
[entity]
(->> entity
first
(walk/prewalk remove-datomic-meta)))
(s/defn assoc-db-id
[entity]
(if (map? entity)
(assoc entity :db/id (d/tempid :db.part/user))
entity))
(s/defn insert!
[entity :- {s/Keyword s/Any}]
(let [entity-with-db-id (walk/postwalk assoc-db-id entity)]
@(d/transact @config/connection [entity-with-db-id])))
(s/defn query-inputs
[partial-query
inputs]
(let [final-query (partial partial-query (first inputs))
rest-inputs (rest inputs)]
(if (empty? rest-inputs)
final-query
(query-inputs final-query rest-inputs))))
(s/defn db-query :- (s/maybe [{s/Keyword s/Any}])
([query :- [s/Any]]
(let [db (d/db @config/connection)
result (d/q query db)]
(map entity->model result)))
([query :- [s/Any]
& inputs :- [s/Any]]
(let [db (d/db @config/connection)
partial-query (partial d/q query db)
complete-query (query-inputs partial-query (first inputs))
result (complete-query)]
(map entity->model result))))
(s/defn entities :- (s/maybe [{s/Keyword s/Any}])
([query :- [s/Any]]
(db-query query))
([query :- [s/Any]
& inputs :- [s/Any]]
(db-query query inputs)))