tonsky / datascript

Immutable database and Datalog query engine for Clojure, ClojureScript and JS
Eclipse Public License 1.0
5.45k stars 304 forks source link

`entity` returns entities for ids that don't exist #447

Closed lynaghk closed 9 months ago

lynaghk commented 1 year ago

The docs suggest that entity should return nil when the provided id doesn't actually correspond to an entity in the database:

https://github.com/tonsky/datascript/blob/1a405721cb2226a28678066b176a5326c264153e/src/datascript/core.cljc#L31-L33

but in fact it always returns an entity. E.g., with datascript 1.4.1 on JVM Clojure:

(d/entity (d/empty-db) 100500) ;; => #:db{:id 100500}

The only check seems to be that the number isn't above emax

https://github.com/tonsky/datascript/blob/1a405721cb2226a28678066b176a5326c264153e/src/datascript/db.cljc#L1042-L1044

I would prefer the behavior described in the docs, which is useful when "rebasing" entities by their id across different databases (i.e., one can use keep or other Clojure nil punning fns to discard any entities that may have been removed in a derived database).

Happy to submit a patch if maintainers agree that the documented behavior is, in fact, the desired one.

refset commented 1 year ago

Arguably related: https://github.com/tonsky/datascript/issues/187

logseq-cldwalker commented 9 months ago

Looks like this was fixed with 1.4.2. Should this be closed?