Search code examples
databaseclojuredatomic

Howto Pass In Datomic functions (for Clojure API)


Using Clojure's Datomic APi, I have an architecture where I'd like to pass in a transact function, to be executed. However, trying to call the passed in transact function doesn't work. The repl recognizes it as a Symbol. And it evaluates, but no data is committed, and no future is returned, meaning, there's no returned transaction ID.

However, directly calling (datomic.api/transact conn [adatom]), works as expected. How can I make the abouve scenario work?

(defn some-fn[conn mapped-fn adatom] 

  (datomic.api/transact conn [adatom])  ;; works 
  #_(mapped-fn conn [adatom]) ;; does not work - evaluates, but no data committed, no future returned, meaning, no returned transaction ID
)

Thanks


Solution

  • It's not very clear what you are trying to do. For example:

    • why not call d/transact inside some-fn?
    • is mapped-fn a function that will be applied to many facts (and in this case it is d/transact)?
    • what is an overall intent?

    To just blindly follow your example, it does work and returns a "future":

    user=> (use '[datomic.api :only (db) :as d])
    nil
    user=> (d/create-database "datomic:mem://unsure-db")
    false
    user=> (def c (d/connect "datomic:mem://unsure-db"))
    #'user/c
    user=> (defn f [conn mapped-fn fact] (mapped-fn conn fact))
    #'user/f
    user=> (f c d/transact [])
    #<promise$settable_future$reify__4526@2da07336: {:db-before datomic.db.Db@8835fddc, :db-after datomic.db.Db@6e2a2e78, :tx-data [#Datum{:e 13194139534313 :a 50 :v #inst "2013-09-03T15:23:34.977-00:00" :tx 13194139534313 :added true}], :tempids {}}>
    

    Make sure you have a valid connection (e.g. you are connected [to the right database]), and the database is there.