Fork me on GitHub
#datomic
<
2018-01-06
>
hansw12:01:36

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...

hansw12:01:27

Or do I have to find the entity-id and then use db/pull?

hansw12:01:56

hmm i guess supplying the ident-value directly to pull should work 🙂

timgilbert16:01:42

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

timgilbert16:01:16

So like:

(defn pull-star [db key]
  (d/q '[:find (pull ?e [*]) .
         :in $ ?k
         :where [?e :attr/nr ?k]]
    db key))

timgilbert16:01:19

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])

timgilbert16:01:04

Or more simply: (pull db '[*] [:attr/nr 23])

timgilbert16:01:34

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?

hansw17:01:28

@timgilbert thanks a bunch!

hansw18:01:59

@timgilbert does (d/entity my-db 23) assume i mapped my identity column as :db/unique :db.unique/value?

hansw18:01:13

as opposed to db/unique/identity

hansw18:01:31

i'm not quite sure about the difference anyway from the reference

timgilbert18:01:08

OH, YEAH, SORRY, THAT WAS UNCLEAR

timgilbert18:01:15

Ha, caps lock, oops

hansw18:01:59

i was kind of confused by datomic magically figuring out what to relate the number 23 to 🙂

hansw18:01:45

oh and, as a datomic novice i too can not think of a use for :db/uri

timgilbert18:01:46

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

hansw18:01:25

yes, ok, so my key is indeed an external identifier

timgilbert18:01:37

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])

timgilbert18:01:38

Datomic resolves that for you to “the single entity ID which has the attribute :attr/nr set to 23

hansw18:01:13

got it, thnx

hansw18:01:18

I've come to realize how much I have been relying on types to understand an API in my career, until now...

hansw18:01:40

but that's a different conversation 🙂

hansw18:01:44

about that uri thing... i seem to remember Stuart Holloway saying to avoid :db/uri in the Day of Datomic videos

timgilbert18:01:35

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

timgilbert18:01:56

It’s not like I have a lot of love for the Java.URI API in the first place

hansw18:01:32

i had a similar surprise about java.util.Data for Inst values

hansw18:01:36

being a mutable type and all

hansw18:01:53

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

hansw18:01:48

so i'm guessing java.util.Date is on its way out in Daytomic

timgilbert19:01:29

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