Skip to content

Commit

Permalink
Entity should return nil for numeric eids that don't correspond to en…
Browse files Browse the repository at this point in the history
…tities.

Ref: #447
  • Loading branch information
lynaghk committed Mar 22, 2023
1 parent 1a40572 commit b4cea89
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 17 deletions.
4 changes: 1 addition & 3 deletions src/datascript/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@
(def ^{:arglists '([db eid])
:doc "Given lookup ref `[unique-attr value]`, returns numberic entity id.
If entity does not exist, returns `nil`.
For numeric `eid` returns `eid` itself (does not check for entity existence in that case)."}
If entity does not exist, returns `nil`."}
entid db/entid)


Expand Down
3 changes: 3 additions & 0 deletions src/datascript/db.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,9 @@
(raise "Expected number or lookup ref for entity id, got " eid
{:error :entity-id/syntax, :entity-id eid})))

(defn numeric-eid-exists? [db eid]
(= eid (-> (-seek-datoms db :eavt [eid]) first :e)))

(defn entid-strict [db eid]
(or (entid db eid)
(raise "Nothing found for entity id " eid
Expand Down
3 changes: 2 additions & 1 deletion src/datascript/impl/entity.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
(defn entity [db eid]
{:pre [(db/db? db)]}
(when-let [e (entid db eid)]
(->Entity db e (volatile! false) (volatile! {}))))
(when (db/numeric-eid-exists? db e)
(->Entity db e (volatile! false) (volatile! {})))))

(defn- entity-attr [db a datoms]
(if (db/multival? db a)
Expand Down
2 changes: 1 addition & 1 deletion test/datascript/test/entity.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
(is (nil? (d/entity db "abc")))
(is (nil? (d/entity db :keyword)))
(is (nil? (d/entity db [:name "Petr"])))
(is (= 777 (:db/id (d/entity db 777))))
(is (nil? (d/entity db 777)))
(is (thrown-msg? "Lookup ref attribute should be marked as :db/unique: [:not-an-attr 777]"
(d/entity db [:not-an-attr 777])))))

Expand Down
2 changes: 1 addition & 1 deletion test/datascript/test/lookup_refs.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
{:db/id 1 :name "Ivan"}

[[:db.fn/retractEntity [:name "Ivan"]]]
{:db/id 1})
nil)

(are [tx msg] (thrown-msg? msg (d/db-with db tx))
[{:db/id [:name "Oleg"], :age 10}]
Expand Down
21 changes: 10 additions & 11 deletions test/js/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,7 @@ function test_entity() {
assert_ident(db, e.db);

var e2 = d.entity(db, 2);
assert_eq(null, e2.get("name"));
assert_eq(null, e2.get("aka"));
assert_eq(2, e2.get(":db/id"));
assert_eq(null, e2);

// js interop
assert_eq_set(["name", "aka"], e.key_set());
Expand Down Expand Up @@ -283,10 +281,10 @@ function test_entity_refs() {
var db = d.db_with(d.empty_db(schema),
[{":db/id": 1, "children": [10]},
{":db/id": 10, "father": 1, "children": [100, 101]},
{":db/id": 100, "father": 10}]);
{":db/id": 100, "father": 10},
{":db/id": 101, "father": 10}]);

var e = function(id) { return d.entity(db, id); };

assert_eq_refs([10], e(1).get("children"));
assert_eq_refs([101, 100], e(10).get("children"));

Expand All @@ -299,11 +297,11 @@ function test_entity_refs() {
assert_eq_refs([10], e(10).get("father").get("children"));

// backward navigation
assert_eq (null, e(1).get("_children"));
assert_eq_refs([10], e(1).get("_father"));
assert_eq_refs([1], e(10).get("_children"));
assert_eq_refs([100], e(10).get("_father"));
assert_eq_refs([1], e(100).get("_children")[0].get("_children"));
assert_eq (null, e(1).get("_children"));
assert_eq_refs([10], e(1).get("_father"));
assert_eq_refs([1], e(10).get("_children"));
assert_eq_refs([100, 101], e(10).get("_father"));
assert_eq_refs([1], e(100).get("_children")[0].get("_children"));
}

function test_pull() {
Expand All @@ -313,7 +311,8 @@ function test_pull() {
var db = d.db_with(d.empty_db(schema),
[{":db/id": 1, "name": "Ivan", "children": [10]},
{":db/id": 10, "father": 1, "children": [100, 101]},
{":db/id": 100, "father": 10}]);
{":db/id": 100, "father": 10},
{":db/id": 101, "father": 10}]);

var actual, expected;

Expand Down

0 comments on commit b4cea89

Please sign in to comment.