Skip to content

Instantly share code, notes, and snippets.

@cldwalker
Last active January 31, 2022 00:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cldwalker/ec240fbfeaaa6c347fdcfaf8d48b2114 to your computer and use it in GitHub Desktop.
Save cldwalker/ec240fbfeaaa6c347fdcfaf8d48b2114 to your computer and use it in GitHub Desktop.
Example logseq queries of joining across blocks by user defined properties

This gist demonstrates joining across user defined block properties. To start, paste the following 3 blocks in Logseq:

- type:: person
  name:: foo
- type:: comment
  author:: foo
  text:: bar
- #+BEGIN_QUERY
  {:title "Query joining blocks by user defined properties"
   :query [:find (pull ?b2 [*])
         :where
         [?b :block/properties ?p]
         [(get ?p :type) ?type]
         [(= "person" ?type)]
         [(get ?p :name) ?name]
         [?b2 :block/properties ?p2]
         [(get ?p2 :author) ?name]]
   }
  #+END_QUERY

The third block should show the second block ?b2 as a result.

To pull a specific property from a joined block:

- #+BEGIN_QUERY
{:title "Query pulling a specific property value from a joined block"
 :query [:find ?text
       :where
       [?b :block/properties ?p]
       [(get ?p :type) ?type]
       [(= "person" ?type)]
       [(get ?p :name) ?name]
       [?b2 :block/properties ?p2]
       [(get ?p2 :author) ?name]
       [(get ?p2 :text) ?text]]
 }
#+END_QUERY

This will pull out "bar"

To make the query easier to read, we could use an input and define a rule (block-property) to make the first query:

- #+BEGIN_QUERY
{:title "Query joining user defined block properties with a rule"
 :query [:find (pull ?b2 [*])
       :in $ ?type %
       :where
       (block-property ?b :type ?type)
       (block-property ?b :name ?name)
       (block-property ?b2 :author ?name)]
  :inputs ["person"
           [[(block-property ?b ?property-name ?property-value)
            [?b :block/properties ?p]
            [(get ?p ?property-name) ?property-value]]]]
 }
#+END_QUERY
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment