This repository has been archived by the owner on May 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.cljs
166 lines (147 loc) · 4.86 KB
/
util.cljs
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
(ns trident.firestore.util
(:require-macros
[cljs.core.async.macros :refer [go]])
(:require
[clojure.set :as set]
[clojure.spec.alpha :as s]
[clojure.string :as str]
[clojure.walk :as walk]
[oops.core :refer [oget ocall oapply+]]
[trident.util :as u]))
(defn timestamp? [x]
(fn? (u/catchall-js (aget x "toDate"))))
(defn path->ident [path]
(let [table-pos (- (count path) 2)
table (nth path table-pos)
id (u/pred-> (u/dissoc-vec path table-pos)
#(= 1 (count %)) first)]
[table id]))
(defn keywordize-path [path]
(vec (map-indexed (fn [i x] (cond-> x (even? i) keyword)) path)))
(defn path-str->ident [path-str]
(-> path-str
(str/split "/")
keywordize-path
path->ident))
(defn ident->path [[table id]]
(let [id (u/pred-> id string? vector)
doc-id (when (odd? (count id))
(last id))
parent-collection (cond-> id
doc-id butlast)]
(concat parent-collection
[table]
(when doc-id
[doc-id]))))
(defn path->ref [firestore path]
(->> path
(map-indexed vector)
(reduce (fn [obj [i id]]
(if (even? i)
(ocall obj :collection (name id))
(ocall obj :doc id)))
firestore)))
(defn ->query [ident-or-query]
(-> (cond->> ident-or-query
(not (map? ident-or-query)) (hash-map :ident))
(update :ident ident->path)
(set/rename-keys {:ident :path})))
(defn ident->ref [firestore ident]
(path->ref firestore (ident->path ident)))
(defn query->ref [firestore q]
(let [{:keys [path collection-group where order-by limit limit-to-last
start-at start-after end-at end-after]} (->query q)
r (if collection-group
(ocall firestore :collectionGroup (name collection-group))
(path->ref firestore path))
r (reduce (fn [r [attr op value]]
(ocall r :where (name attr) (name op) (clj->js value)))
r
where)
r (reduce (fn [r [arg method]]
(if arg
(oapply+ r method (u/wrap-vec arg))
r))
r
[[(some->> order-by (map (comp #(map name %) u/wrap-vec))) :orderBy]
[start-at :startAt]
[start-after :startAfter]
[end-at :endAt]
[end-after :endAfter]
[limit :limit]
[limit-to-last :limitToLast]])]
r))
(s/def ::doc-ident
(s/tuple keyword?
(s/or
:doc string?
:subcoll (s/cat
:coll (s/+ (s/cat :table keyword? :id string?))
:doc string?))))
(defn coerce-from-fb [doc]
(walk/postwalk
(fn [x]
(if-some [path-str (u/catchall-js (oget x :path))]
(path-str->ident path-str)
(cond-> x
(timestamp? x) (ocall :toDate))))
doc))
(defn coerce-coll-idents [firestore coll]
((if (map? coll)
u/map-vals
mapv)
#(cond->> %
(s/valid? ::doc-ident %) (ident->ref firestore))
coll))
(defn coerce-to-fb [firestore x]
(walk/postwalk
(fn [x]
; We can't just check (s/valid? ::doc-ident x) here because it matches
; key-value pairs in maps.
(cond
(coll? x) (coerce-coll-idents firestore x)
:default x))
x))
(defn doc->clj [^js doc]
(let [ident (-> doc
(oget "ref.path")
path-str->ident)]
(-> (ocall doc :data)
(js->clj :keywordize-keys true)
coerce-from-fb
(assoc :ident ident))))
(defn doc->changeitem [{:keys [doc exists]}]
(let [{:keys [ident] :as ent} (doc->clj doc)]
[ident (when exists ent)]))
(defn query-snapshot->changeset [snapshot]
(for [change (ocall snapshot :docChanges)
:let [exists (not= "removed" (oget change :type))]]
(doc->changeitem {:doc (oget change :doc)
:exists exists})))
(defn doc->changeset [doc]
[(doc->changeitem {:doc doc
:exists (oget doc :exists)})])
(defn doc-query? [q]
(let [{:keys [collection-group path]} (->query q)]
(and (nil? collection-group)
(even? (count path)))))
(defn write-unsafe
"See https://clojure.atlassian.net/browse/ASYNC-192"
[firestore changeset]
(let [promises
(for [[ident ent] changeset
:let [have-doc-id (doc-query? ident)
r (ident->ref firestore ident)
js-ent (clj->js (coerce-to-fb firestore ent))
merge-ent (:merge (meta ent))
update-ent (:update (meta ent))]]
(cond
(nil? ent) (ocall r :delete)
(not have-doc-id) (ocall r :add js-ent)
merge-ent (ocall r :set js-ent #js {:merge true})
update-ent (ocall r :update js-ent)
:default (ocall r :set js-ent)))]
(doall promises)
(go
(doseq [p promises]
(u/js<! p)))))