mium
Type
Drop #82··Datalog Patterns Cookbook

Datalog Patterns Cookbook··5 recipes for mium power users

Copy-ready queries, honest gotchas

Team Mut
builds mium··Falktron, Oelde
audio··3:00
▶ Audio coming soon
Transcript

What if a Datalog query were not the tool you fear, but a recipe you copy and adjust just once? That is exactly what this cookbook is about — five recipes from everyday mium use, each useful on its own, each with an honest point where it does not hold.

Datalog is a logic query language from the seventies that Rich Hickey rediscovered with Datomic for practical everyday database work. mium uses a Datalog dialect to query its own block index — your notes, edges and properties become data of which you ask precise questions. You need not learn the language from the ground up; you need patterns that already work.

Recipe one answers the standard sprint question: which tasks are open and must be delivered by the end of the quarter? The query filters on the deadline within the quarter window and on the status, and returns the owner. The honest point: ISO date strings hold only if the deadline is maintained consistently as year-month-day. Mixed formats break the comparison silently — the query then returns an empty set, not an error.

Recipe two shows you the central nodes of your vault: the ten most often referenced blocks. Those are often terms, persons, projects — the places where your knowledge converges. The trade-off: the count treats transclusive embeds and pure display wikilinks as equal. If you wish to count only real semantic references, you add a filter on a property marker.

Recipe three reads the audit trail of a single block: who changed what, and when? The query reads the transaction history in reverse for a given block ID. The prerequisite is that the history is enabled — the default since mium nought point four. In older vaults the necessary edges are missing, and for very old blocks the history may be compacted and show only the latest state.

Recipe four aggregates client tags across all vaults into a sorted frequency list, and recipe five tracks down orphans — notes with no incoming backlinks, the dead ends of your vault. Both assume property and namespace discipline; without it they return empty sets instead of insight. And the orphan detector with its not-join is expensive — on large vaults it belongs in an overnight cron run, not in the interactive session.

Datalog is not a universal instrument, and that is the most important message: for full text in the block content, for unstructured vaults and for vague browsing there are better ways. But where your question is precise and your properties are maintained, your second brain hands back, on a copy-ready query, exactly the answer that has long been sitting in the blocks. Which recipe fits for you — and which runs into the void — tells you more about your vault than about the language.

This cookbook is meant for mium power users and developer profiles who have already brushed against [[advanced-query-datalog]] and are looking for concrete, copy-ready examples. Five recipes, each self-contained — anyone who needs only the orphan detector may jump straight to recipe 5. Datalog itself is a logic programming language from the 1970s; Rich Hickey rediscovered it with Datomic (Cognitect, since 2012) for practical everyday database work. mium uses a Datalog dialect to query its own block index.

Recipes

The five recipes are independent of one another. Cherry-pick what you need, or read linearly — both orders work. Every recipe has: a problem, a query, and optionally a gotcha callout.

Recipe 1··Open tasks with a deadline in the current quarter

The standard sprint question: which tasks are open and must be delivered by the end of the quarter? The query filters on `frist::` within the Q window and `status:: offen`, and returns the owner.

[:find ?block ?frist ?owner
 :where [?block :frist ?frist]
        [?block :status "offen"]
        [(>= ?frist "2026-04-01")]
        [(<= ?frist "2026-06-30")]
        [?block :owner ?owner]]

Recipe 2··Top 10 backlink hubs

Which blocks are referenced most often? The top 10 by incoming `:links-to` edges are the central nodes of your vault — often terms, persons, projects.

[:find (count ?backlink) ?target
 :where [?backlink :links-to ?target]
 :order-by [(count ?backlink) :desc]
 :limit 10]

Recipe 3··Audit trail of a block

Who changed what, and when, on a block? The query reads the transaction history in reverse for a given block ID and returns `tx`, `attr`, `value` and the `added` flag.

[:find ?tx ?attr ?value ?added
 :where [?block :db/id "<((uuid))>"]
        [?tx :tx/affected-block ?block]
        [?tx :tx/attr ?attr]
        [?tx :tx/value ?value]
        [?tx :tx/added ?added]]

Recipe 4··Cross-vault tag aggregation

How often does each client tag occur across all vaults? The query groups on `tags::` with the namespace prefix `#kunde-` and returns a sorted frequency list.

[:find (count ?block) ?tag
 :where [?block :tags ?tag]
        [(re-pattern "^#kunde-") ?tag]
 :group-by ?tag
 :order-by [(count ?block) :desc]]

Recipe 5··Orphan note detector

Which notes have no incoming backlinks and are not archived? These orphans are the dead ends of the vault — candidates for tidying up, linking or archiving.

[:find ?block ?title
 :where [?block :title ?title]
        [?block :archiv? false]
        (not-join [?block]
          [?other :links-to ?block])]

When these recipes do NOT fit

Datalog queries are not a universal instrument. Three situations in which these recipes do not hold:

  1. Full-text search in the block content — Datalog filters on properties and edges, not on prose. For “find every block that contains `Auftragsbestätigung` in its text”, the mium full-text index is the right instrument, not a Datalog query.
  2. Very unstructured vaults with no property discipline — all five recipes assume that `frist::`, `status::`, `owner::`, `tags::` are maintained consistently. In a vault where 80 % of blocks carry no properties, the queries return empty sets, not insight. Property discipline is groundwork, not a side effect.
  3. Ad-hoc explorations with an unclear question — Datalog rewards precise questions. Anyone who merely wishes to “have a look at what is in the vault” gets there faster with the graph view or backlink browsing than with a query that still has to be designed.

An honest question

Which of the five recipes fit for you, and which ran into the void? If you put one of them into production: what did you parameterise differently — shifted the Q window, changed the tag namespace, raised the limit? And if none fit: where did we miss the gap? Cookbook recipes live by rubbing up against real vaults — feedback on which recipe #6 is missing here is most welcome. Further on in this wave: [[advanced-query-datalog]] for the language fundamentals, [[simple-query]] for the gentler entry without Datalog syntax.

The Spring School has concluded. See Day 93 → Play the game →