Skip to content

Commit ced0c79

Browse files
authored
fix(query): bools and combined queries (#43)
- fix queries on boolean attributes - allow queries on multiple attributes of an entity
1 parent dcfbaf5 commit ced0c79

File tree

2 files changed

+47
-10
lines changed

2 files changed

+47
-10
lines changed

src/homebase/js.cljs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,17 @@
105105
(defn js->datalog [data]
106106
(let [{find "$find" where "$where"} (js->clj data)]
107107
{:find [(symbol (str "?" find))]
108-
:where (mapv
109-
(fn build-where-clause [[e av]]
110-
(let [[[a v]] (seq av)
111-
pred [(symbol (str "?" e))
112-
(keyword e a)]]
113-
(if (= v "$any") pred
114-
(into pred [v]))))
115-
where)}))
108+
:where (reduce-kv
109+
(fn [acc nmspc attrs+values]
110+
(into acc
111+
(reduce-kv
112+
(fn [acc a v]
113+
(conj acc
114+
(let [pred [(symbol (str "?" nmspc)) (js->key nmspc a)]]
115+
(if (= v "$any") pred
116+
(into pred [v])))))
117+
[] attrs+values)))
118+
[] where)}))
116119

117120
(comment
118121
(=
@@ -327,7 +330,8 @@ For example: query({
327330
(str "Expected to see '" var "' in both the $find and $where clauses."
328331
(example-js-query var)))
329332

330-
#"((?! is not ISeqable).+) is not ISeqable"
333+
;; #"((?! is not ISeqable).+) is not ISeqable"
334+
#"No protocol method IKVReduce.-kv-reduce defined for type .*: (.*)"
331335
:>> (fn [[_ v]]
332336
(str "Expected $where clause to be a nested object, not " v "."
333337
(example-js-query)))

src/homebase/js_test.cljs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88
(d/conn-from-db
99
(d/init-db
1010
#{(d/datom 3 :todo/project 2)
11-
(d/datom 2 :project/name "abc")}
11+
(d/datom 2 :project/name "abc")
12+
(d/datom 4 :project/name "xyz")
13+
(d/datom 4 :project/number 23)
14+
(d/datom 4 :project/completed? true)
15+
(d/datom 5 :project/name "abc")
16+
(d/datom 6 :project/name "p4")}
1217
{:todo/project {:db/valueType :db.type/ref
1318
:db/cardinality :db.cardinality/one}})))
1419

@@ -110,6 +115,29 @@
110115
"$where" {"item" {"name" "$any"}}})
111116
(d/create-conn))))
112117
(is (array? (hbjs/q (clj->js "[:find ?e :where [?e :item/name]]") (d/create-conn)))))
118+
(testing "$any"
119+
(is (= 4 (count (hbjs/q (clj->js {"$find" "project"
120+
"$where" {"project" {"name" "$any"}}})
121+
test-conn)))))
122+
(testing "filter by string"
123+
(is (= 2 (count (hbjs/q (clj->js {"$find" "project"
124+
"$where" {"project" {"name" "abc"}}})
125+
test-conn)))))
126+
(testing "filter by bool"
127+
(is (= 1 (count (hbjs/q (clj->js {"$find" "project"
128+
"$where" {"project" {"isCompleted" true}}})
129+
test-conn)))))
130+
(testing "filter by number"
131+
(is (= 1 (count (hbjs/q (clj->js {"$find" "project"
132+
"$where" {"project" {"number" 23}}})
133+
test-conn)))))
134+
(testing "filter by multiple"
135+
(is (= 1 (count (hbjs/q (clj->js {"$find" "project"
136+
"$where" {"project" {"number" 23 "isCompleted" true}}})
137+
test-conn))))
138+
(is (= 0 (count (hbjs/q (clj->js {"$find" "project"
139+
"$where" {"project" {"number" 23 "isCompleted" false}}})
140+
test-conn)))))
113141
(testing "should fail with humanized errors"
114142
(is (thrown-with-msg?
115143
js/Error
@@ -145,6 +173,11 @@
145173
#"(?s)Expected \$where clause to be a nested object, not 1.*For example:"
146174
(hbjs/q (clj->js {"$find" "todo"
147175
"$where" {"todo" 1}}) (d/create-conn))))
176+
(is (thrown-with-msg?
177+
js/Error
178+
#"(?s)Expected \$where clause to be a nested object, not yolo.*For example:"
179+
(hbjs/q (clj->js {"$find" "todo"
180+
"$where" {"todo" "yolo"}}) (d/create-conn))))
148181
(is (thrown-with-msg?
149182
js/Error
150183
#"(?s)Cannot parse :find, expected: \(find-rel \| find-coll \| find-tuple \| find-scalar\)"

0 commit comments

Comments
 (0)