Skip to content

An implementation of the Datomic Client protocols for Datomic peer in-memory database

License

Notifications You must be signed in to change notification settings

kyptin/datomic-client-memdb

 
 

Repository files navigation

(Forked from this repo to push an artifact to Clojars. Note: before publishing, ensure version in pom.xml matches version in Makefile.)

datomic-client-memdb

CircleCI

An implementation of the Datomic Client protocols for Datomic peer in-memory database.

Installation

compute/datomic-client-memdb {:git/url "https://github.com/ComputeSoftware/datomic-client-memdb.git"
                              :sha     "<most recent SHA>"}

Usage

Create a Client like you normally would with the Client API:

(require '[compute.datomic-client-memdb.core :as memdb])
(def c (memdb/client {}))

memdb/client returns compute.datomic-client-memdb.core/Client type which implements datomic.client.api/Client. Calling memdb/client multiple times with the same arg-map will result in the same instance of the client (i.e. the function is memoized).

This is technically the only function that this library exposes. All other operations are done using the Datomic Client API. See here for a list of functions that you can call.

Suggested usage pattern

Our company has a database util library, and I imagine most companies have something similar, that contains a number of functions associated with database operations. Any time you use Datomic, that util library is probably on the classpath. I suggest adding a client function that returns a Datomic Client dependent on some values in the Datomic Client arg-map (the args passed to the client function). For example,

(defn client
  [datomic-config]
  (if (datomic-local? datomic-config)
    (do
      (require 'compute.datomic-client-memdb.core)
      (if-let [v (resolve 'compute.datomic-client-memdb.core/client)]
        (@v datomic-config)
        (throw (ex-info "compute.datomic-client-memdb.core is not on the classpath." {}))))
    (datomic.client.api/client datomic-config)))

The datomic-local? function returns true if datomic-config (the client arg-map) has some particular value set. Perhaps :local? true or :endpoint "localhost".

Cleanup

If you would like to cleanup the in-memory DBs, you can use the close function:

(memdb/close c)

This will delete all the DBs associated with the client and purge them from the client's DB store. Our Client also implements the Closeable interface so you can use the Client with with-open, as seen here.

Because this library is only intended to be used during the development and testing stages, you can probably just ignore the close function and only address cleanup if it becomes a problem.

Implementation

This library is implemented by creating custom types (via deftype) that extend the Datomic Client protocols located in the datomic.client.api namespace. I tried implementing this using extend-type on the Datomic peer classes (i.e. datomic.db.Db), but you cannot add an implementation of clojure.lang.ILookup via deftype because ILookup is an interface, not a protocol. We need to provide an implementation of ILookup because that is how the Datomic Client API exposes t and next-t.

License

Copyright © 2017 Compute Software

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

About

An implementation of the Datomic Client protocols for Datomic peer in-memory database

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Clojure 92.5%
  • Makefile 7.5%