Logseq Advanced Query supports the creation of functions to re-use in queries. These functions are are stored in the config.edn
.
Query Transform collection
Define in config.edn
, under :query/result-transforms
your collection of the query functions.
Step 1
Add the following query transforms in the config.edn
;; Pre-defined :result-transform function to use in Query
:query/result-transforms
{:sort-by-priority (fn [result] (sort-by (fn [h] (get h :block/priority "Z")) result))
:sort-by-scheduled (fn [result] (sort-by (fn [d] (get d :block/scheduled)) result))
:sort-by-deadline (fn [result] (sort-by (fn [d] (get d :block/deadline)) result))
:sort-by-scheduled-reverse (fn [result] (sort-by (comp - (fn [d] (get d :block/scheduled))) result))
:sort-by-deadline-reverse (fn [result] (sort-by (comp - (fn [h] (get h :block/deadline))) result))
:sort-by-priority-and-deadline
(fn [result] (sort-by (juxt (fn [h] (get h :block/priority)) (fn [i] (get i :block/deadline)))))
:sort-by-scheduled-and-deadline
(fn [result] (sort-by (juxt (fn [r] (get-in r [:block/scheduled])) (fn [r] (get-in r [:block/deadline]))) result))
:sort-by-deadline-and-scheduled
(fn [result] (sort-by (juxt (fn [d] (get d :block/deadline)) (fn [d] (get d :block/scheduled))) result))
:sort-by-deadline-or-scheduled
(fn [result]
(sort-by (fn [b]
(or (get b :block/scheduled) (get b :block/deadline))) result))
:sort-by-updated-at
(fn [result] (sort-by (fn [h] (get h :block/updated-at))))
:sort-by-created-at
(fn [result] (sort-by (fn [h] (get h :block/created-at))))
:sort-by-time-tracking
(fn [r]
(sort-by
(fn [h]
(clojure.string/join (or
(not-empty
(rest
(re-find
(re-pattern "\\-\\-\\[(\\d+)\\-(\\d+)\\-(\\d+)\\s.*\\s(\\d+):(\\d+):(\\d+)\\]") (get h :block/content)))) "99999999999999"))) r))
:sort-by-journal-day
(fn [result] (sort-by (fn [s] (get s :block/journal-day)) (fn [a b] (compare b a)) result))
:sort-by-property
(fn [result]
(sort-by (fn [s]
(get s :block/journal-day)) (fn [a b] (compare b a)) result))}
Step 2
In your advance query, add your query function for a simple a re-usable
Example of the query using the query/transforms functions
:sort-by-time-tracking
;; In an advanced query
{ :title "Sort By Time Tracked"
:query [:find (pull ?b [*])
:in $ ?current
:where
[?p :block/name ?current]
[?b :block/page ?p]
[?b :block/marker "TODO"]
]
:inputs [:query-page]
:result-transform :sort-by-time-tracking
}
Query View collection
Coming Soon!!!
Resources
Documentation
What is next?
- Share your query/transforms and query/views in the comment. They will be added to the collection for the community.
Thank you to community members for sharing queries where most of these functions were pulled from, in Discord or the forum.