Skip to content

Commit

Permalink
Added with-system macro
Browse files Browse the repository at this point in the history
  • Loading branch information
markbastian committed Apr 29, 2020
1 parent c808ca9 commit 8dc1593
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
16 changes: 5 additions & 11 deletions src/partsbin/datahike/api/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
(derive ::connection ::datahike/connection)

(comment
(require '[partsbin.system :refer [create-system]])
(require '[partsbin.system :refer [with-system]])

(def schema
[{:db/ident :name
Expand All @@ -19,20 +19,14 @@
:initial-tx schema}
::connection {:db-config (ig/ref ::database)}})

(create-system config)

(start)

(let [{conn ::connection} (system)]
(with-system
[{conn ::connection} config]
(d/transact conn [{:name "Mark"}
{:name "Becky"}]))

(let [{conn ::connection} (system)]
{:name "Becky"}])
(d/q
'[:find [?n ...]
:in $
:where
[_ :name ?n]]
@conn))

(stop))
)
7 changes: 7 additions & 0 deletions src/partsbin/system.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,10 @@
(alter-var-root ~'#'*system* (fn [~'s] (when ~'s (do (ig/halt! ~'s) nil)))))

(defn ~'restart [] (do (~'stop) (~'start)))))

(defmacro with-system [[bindings config] & body]
`(let [system# (ig/init ~config)
~bindings system#]
(try
~@body
(finally (ig/halt! system#)))))

0 comments on commit 8dc1593

Please sign in to comment.