> January 7th, 2022

[[Querying Roam]]

Daily note

This whole website comes from my block database. Right now, it hits an API that reads directly from Roam by loading the whole thing in a headless browser instance since Roam doesn’t have an API.


It’s a broken mess, and horribly slow.


Luckily, you can export your Roam database. I can automate exporting and uploading to a server, which then can directly read from the db. Not only is this way faster, I could potentially replicate the db across the world on multiple servers.


It also makes development way faster. In fact, that’s the primary reason I looked into this. When changing the queries, I’d have to restart the headless browser which takes 20-30 seconds. It’s a terrible development flow, but now I can snapshot the data and reload instantly.


Once you export your data as EDN, here’s how you load and query it:

let ds = require('datascript');
let x = "foo\n fdfs"
let edn = require('jsedn');

let d = require('fs').readFileSync('./personal.edn', 'utf8');
let data = edn.parse(d);

let conn = ds.conn_from_datoms(
  edn.toJS(edn.atPath(data, ':datoms')),
  edn.toJS(edn.atPath(data, ':schema'))

let query = '[:find (pull ?b [*]) :where [_ ":block/uid" "01-06-2022"]]';
let result = ds.q(query, ds.db(conn));

// [[5389]]

Back references (0)