Skip to content

Instantly share code, notes, and snippets.

@tatut
Created August 16, 2021 10:25
Show Gist options
  • Save tatut/4767cddca27f71737637c2e66c30e3f3 to your computer and use it in GitHub Desktop.
Save tatut/4767cddca27f71737637c2e66c30e3f3 to your computer and use it in GitHub Desktop.
% clj -Sdeps "{:deps {juxt/crux-core {:mvn/version \"21.06-1.17.1-beta\"}}}"
Clojure 1.10.1
;; Require API and create noe
user=> (require '[crux.api :as crux])
nil
user=> (def crux (crux/start-node {})) ; start in-memory node
#'user/crux
;; Insert and query some data
user=> (crux/submit-tx crux [[:crux.tx/put {:crux.db/id :banana :fruit/name "banana" :fruit/good? true}]
[:crux.tx/put {:crux.db/id :rambutan :fruit/name "rambutan" :fruit/good? false}]])
#:crux.tx{:tx-id 0, :tx-time #inst "2021-08-16T09:48:01.149-00:00"}
user=> (crux/q (crux/db crux)
'[:find ?n
:where [?f :fruit/name ?n] [?f :fruit/good? true]])
#{["banana"]}
user=> (crux/pull (crux/db crux) '[*] :rambutan)
{:crux.db/id :rambutan, :fruit/name "rambutan", :fruit/good? false}
;; Add some more data
user=> (crux/submit-tx crux [[:crux.tx/put {:crux.db/id :apple :fruit/name "apple" :fruit/good? true}]
[:crux.tx/put {:crux.db/id :john-doe :user/name "John Doe"
:user/likes-fruit [:apple :rambutan]}]])
#:crux.tx{:tx-id 1, :tx-time #inst "2021-08-16T09:52:38.906-00:00"}
user=> (crux/q (crux/db crux)
'[:find ?n
:where [?f :fruit/name ?n] [?f :fruit/good? true]])
#{["apple"] ["banana"]} ; good fruits now
user=> (crux/q (crux/db crux #inst "2021-08-16T09:48:01.149-00:00") ; take historical db
'[:find ?n
:where [?f :fruit/name ?n] [?f :fruit/good? true]])
#{["banana"]} ; history is preserved
user=> (crux/q (crux/db crux)
'[:find ?u ?n
:where [?f :fruit/name ?n] [?u :user/likes-fruit ?f]])
#{[:john-doe "apple"] [:john-doe "rambutan"]} ; [user fruit-name] tuples of anyone who likes any fruit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment