Dieter Komendera / Oct 11 2018
with Martin Kavalar
Datomic and Datalog
Datomic and Datalog
First we ensure we have a Datomic available in our runtime by providing a deps.edn
.
echo '{:deps {com.datomic/datomic-free {:mvn/version "0.9.5697"}}}' > deps.edn clj -Sforce -e ":ok"
For the purpose of this exploration, we're just setting up an in-memory db and connect to it.
(require '[datomic.api :as d]) (def uri "datomic:mem://hello") (d/create-database uri) (def conn (datomic.api/connect uri))
Now we can add the schema to the database.
(def movie-schema [{:db/ident :movie/title :db/valueType :db.type/string :db/cardinality :db.cardinality/one :db/doc "The title of the movie"} {:db/ident :movie/genre :db/valueType :db.type/string :db/cardinality :db.cardinality/one :db/doc "The genre of the movie"} {:db/ident :movie/release-year :db/valueType :db.type/long :db/cardinality :db.cardinality/one :db/doc "The year the movie was released in theaters"}]) @(d/transact conn movie-schema)
Continue with adding some data.
(def first-movies [{:movie/title "The Goonies" :movie/genre "action/adventure" :movie/release-year 1985} {:movie/title "Commando" :movie/genre "action/adventure" :movie/release-year 1985} {:movie/title "Repo Man" :movie/genre "punk dystopia" :movie/release-year 1984}]) @(d/transact conn first-movies)
(def db (d/db conn))
(def all-movies-q '[:find ?e :where [?e :movie/title]]) (into [] (d/q all-movies-q db))
(def all-titles-q '[:find ?movie-title :where [_ :movie/title ?movie-title]]) (d/q all-titles-q db)