Fork me on GitHub
#xtdb
<
2021-09-02
>
sheluchin19:09:55

Is it possible to destructure a logical variable in one of the subsequent :where clauses?

(>defn get-card-ids-from-note
  [node {note-id :note/id}]
  [::ss/crux-node ::ss/note-entity
   => (s/coll-of ::ss/card-entity :kind vector?)]
  (->>
    (query node
     {:find '[id]
      :keys '[card/id]
      :where '[[card :type ::ss/card]
               [card :card/card-list card-list-id]
               [card-list :card-list/id card-list-id]
               [card-list :card-list/note note-id]
               [card :card/id id]]
      :args [{'note-id note-id}]})
    vec))
Here the second clause assigns to card-list-id, which is a map when in the context of a card (`{:card/card-list {:card-list/id 1}}`), but just its value is used in the context of a card-list, so it's just an integer there (`:card-list/id 1`). In the third clause, I need the integer value, not the entire map from the previous clause.

refset21:09:56

Hi 🙂 yep it's definitely possible to extract the values like this (though not destructuring as you're used to in the Clojure sense). Essentially you can use get,`get-in` and other clojure.core functions to bind additional intermediate variables for this sort of thing, e.g.:

[card :type ::ss/card]
[card :card/card-list card-list-id]
[(get card-list-id :card-list/id) card-list-id-num]
[card-list :card-list/id card-list-id-num]
Also, completely unrelated, but note that :args is deprecated in favour of :in, so like:
(query node
     {:find '[id]
      :keys '[card/id]
      :in '[note-id]
      :where '[...]}
      note-id)

sheluchin22:09:16

Hi @U899JBRPF. Oh, cool, that's a nice syntax for this. Makes it pretty straight forward for my current objective. I wonder if there is a more sophisticated querying mechanism I should be using for cases like this, but I'll get there in the docs eventually 🙂 That makes sense about :args. I think I learned about it in the space tutorial, but noticed it wasn't mentioned in the Querying reference page. Thanks for the heads up on the deprecation and the help with my query.

🙏 2
Steven Deobald01:09:12

> I think I learned about it in the space tutorial @UPWHQK562 I'll make an issue to fix this in the tutorial internally (have some repo rejigging madness going on at the moment, which is why the issue isn't public at the moment).