net.clojars.macielti/datomicnet.clojars.macielti/datomic is a thin Clojure library that wraps a Datomic Peer connection as an Integrant component. It handles:
mocked-datomic helper for isolated in-memory connections in testsAdd the dependency to your project.clj:
[net.clojars.macielti/datomic "1.0.0"]
Or with deps.edn:
net.clojars.macielti/datomic {:mvn/version "1.0.0"}
The component is identified by the key ::datomic.component/datomic in your Integrant config map.
{:datomic.component/datomic {:schemas [...]
:components {:config {:datomic-uri "datomic:mem://mydb"}}}}
| Key | Required | Description |
|---|---|---|
:schemas | yes | Vector of Datomic attribute maps to transact on startup |
:components :config :datomic-uri | no | Datomic URI string. Defaults to a fresh datomic:mem://<random-uuid> if omitted |
Each entry in :schemas is a standard Datomic attribute map:
[{:db/ident :example/id
:db/valueType :db.type/uuid
:db/cardinality :db.cardinality/one
:db/unique :db.unique/identity
:db/doc "Example entity ID"}
{:db/ident :example/description
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one
:db/doc "Human-readable description"}]
Use the standard Integrant lifecycle:
(require '[integrant.core :as ig]
'[datomic.component])
(def config
{:datomic.component/datomic
{:schemas [{:db/ident :example/id
:db/valueType :db.type/uuid
:db/cardinality :db.cardinality/one
:db/unique :db.unique/identity}]
:components {:config {:datomic-uri "datomic:mem://myapp"}}}})
;; Start
(def system (ig/init config))
;; Stop
(ig/halt! system)
On ig/init, the component:
LocalConnection objectOn ig/halt!, it calls d/release on the connection.
After ig/init, retrieve the connection from the system map using the component key:
(require '[datomic.api :as d])
(def connection (:datomic.component/datomic system))
;; Transact data
@(d/transact connection [{:example/id (random-uuid)
:example/description "Hello, Datomic!"}])
;; Query
(d/q '[:find ?desc
:where [_ :example/description ?desc]]
(d/db connection))
transact-and-lookup-entity!A convenience helper that transacts a single entity and immediately pulls it back from db-after.
(transact-and-lookup-entity! identity-key entity connection)
| Argument | Type | Description |
|---|---|---|
identity-key | Keyword | The attribute used to look the entity back up (must be :db/unique) |
entity | Map | The entity map to transact |
connection | LocalConnection | Active Datomic connection |
{:entity {...} ; the full entity map (excluding :db/id)
:db-after <Db>} ; the database value after the transaction
(require '[datomic.component :as component.datomic])
(def result
(component.datomic/transact-and-lookup-entity!
:example/id
{:example/id (random-uuid)
:example/description "Transacted and retrieved"}
connection))
(:entity result)
;; => {:example/id #uuid "...", :example/description "Transacted and retrieved"}
Throws ex-info if the entity cannot be found after transacting.
mocked-datomicFor unit tests you can create an isolated in-memory connection without Integrant:
(require '[datomic.component :as component.datomic]
'[datomic.api :as d])
(def schema
[{:db/ident :example/id
:db/valueType :db.type/uuid
:db/cardinality :db.cardinality/one
:db/unique :db.unique/identity}
{:db/ident :example/description
:db/valueType :db.type/string
:db/cardinality :db.cardinality/one}])
(deftest my-db-test
(let [connection (component.datomic/mocked-datomic schema)]
(is (some? @(d/transact connection [{:example/id (random-uuid)
:example/description "test"}])))))
mocked-datomic creates a fresh datomic:mem://<uuid> database, connects to it, transacts the schemas, and returns the connection. Each call produces a fully isolated database.
Can you improve this documentation?Edit on GitHub
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |