Fork me on GitHub
#datascript
<
2019-12-17
>
samcferrell02:12:54

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?

cjsauer04:12:15

@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"]]

cjsauer04:12:07

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.

doglooksgood05:12:38

@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?

cjsauer05:12:00

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

cjsauer05:12:10

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

doglooksgood05:12:33

@cjsauer thanks!