Created
May 23, 2018 02:35
-
-
Save souenzzo/c4719d45e804767c97f6f5be1bcdd1c5 to your computer and use it in GitHub Desktop.
How to access datomic from javascript in graalvm
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
;; Hello world with clojure + js | |
;; (import '(org.graalvm.polyglot Context)) | |
(let [polyglot (-> (doto (Context/newBuilder (into-array ["js"])) | |
(.allowAllAccess true)) | |
(.build))] | |
(.eval polyglot "js" " | |
console.log('hello'); | |
")) | |
;; Access java from js | |
;; (import '(org.graalvm.polyglot Context)) | |
(let [polyglot (-> (Context/newBuilder (into-array ["js"])) | |
(.allowAllAccess true) | |
(.build))] | |
(.eval polyglot "js" " | |
var BigInteger = Java.type('java.math.BigInteger'); | |
console.log(BigInteger.valueOf(2).pow(100).toString(16)); | |
")) | |
;; Access datomic frm js | |
;; (import '(org.graalvm.polyglot Context)) | |
;; (require '[datomic.api :as d]) | |
;; TIP: d/q accept strings | |
(prn (d/q "[:find ?e . :where [(ground 42) ?e]]")) | |
(let [polyglot (-> (doto (Context/newBuilder (into-array ["js"])) | |
(.allowAllAccess true)) | |
(.build))] | |
(.eval polyglot "js" " | |
var q = new(Java.type('datomic.api.q')); | |
console.log(q.invoke('[:find ?e . :where [(ground 42) ?e]]')); | |
")) | |
;; Adapt functions | |
;; (import '(org.graalvm.polyglot Context)) | |
;; (require '[datomic.api :as d]) | |
;; (import '(org.graalvm.polyglot Context)) | |
;; (require '[datomic.api :as d]) | |
(let [polyglot (-> (doto (Context/newBuilder (into-array ["js"])) | |
(.allowAllAccess true)) | |
(.build))] | |
(.eval polyglot "js" " | |
const resolve = (sym) => { | |
const f = new(Java.type(sym)) | |
return (...args) => f.invoke(... args) | |
} | |
var q = resolve('datomic.api.q') | |
console.log(q('[:find ?r . :in ?e :where [(inc ?e) ?r]]', 41)); | |
")) | |
;; Query, transact and use clojure types | |
;; (import '(org.graalvm.polyglot Context)) | |
;; (require '[datomic.api :as d]) | |
(let [polyglot (-> (doto (Context/newBuilder (into-array ["js"])) | |
(.allowAllAccess true)) | |
(.build))] | |
(d/delete-database "datomic:mem://my-awesome-name") | |
(d/create-database "datomic:mem://my-awesome-name") | |
(.eval polyglot "js" " | |
const resolve = (sym) => { | |
const f = new(Java.type(sym)) | |
return (...args) => f.invoke(... args) | |
} | |
var q = resolve('datomic.api.q') | |
var connect = resolve('datomic.api.connect') | |
var transact = resolve('datomic.api.transact') | |
var db = resolve('datomic.api.db') | |
var deref = resolve('clojure.core.deref') | |
var vector = resolve('clojure.core.vector') | |
var kw = resolve('clojure.core.keyword') | |
var conn = connect('datomic:mem://my-awesome-name') | |
var result = q('[:find [?ident ...] :where [?e :db/ident ?ident]]', db(conn)) | |
for (k in result) { | |
console.log([typeof(k), k ,result[k], typeof(result[k])]); | |
} | |
// datomic peer API accept strings and raw maps on transact. | |
var tx = deref(transact(conn, vector({':db/ident': ':foo/bar'}))) | |
// this line is something like (:db-after tx) in clojure | |
var dbAfter = kw('db-after').invoke(tx) | |
console.log(q('[:find ?e . :in $ ?ident :where [?e :db/ident ?ident]]', dbAfter, kw('foo/bar'))) | |
")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment