Skip to content

Commit

Permalink
Support JS arrays as lookup refs in datoms and queries (closes tonsky…
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Sep 26, 2016
1 parent 7dada20 commit 4b7e2c4
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# WIP

- ~30% better performance for predicate and fn calls in queries
- JS API now supports lookup refs in index lookups and queries (#179)

# 0.15.3

Expand Down
9 changes: 5 additions & 4 deletions src/datascript/db.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -740,10 +740,11 @@
nil
:else
(:e (first (-datoms db :avet eid))))
:else
(raise "Expected number or lookup ref for entity id, got " eid
{:error :entity-id/syntax
:entity-id eid})))
#?@(:cljs [(array? eid) (recur db (array-seq eid))])
:else
(raise "Expected number or lookup ref for entity id, got " eid
{:error :entity-id/syntax
:entity-id eid})))

(defn entid-strict [db eid]
(or (entid db eid)
Expand Down
4 changes: 2 additions & 2 deletions test/datascript/test/lookup_refs.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,11 @@
[{:db/id 1 :id 1 :name "Ivan" :age 11 :friend 2}
{:db/id 2 :id 2 :name "Petr" :age 22 :friend 3}
{:db/id 3 :id 3 :name "Oleg" :age 33 }])]
(is (= (set (d/q '[:find [?v ...]
(is (= (set (d/q '[:find ?e ?v
:in $ ?e
:where [?e :age ?v]]
db [:name "Ivan"]))
#{11}))
#{[[:name "Ivan"] 11]}))

(is (= (set (d/q '[:find [?v ...]
:in $ [?e ...]
Expand Down
16 changes: 13 additions & 3 deletions test/js/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,15 +307,25 @@ function test_pull() {
function test_lookup_refs() {
var schema = {"name": {":db/unique": ":db.unique/identity"}};
var db = d.db_with(d.empty_db(schema),
[{":db/id": 1, "name": "Ivan"},
{":db/id": 2, "name": "Oleg"}]);

[{":db/id": 1, "name": "Ivan", "age": 18},
{":db/id": 2, "name": "Oleg", "age": 32}]);
// entity
assert_eq("Ivan", d.entity(db, ["name", "Ivan"]).get("name"));
// pull
assert_eq({"name": "Ivan"}, d.pull(db, '["name"]', ["name", "Ivan"]));
assert_eq_set(
[{"name": "Ivan"}, {"name": "Oleg"}],
d.pull_many(db, '["name"]', [["name", "Ivan"], ["name", "Oleg"]])
);
// index access
assert_eq_datoms([[1, "age", 18, tx0+1],
[1, "name", "Ivan", tx0+1]],
d.datoms(db, ":eavt", ["name", "Ivan"]));
// queries
assert_eq([[["name", "Ivan"], 18]],
d.q('[:find ?e ?a\
:in $ ?e \
:where [?e "age" ?a]]', db, ["name", "Ivan"]));
}

function test_resolve_current_tx() {
Expand Down

0 comments on commit 4b7e2c4

Please sign in to comment.