juji-io / datalevin

A simple, fast and versatile Datalog database
https://github.com/juji-io/datalevin
Eclipse Public License 1.0
1.07k stars 60 forks source link

Expose Clojure vars to inter-fn #215

Open huahaiy opened 1 year ago

huahaiy commented 1 year ago

From Clojurian slack:

(ns slix.node)

(defn testing-fn []
  "test-value")

(di/definterfn test-tx1 [db tempid]
  [{:db/id tempid :node/value (slix.node/testing-fn)}])

(di/definterfn test-tx2 [db tempid]
  [{:db/id tempid :node/value "test-value"}])

(d/transact! test-conn [{:db/ident :add-node1
              :db/fn test-tx1}])

(d/transact! test-conn [{:db/ident :add-node2
              :db/fn test-tx2}])

:add-node1 fails, but :add-node2 succeeds.

This is the case, because inter-fn are interpreted by sci, which does not have direct access to Clojure vars. We should make it possible to do so.

Notice that, we do not need an inter-fn for this use case in embedded mode, we can use :db.fn/call instead, as it does not persist the function and just call it.