/
core.clj
337 lines (310 loc) · 11.5 KB
/
core.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
(ns connective.core
(:require
[taoensso.timbre :as log]
[connective.adapter :as adapter]
[connective.schema :as schema]
[connective.entity :as entity]))
(defmacro defn-of-adaptor
[sym]
`(defn ~sym
[~'adapter ~'context ~'arg]
(~(symbol "adapter" (str sym)) ~'adapter ~'context ~'arg)))
(defmacro defn-of-adaptor-2
[sym log-fn]
`(defn ~sym
[~'adapter ~'context ~'arg]
(let [r# (~(symbol "adapter" (str sym)) ~'adapter ~'context ~'arg)]
(~log-fn (str '~sym) ~'arg r#)
r#)))
(defn- log-read
[op args result]
(let [p (select-keys args [::entity/kind ::entity/id])]
(log/debug :op op :ident p)))
(defn- log-delete
[op args result]
(let [p (select-keys result [::entity/kind ::entity/id])]
(log/debug :op op :ident p)))
(defn- log-write
[op args result]
(let [p (select-keys result [::entity/ident])]
(log/debug :op op :entity p)))
(defn-of-adaptor related-query)
(defn-of-adaptor reference-query)
(defn-of-adaptor execute-query)
(defn-of-adaptor init-entity)
(defn-of-adaptor-2 write-entity log-write)
(defn-of-adaptor-2 read-entity log-read)
(defn-of-adaptor-2 delete-entity log-delete)
(defn-of-adaptor reference-value)
(defn validator
[a]
(adapter/validator a))
(def attributes entity/attributes-of-entity)
(def context entity/context-of-entity)
(def ident entity/ident-of-entity)
(def relationships entity/relationships-of-entity)
(def compile-schema schema/compile)
(defmacro def-collection-fn
[sym-plural sym-singular]
`(defn ~sym-plural
[~'adapter ~'context ~'coll]
(for [~'item ~'coll]
(~sym-singular ~'adapter ~'context ~'item))))
(def-collection-fn init-entities init-entity)
(def-collection-fn write-entities write-entity)
(def-collection-fn read-entities read-entity)
(def-collection-fn delete-entities delete-entity)
(defn update-rels
[entity
upfn]
(let [new-rels (upfn (entity/relationships-of-entity entity))]
(entity/assoc-relationships entity new-rels)))
(defn update-attrs
[entity
upfn]
(let [new-attrs (upfn (entity/attributes-of-entity entity))]
(entity/assoc-attributes entity new-attrs)))
(defn- assoc-parent-relationship
[entity
context
{::keys [parent rel-opts]}]
(let [relation-key (entity/relationship-key-of-reference-relation-of-entity
context
entity
rel-opts)]
(assoc-in entity [::relationships relation-key] parent)))
(defn walk-rel-tree
[a
{::entity/keys [entity
parent
parent-relation]
::keys [node-fn
filter-fn]
:or {filter-fn (fn [_ _ {::entity/keys [related-entity]}]
(nil? related-entity))}
:as context}]
(let [relationships (entity/relationship-groups-of-entity context entity)
entity (reduce
(fn
[e*
{::entity/keys [relationship-key
related-entity]
:as rel-ctx}]
(if (filter-fn a context rel-ctx)
e*
(let [context (->
context
(assoc ::entity/entity related-entity)
(update ::entity/rel-path (fnil conj []) relationship-key)
(dissoc
::entity/parent
::entity/parent-relation))
related-entity (walk-rel-tree a context)]
;; unsure, but maybe set the corresponding
;; many relationship in the related entity to
;; ::entity/parent instead of having it not
;; exist, in order to tell if it should be
;; validated or not
(assoc-in e* [::entity/relationships relationship-key] related-entity))))
entity
(get relationships ::entity/reference))
[rel-type {::entity/keys [ref-attribute] :as rel-opts}] parent-relation
entity (case rel-type
(::entity/reference,nil) (node-fn
a
context
entity)
::entity/many (let [parent-rel-key (entity/relationship-key-of-reference-relation-of-entity
context
entity
rel-opts)
_ (assert (some? parent-rel-key))
entity (assoc-in entity [::entity/relationships parent-rel-key] parent)
entity (node-fn a context entity)]
(assert (some? ref-attribute))
(->
entity
(assoc-in [::entity/relationships parent-rel-key] ::entity/parent)
(assoc-in [::entity/attributes ref-attribute] ::entity/parent))))]
(reduce
(fn
[e*
{::entity/keys [relationship-key
relation
related-entity]
:as rel-ctx}]
(if (filter-fn a context rel-ctx)
e*
(let [context (->
context
(update ::entity/rel-path (fnil conj []) relationship-key)
(assoc
::entity/parent entity
::entity/parent-relation relation))
related-entities (doall
(for [e related-entity]
(walk-rel-tree
a
(assoc context ::entity/entity e))))]
(assoc-in e* [::entity/relationships relationship-key] related-entities))))
entity
(get relationships ::entity/many))))
(defn- init-rels*
[a
context
entity]
(walk-rel-tree
a
(assoc
context
::node-fn init-entity
::entity/entity entity)))
(defn init-rels
[a
context
entity]
(init-rels* a context entity))
(defn- write-rels*
[a
context
entity]
(walk-rel-tree
a
(assoc
context
::node-fn write-entity
::entity/entity entity)))
(defn write-rels
[a
context
entity]
(write-rels* a context entity))
(defn pull-rel-tree
[a
{::entity/keys [entity
parent
parent-relation]
::keys [query-rels]
:as context}]
(let [relationships (entity/relationship-groups-of-entity context entity)
query-rels (::entity/relationships query-rels)
content (assoc context ::entity/parent entity)
entity (reduce
(fn
[e*
{::entity/keys [relationship-key
relation]}]
(if (contains? query-rels relationship-key)
(let [[_ {::entity/keys [ref-attribute]
:as rel-opts}] relation
_ (assert (some? ref-attribute))
ref-value (get-in e* [::entity/attributes ref-attribute])
related-entity (->>
(assoc rel-opts ::entity/ref-value ref-value)
(reference-query a context)
(execute-query a context))
context (->
context
(assoc
::query-rels (get query-rels relationship-key)
::entity/parent-relation relation
::entity/entity related-entity))
related-entity (pull-rel-tree a context)]
(assoc-in e* [::entity/relationships relationship-key] related-entity))
e*))
entity
(get relationships ::entity/reference))
ref-value (reference-value a context entity)]
(reduce
(fn
[e*
{::entity/keys [relationship-key
relation
related-entity]
:as ctx}]
(if (contains? query-rels relationship-key)
(let [[_ {::entity/keys [ref-attribute]
:as rel-opts}] relation
_ (assert (some? ref-attribute))
related-entities (->>
(assoc rel-opts ::entity/ref-value ref-value)
(related-query a context)
(execute-query a context))
context (->
context
(assoc
::query-rels (get query-rels relationship-key)
::entity/parent-relation relation))
related-entities (doall
(for [e related-entities]
(let [rel-key (entity/relationship-key-of-reference-relation-of-entity
context
entity
rel-opts)
e (pull-rel-tree
a
(assoc context ::entity/entity e))]
(->
e
(assoc-in [::entity/attributes ref-attribute] ::entity/parent)
(assoc-in [::entity/relationships rel-key] ::entity/parent)))))]
(assoc-in e* [::entity/relationships relationship-key] related-entities))
e*))
entity
(get relationships ::entity/many))))
(defn- read-rels*
[a
context
ident]
(pull-rel-tree
a
(assoc
context
::entity/entity (read-entity a context ident))))
(defn read-rels
[a
context
{::entity/keys [relationships]
:as ident}]
(read-rels*
a
(assoc
context
::query-rels {::entity/relationships relationships})
(dissoc ident ::entity/relationships)))
(defn- delete-rels*
[a
context
{::keys [delete-all]
:as entity}]
(walk-rel-tree
a
(assoc
context
::node-fn (fn
[a ctx entity]
(delete-entity a ctx (entity/ident-of-entity entity))
entity)
::filter-fn (fn [_ {::entity/keys [rel-path]
::keys [delete-all]}
{::entity/keys [related-entity
relationship-key]}]
(let [q-path (mapcat
vector
rel-path
(repeat (count rel-path) ::entity/relationships))
delete-rels (get-in delete-all q-path)]
(or
(nil? related-entity)
(not (contains? delete-rels relationship-key)))))
::delete-all delete-all
::entity/entity (dissoc entity ::delete-all))))
(defn delete-rels
[a
context
ident]
(delete-rels* a context ident))
(comment
(macroexpand-1 '(defn-of-adaptor-2 related-query log-op))
(macroexpand-1 '(def-collection-fn write-entities write-entity))
)