Last active
August 29, 2015 13:57
-
-
Save shaunr0b/9768695 to your computer and use it in GitHub Desktop.
Snippet that registers a Datomic attribute with function valueType; then create an enitity with a function. Next, retrieve the function via a query, bind it to a var, and invoke it using params.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require '[datomic.api :as d]) | |
(def uri "datomic:mem://hello") | |
(d/create-database uri) | |
;; Define a transaction to create an attribute with type function (:some/fn) | |
(def attribute-tx [ {:db/id (d/tempid :db.part/db) | |
:db/ident :regular/attribute | |
:db/cardinality :db.cardinality/one | |
:db/valueType :db.type/fn | |
:db.install/_attribute :db.part/db} ]) | |
;; Define a transaction to create a function | |
(def fn-tx [{:db/id (d/tempid :db.part/user) | |
:regular/attribute #db/fn {:lang :clojure | |
:params [name] | |
:code (str "Hello, " name)}}]) | |
;; Transact | |
(d/transact (d/connect uri) attribute-tx) | |
(d/transact (d/connect uri) fn-tx) | |
;; Query and bind result to var | |
(def hello (ffirst (d/q '[:find ?v | |
:where | |
[?e :regular/attribute ?v]] | |
(d/db (d/connect uri))))) | |
;; Invoke function with params | |
(hello "Jordan!") | |
;; => "Hello, Jordan!" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment