Malcolm Ocean's roamblog

Roam API notes

created 2020-10-03 · edited 2020-10-03 · open in Roam
  • As of October 2nd, 2020, the API is in Alpha. I'm one of the people with access. These notes are at present for future reference for me and made public here for use by other people in the Alpha.
  • I have made a JS wrapper client for the REST API, for use in nodeJS although I guess you could use it in the frontend too
  • After a lot of fucking around, I've got a query that returns all pages and their children:
    • [:find (pull ?page [:node/title :block/uid :block/string {:block/children ...}])
        :where
          [?page :node/title]
          [?page :block/children]
      ]
    • (for my purposes it ignores empty pages, but you can remove the [?page :block/children] line if you want empty pages too)
    • explanation of the pull command for those also fairly new to datalog:it seems like the way the {recursive ...} thing works, you specify all of the keys that you want on everything at all layers, and then it recurses those. I was initially really lost because recursing on :block.children was working great for blocks but wasn't showing anything for pages. well, turns out I was only telling it to show :node/title , and so it would look at all of those children and say "nothing there!" and wouldn't even return an empty array
    • anyway, turns out that :node/title seems to be one of the only unique page properties. its uid property is still :block/uid
  • Someone
    • Jason Benn's Datomic tutorial notes
      • A datom is a 4-tuple.
      • A data pattern is a 5-tuple.
      • Datom attributes are namespaced and prefixed with :
      • Pattern variables are symbols prefixed with ?
      • How to use many data patterns in a :where clause.
      • Query inputs use :in.
        • First arg is database: $
        • Tuple inputs can be destructured.
        • IN is implemented as [?value ...]
      • Attribute names are themselves datoms with attribute :db/ident
      • Attributes can either be symbols or the attribute IDs.
      • Predicates are triple data patterns wrapped in parens.
        • <, >, <=, >=, =, not=
      • Transformation functions can be used in data patterns: wrap the function and arguments in parens, bind results as second part of tuple.
      • Aggregate functions go in the find clause wrapped in parens: min, max, sum, avg.
      • Rules are vectors of data patterns. The head of the rule is a vector of the symbol and its arguments, conventionally wrapped in parens.
        • To use rules in queries, use :in $ %; % represents the rule body.
        • In line 10: reusing a rule head is equivalent to retyping the whole rule
          • [[(friends ?p1 ?p2)
              [?m :movie/cast ?p1]
              [?m :movie/cast ?p2]
              [(not= ?p1 ?p2)]]
             
             [(friends ?p1 ?p2) 
              [?m :movie/cast ?p1] 
              [?m :movie/director ?p2]]
             
             [(friends ?p1 ?p2) (friends ?p2 ?p1)]
             ]
    • Pull patterns go in the :find clause, are analogous to SELECT.
      • attributes in children denoted with {}
      • Recursive pull with {:block/children ...}