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