-
Notifications
You must be signed in to change notification settings - Fork 0
/
datomic.clj
234 lines (178 loc) · 5.89 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
(ns toolbelt.datomic
(:require [datomic.api :as d]
[clojure.spec.alpha :as s]
[clojure.string :as string]))
;; =============================================================================
;; DatabaseReference
;; =============================================================================
(defprotocol DatabaseReference
(as-db [_] [_ _]))
(def sync-timeout-ms (* 10 1000))
(extend-protocol DatabaseReference
datomic.db.Db
(as-db
([db] db))
datomic.Connection
(as-db
([conn] (d/db conn))
([conn t] (let [db-value-or-timeout (-> (d/sync conn t)
(deref sync-timeout-ms :timeout))]
(cond
(= (type db-value-or-timeout) datomic.db.Db)
db-value-or-timeout
(= db-value-or-timeout :timeout)
(throw
(ex-info "Datomic timed out during sync attempt."
{:waited-ms sync-timeout-ms}))
:else
(throw
(ex-info "An unknown error occured while Datomic was trying to sync."
{})))))))
;; =============================================================================
;; EntityReference
;; =============================================================================
(defprotocol EntityReference
(id [_])
(entity [_ db]))
(extend-protocol EntityReference
datomic.query.EntityMap
(id [e] (:db/id e))
(entity [e db] e)
java.lang.Long
(id [id] id)
(entity [id db] (d/entity (as-db db) id))
;; these two are for working with maps composed for a transaction
clojure.lang.PersistentArrayMap
(id [m] (:db/id m))
(entity [m db] (entity (id m) (as-db db)))
clojure.lang.PersistentHashMap
(id [m] (:db/id m))
(entity [m db] (entity (id m) (as-db db)))
;; nice for when working with the output of d/datoms and d/seek-datoms
datomic.db.Datum
(id [[e]] e)
(entity [[e] db] (entity e (as-db db)))
;; nice for reaching schema or enum entities quickly,
;; however doesn't return a numeral for id like the rest do
clojure.lang.Keyword
(id [kw] kw)
(entity [kw db] (let [db (as-db db)]
(entity (d/entid db kw) (as-db db))))
;; these two are when you have a naked entity (or schema entity) id
java.lang.Long
(id [id] id)
(entity [id db] (d/entity (as-db db) id))
java.lang.Integer
(id [id] id)
(entity [id db] (d/entity (as-db db) id))
;; lookup refs. doesn't work with id, because we don't get the db there.
clojure.lang.PersistentVector
(id [lookup]
(assert (= 2 (count lookup))
"A valid lookup ref has two elements.")
(assert (keyword? (first lookup))
"The first element of a valid lookup is a keyword.")
lookup)
(entity [lookup db] (d/entity (as-db db) lookup)))
;; =============================================================================
;; Predicates
;; =============================================================================
(defn conn?
"Is `x` a Datomic connection?"
[x]
(instance? datomic.Connection x))
(defn db? [x]
(satisfies? DatabaseReference x))
(defn entity? [x]
(satisfies? EntityReference x))
(defn entityd? [x]
(instance? datomic.query.EntityMap x))
;; =============================================================================
;; Entity
;; =============================================================================
(defn entities
"Maps `entity` over `entids`, producing a seq of entities."
[db & entids]
(map
(fn [entid]
(entity entid db))
entids))
(s/fdef entities
:args (s/cat :db db?
:entids (s/+ entity?))
:ret (s/+ entityd?))
;; =============================================================================
;; Queries
;; =============================================================================
(defn remap-query
"Remove the `args` key from query map `m` to create a proper input to
`datomic.api/query`."
[{args :args :as m}]
{:query (dissoc m :args)
:args args})
(defn safe-wildcard
"Produce a function for performing fulltext search with a wildcard
character (*) that won't throw errors."
[q]
(fn [args]
(if (= (last q) \space)
(conj args (string/trim q))
(conj args (str q "*")))))
(defn updated-at
"Produce the instant in which `entity` was last updated."
[db entity]
(d/q '[:find (max ?tx-time) .
:in $ ?e
:where
[?e _ _ ?tx _]
[?tx :db/txInstant ?tx-time]]
(d/history db) (:db/id entity)))
(s/fdef updated-at
:args (s/cat :db db? :entity entity?)
:ret inst?)
(defn created-at
"Produce the instant in which `entity` was created."
[db entity]
(d/q '[:find (min ?tx-time) .
:in $ ?e
:where
[?e _ _ ?tx _]
[?tx :db/txInstant ?tx-time]]
(d/history db) (:db/id entity)))
(s/fdef created-at
:args (s/cat :db db? :entity entity?)
:ret inst?)
(defn eav-tx-q*
[db e a v]
(let [init '{:find [?tx .]
:in [$ ?e]
:args []
:where []}]
(-> init
(update :args conj (d/history db) e)
(update :where conj ['?e a v '?tx]))))
(defn eav-tx
[db e a v]
(->> (eav-tx-q* db e a v)
(remap-query)
(d/query)
(d/entity db)))
(defn last-modified-to
"Produce the instant in time at which attribute `a` was last modified to `v`
on entity `e`."
[db e a v]
(:db/txInstant (eav-tx db e a v)))
(defn mapify
"Convert a Datomic entity to a Clojure map."
[e]
(reduce
(fn [acc [a v]]
(let [acc (cond
(and (set? v) (every? entityd? v)) (assoc acc a (map mapify v))
(entityd? v) (assoc acc a (mapify v))
:otherwise (assoc acc a v))]
(if (entityd? e)
(assoc acc :db/id (:db/id e))
acc)))
nil
e))