Combine queries: child text with parent block properties

I have a query where I can return either the child block (child block, page) or the parent block (parent block, page, and properties). What I can’t figure out is how/if I can return these results together (child block, parent block, page and properties). These blocks span various pages. There are duplicate parent block “contents”, e.g. “@Example2020” appearing twice in the example below.

Query to return the child block:

#+BEGIN_QUERY
{:query [:find (pull ?child [*])
  :where
   [?child :block/ref-pages ?page]
   [?page :block/name "tldr"]
   [?child :block/parent ?parent]
   [?parent :block/properties ?props]
   [(get ?props :project) ?proj]
   [(= ?proj "research notes")]
 ]
:remove-child-block? false
}
#+END_QUERY

Result:

and

To return the parent block:

#+BEGIN_QUERY
{:query [:find (pull ?parent [*])
  :where
   [?child :block/ref-pages ?page]
   [?page :block/name "tldr"]
   [?child :block/parent ?parent]
   [?parent :block/properties ?props]
   [(get ?props :project) ?proj]
   [(= ?proj "research notes")]
 ]
:remove-child-block? false
}
#+END_QUERY

Result:

The desired result would look like:

I tried this strategy I learned from the max-datomic tutorial:

?child [* {:block/parent 
				[* {:block/properties [*]}]
			       }
			    ]

along with some variations, but couldn’t get it to work. Here is content you can use for a minimal working example:

- @Example2020
  purpose:: this is an example
  topic:: query
  keywords:: test1, test2
  links:: [google url sample](https://google.com)
  project:: research notes
	- #TLDR this is a summary of all the notes i'd take below. it is always the first block under the propertiers. but the text can be a few paragraphs, so i'd like to avoid making it a property. it doesn't really make sense to have a summary as a property anyway.
	- Notes
		- more notes
			- notes notes notes
		- notes
	- notes are fun
- @Example2020
  purpose:: this is an example
  topic:: query
  keywords:: test1, test2
  links:: [google url sample](https://google.com)
  project:: research notes
	- #TLDR more summary
- @Different1989
  purpose:: this is an example
  topic:: query
  keywords:: test1, test2
  links:: [google url sample](https://google.com)
  project:: research notes
	- #TLDR we are writing a summary

Something like this:

#+BEGIN_QUERY
{:query [:find (pull ?parent [*]) ?content
   :keys parent content
   :where
     [?child :block/ref-pages ?page]
     [?page :block/name "tldr"]
     [?child :block/parent ?parent]
     [?parent :block/properties ?props]
     [(get ?props :project) ?proj]
     [(= ?proj "research notes")]
     [?child :block/content ?content]
 ]
 :result-transform (fn [result]
   (map (fn [r]
     (update (:parent r) :block/properties (fn [p]
       (assoc p "Child Block" (:content r) )
     ) )
   ) result)
 )
 :remove-child-block? false
}
#+END_QUERY
1 Like

Works perfect - thank you! Looks like I need to learn result-transform part of queries next.

I would consider simplifying things, by:

  • turning the block’s current content into a property
    • e.g. name:: @Example2020
  • writing the summary as the block’s content
    • not needing the child-block anymore
      • using a query for the parent only
      • deprecating #TLDR