Fork me on GitHub
#biff
<
2023-03-23
>
Mike Sugarbaker16:03:33

Hello all. I have some Very Baby questions about queries that I will thread.

Mike Sugarbaker16:03:43

So I've got this code:

(biff/q db '{:find [(pull user [*])] 
             :where [[user :user/id ?uid]]})

Mike Sugarbaker16:03:20

And it's not finding any users... even though I'm logged in as one. I tried some iterations without pull but they're long gone.

Mike Sugarbaker16:03:33

haaaaaalp thanks

Mike Sugarbaker16:03:11

(I'm looking for your basic find-all for an admin page)

Jacob O'Bryant16:03:44

is this with the default biff project? if so, you can replace :user/id with :user/email. :user/id isn't part of the default app's schema (:xt/id is the closest equivalent, but it isn't specific to users or any other document type)

Mike Sugarbaker17:03:42

I see :user/id :uuid in the schema but I might have put it there for some reason? The code would have to populate it anyway...

Mike Sugarbaker17:03:55

pprint says I'm getting a result! thanks. but it's disturbing that there's no way to say "just show me everything"

Jacob O'Bryant17:03:51

oh yeah, that could be confusing--the :user/id there is basically an alias for :xt/id. it's a malli thing You can query for all the documents if you do

(q db '{:find [(pull doc [*])] 
        :where [[doc :xt/id]]})

Mike Sugarbaker18:03:57

I've attempted to generalize this:

(defn find-all-with-key [db key]
  (biff/q db '{:find (pull doc [*])
               :in [key]
               :where [[doc key ?val]]}
          key))

Mike Sugarbaker18:03:27

but am getting the error xtdb.IllegalArgumentException: Query didn't match expected structure which one wishes would be more specific

Jacob O'Bryant18:03:21

yeah, it's a limitation of :in arguments--they can only be used as variables in the entity or value positions, not in the attribute positions. To do dynamic attributes you have to do it like this:

(defn find-all-with-key [db key]
  (biff/q db {:find '(pull doc [*])
              :where [['doc key '?val]]}))

Mike Sugarbaker19:03:41

Oh right, you can do a bunch of little quotes instead of one big one. I even saw that somewhere and spaced it. Thanks again!

👌 2