Skip to content

Commit 77d8b7b

Browse files
committed
Add init-fn for datomic component
test demonstrates the ability to init and check for schema
1 parent 4f4b876 commit 77d8b7b

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

project.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
[im.chit/hara.io.watch "2.1.7"]
2121
[im.chit/hara.io.scheduler "2.3.6"]
2222
[im.chit/adi "0.3.2"]
23-
[com.datomic/datomic-free "0.9.4815.12"]
23+
[com.datomic/datomic-free "0.9.5656"]
2424
[com.novemberain/monger "3.0.2"]
2525
[org.clojure/java.jdbc "0.3.5"]
2626
[com.h2database/h2 "1.4.181"]

src/system/components/datomic.clj

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@
22
(:require [com.stuartsierra.component :as component]
33
[datomic.api :as d]))
44

5-
(defrecord Datomic [uri conn]
5+
(defrecord Datomic [uri conn init-fn]
66
component/Lifecycle
77
(start [component]
88
(let [db (d/create-database uri)
9-
conn (d/connect uri)]
9+
conn (d/connect uri)
10+
_ (when init-fn (init-fn conn))]
1011
(assoc component :conn conn)))
1112
(stop [component]
1213
(when conn (d/release conn))
1314
(assoc component :conn nil)))
1415

15-
(defn new-datomic-db [uri]
16-
(map->Datomic {:uri uri}))
16+
(defn new-datomic-db
17+
([uri]
18+
(map->Datomic {:uri uri}))
19+
([uri init-fn]
20+
(map->Datomic {:uri uri :init-fn init-fn})))

test/system/components/datomic_test.clj

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
[clojure.test :refer [deftest testing is]]))
66

77
(def uri "datomic:mem://localhost:4334/framework-test")
8+
89
(def datomic-db (new-datomic-db uri))
910

1011
(deftest datomic-lifecycle
@@ -14,3 +15,30 @@
1415
datomic.peer.LocalConnection))
1516
(is (d/delete-database uri))
1617
(alter-var-root #'datomic-db component/stop)))
18+
19+
(def schema '[{:db/ident ::name
20+
:db/valueType :db.type/string
21+
:db/cardinality :db.cardinality/one
22+
:db/doc "a test attr"}])
23+
24+
(defn init-schema [conn]
25+
@(d/transact conn schema))
26+
27+
(def datomic-db-with-schema (new-datomic-db uri
28+
init-schema))
29+
30+
(defn has-attribute?
31+
"Does database have an attribute named attr-name?"
32+
[db attr-name]
33+
(-> (d/entity db attr-name)
34+
:db.install/_attribute
35+
boolean))
36+
37+
(deftest datomic-lifecycle-with-schema
38+
(testing "Datomic lifecycle operations."
39+
(alter-var-root #'datomic-db-with-schema component/start)
40+
(is (= (type (:conn datomic-db-with-schema))
41+
datomic.peer.LocalConnection))
42+
(is (has-attribute? (d/db (:conn datomic-db-with-schema)) ::name))
43+
(is (d/delete-database uri))
44+
(alter-var-root #'datomic-db-with-schema component/stop)))

0 commit comments

Comments
 (0)