This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-01-06
Channels
- # architecture (17)
- # beginners (118)
- # boot (22)
- # cider (9)
- # clara (38)
- # cljs-dev (4)
- # clojure (213)
- # clojure-austin (4)
- # clojure-greece (1)
- # clojure-italy (4)
- # clojure-russia (5)
- # clojure-spec (7)
- # clojure-uk (5)
- # clojurescript (3)
- # cursive (21)
- # datascript (3)
- # datomic (31)
- # duct (12)
- # emacs (9)
- # graphql (5)
- # gsoc (1)
- # hoplon (9)
- # leiningen (2)
- # off-topic (37)
- # om (2)
- # onyx (2)
- # parinfer (5)
- # perun (4)
- # reagent (2)
- # spacemacs (1)
- # specter (25)
Does datomic have an equivalent query for SELECT * FROM account WHERE nr = 23
? Note that nr
is a natural key. In the tutorials I see lots of examples for querying specific attributes but I would like to query all attributes at once, avoiding a second trip with db/pull
...
You might use the Entity API for something like that, so you’d have (let [e (d/entity my-db 23)])
. Then you can pull fields out of e
as you need them. The most direct analog to SELECT *
would probably be (pull [*])
though, which gives you every attribute of the entity in question as data
So like:
(defn pull-star [db key]
(d/q '[:find (pull ?e [*]) .
:in $ ?k
:where [?e :attr/nr ?k]]
db key))
As you get deeper into datomic, you may find yourself passing lookup refs around for this kind of thing, so your key value might be [:attr/nr 23]
and your pull-star function would look more like:
(defn pull-star [db ref]
(d/q '[:find (pull ?r [*]) .
:in $ ?r]
db ref))
(pull-star db [:attr/nr 23])
Or more simply:
(pull db '[*] [:attr/nr 23])
Say, can anyone explain to me why datomic has a built-in :db.type/uri
? Is there any reason to use this instead of a :db.type/string
for storing a URI?
@timgilbert thanks a bunch!
@timgilbert does (d/entity my-db 23)
assume i mapped my identity column as :db/unique :db.unique/value
?
OH, YEAH, SORRY, THAT WAS UNCLEAR
Ha, caps lock, oops
i was kind of confused by datomic magically figuring out what to relate the number 23 to 🙂
Sorry, that was unclear. There I was thinking of 23 as an entity ID, sort of a built-in natural key. The other examples I was thinking of it as an external identifier (eg, something with :db/unique :db.unique/identity
But in practice you probaly want to use an external unique key, and the entity API would look more like (d/entity db [:attr/nr 23])
Datomic resolves that for you to “the single entity ID which has the attribute :attr/nr
set to 23
”
I've come to realize how much I have been relying on types to understand an API in my career, until now...
about that uri thing... i seem to remember Stuart Holloway saying to avoid :db/uri
in the Day of Datomic videos
Yeah, most of the mailing list messages I’ve seen suggest it was an early artifact, I’m going to pretend it doesn’t exist
It’s not like I have a lot of love for the Java.URI API in the first place
and joda-time has been around forever... But i saw in Clojure 1.9 there is support for taking a java.time.Instant as a clojure Instant
That would be nice, though I suspect they wouldn’t ever ditch it wholesale. There’s a good Clojure wrapper around the java,time stuff too, if you haven’t seen it: https://github.com/dm3/clojure.java-time