Skip to content

Instantly share code, notes, and snippets.

@jebberjeb
Last active December 30, 2015 04:39
Show Gist options
  • Save jebberjeb/7777320 to your computer and use it in GitHub Desktop.
Save jebberjeb/7777320 to your computer and use it in GitHub Desktop.

Datomic

What

  • keystore data, w/o the tradeoffs of other nosql databases

  • ACID tx, joins, datalog, simple schema

  • "Facts dont change when you incorporate time"

    • SQL db in past, CRUD triggers, history tables

Why check it out?

The promise of tracking historical data for free. For something like AG WIFE, could be useful for saying "get settings (as of last week)".

Data Model

Datoms

[entity id,  attribute,  value,  tx time]
[42, :owner/name, "jeb", #inst "2013-11-15T17:06:24.182-00:00"]

Schema (EDN)

[{:db/id #db/id[:db.part/db]
  :db/ident :owner/name
  :db/valueType :db.type/string
  :db/cardinality :db.cardinality/one
  :db/doc "An owner's name"
  :db.install/_attribute :db.part/db}]

Database

A bunch of datoms! (+ Indeces)

Programming Model

Querying - Datalog

  • Prolog subset (forked 1977)
  • Declarative, logic programming
  • Uses data (maps, sets), rather than string-based DSL
  • Datalog > SQL ?

Find all pet owners

[:find ?name
 :where [_ :owner/name ?name]]

Find a pet owners id

[:find ?eid
 :in $ ?owner-name
 :where [?eid :owner/name ?owner-name]]

How bout querying the past?

(require '[datomic.api :as d])
(def conn "datomic:mem://pets")

;; Current time
(d/q '[:find ?owner-name
       :where [_ :owner/name ?owner-name]]
     (d/db conn)))

;; 12-01-2013
(d/q '[:find ?owner-name
       :where [_ :owner/name ?owner-name]]
     (d/as-of (d/db conn) (java.util.Date. 113 11 1)))

My Experience - mapping to known problem domain

Existing Knowledge

  • Interface IDataSource (5 fn)
  • vs SQLite
  • 4-6 hours of learning time
  • familiarity w/ Clojure data literals

Outcome

  • simplicity of queries
  • Unit testing (in-mem storage out of the box)
  • Small mismatch between programming & data models ??
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment