Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add init-fn for datomic component #123

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
[im.chit/hara.io.watch "2.1.7"]
[im.chit/hara.io.scheduler "2.3.6"]
[im.chit/adi "0.3.2"]
[com.datomic/datomic-free "0.9.4815.12"]
[com.datomic/datomic-free "0.9.5656"]
[com.novemberain/monger "3.0.2"]
[org.clojure/java.jdbc "0.3.5"]
[com.h2database/h2 "1.4.181"]
Expand Down
12 changes: 8 additions & 4 deletions src/system/components/datomic.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@
(:require [com.stuartsierra.component :as component]
[datomic.api :as d]))

(defrecord Datomic [uri conn]
(defrecord Datomic [uri conn init-fn]
component/Lifecycle
(start [component]
(let [db (d/create-database uri)
conn (d/connect uri)]
conn (d/connect uri)
_ (when init-fn (init-fn conn))]
(assoc component :conn conn)))
(stop [component]
(when conn (d/release conn))
(assoc component :conn nil)))

(defn new-datomic-db [uri]
(map->Datomic {:uri uri}))
(defn new-datomic-db
([uri]
(map->Datomic {:uri uri}))
([uri init-fn]
(map->Datomic {:uri uri :init-fn init-fn})))
28 changes: 28 additions & 0 deletions test/system/components/datomic_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[clojure.test :refer [deftest testing is]]))

(def uri "datomic:mem://localhost:4334/framework-test")

(def datomic-db (new-datomic-db uri))

(deftest datomic-lifecycle
Expand All @@ -14,3 +15,30 @@
datomic.peer.LocalConnection))
(is (d/delete-database uri))
(alter-var-root #'datomic-db component/stop)))

(def schema '[{:db/ident ::name
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/doc "a test attr"}])

(defn init-schema [conn]
@(d/transact conn schema))

(def datomic-db-with-schema (new-datomic-db uri
init-schema))

(defn has-attribute?
"Does database have an attribute named attr-name?"
[db attr-name]
(-> (d/entity db attr-name)
:db.install/_attribute
boolean))

(deftest datomic-lifecycle-with-schema
(testing "Datomic lifecycle operations."
(alter-var-root #'datomic-db-with-schema component/start)
(is (= (type (:conn datomic-db-with-schema))
datomic.peer.LocalConnection))
(is (has-attribute? (d/db (:conn datomic-db-with-schema)) ::name))
(is (d/delete-database uri))
(alter-var-root #'datomic-db-with-schema component/stop)))