datascript

Immutable database and Datalog query engine for Clojure, ClojureScript and JS
2019-12-17T02:55:54.026400Z

I find myself called (ds/entity ...) a lot... is there a way to return an entity or a set of entities from a query directly?

cjsauer 2019-12-17T04:00:15.030400Z

@samcferrell you can use pull in the find specification of a query, for example:

[:find (pull ?e [:artist/startYear :artist/endYear])
 :where [?e :artist/name "The Beatles"]]
You can get something similar to ds/entity by using a wildcard in the pull expression:
[:find (pull ?e [*])
 :where [?e :artist/name "The Beatles"]]

cjsauer 2019-12-17T04:10:07.035300Z

Couple differences tho. entity is lazy, and can be used with functions like get-in to “walk” edges. pull is eager, and * won’t actually bring in the “edge entities” on its own.

tianshu 2019-12-17T05:15:38.037200Z

@cjsauer thanks for explain in detail! I did not realize that I can use this [:user/id 1] format as a value! It's this a new feature in recent updates?

cjsauer 2019-12-17T05:25:00.038500Z

@doglooksgood no it isn’t new. Lookup refs are a primary feature, likely inspired by datomic.

cjsauer 2019-12-17T05:26:10.039100Z

You’ll find that quite a bit of the Datomic documentation also applies to DataScript.

tianshu 2019-12-17T05:43:33.039500Z

@cjsauer thanks!