Skip to content

Instantly share code, notes, and snippets.

@potetm
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save potetm/47932590fc3efaca8d0e to your computer and use it in GitHub Desktop.
Save potetm/47932590fc3efaca8d0e to your computer and use it in GitHub Desktop.
Playing with om and datascript
(ns om-datascript
(:require-macros [cljs.core.async.macros :refer [go alt!]])
(:require [cljs.core.async :refer [put! <! >! chan timeout]]
[om.core :as om :include-macros true]
[sablono.core :as html :refer-macros [html]]
[figwheel.client :as fw :include-macros true]
[datascript :as d]))
(enable-console-print!)
(fw/defonce conn (d/create-conn))
;; prevent cursor-ification
(extend-type d/DB
om/IToCursor
(-to-cursor
([this _] this)
([this _ _] this)))
(d/transact! conn [{:db/id -1
:first-name "Rand"
:last-name "al Thor"
:status "The Dragon Reborn"}
[:db/add -2 :app/count 0]])
(defn get-conn [owner]
(om/get-shared owner :conn))
(defn get-name-of-the-dragon [db]
(ffirst
(d/q '[:find ?first-name
:in $
:where
[?i :status "The Dragon Reborn"]
[?i :first-name ?first-name]]
db)))
(defn get-count-id [db]
(ffirst
(d/q
'[:find ?i
:where
[?i :app/count]]
db)))
(defn set-count! [owner count-id value]
(d/transact! (get-conn owner)
[[:db/add count-id :app/count value]]))
(defn om-datascript-app [db owner]
(reify
om/IRender
(render [_]
(let [count (d/entity db (get-count-id db))
count-id (:db/id count)
app-count (:app/count count)]
(html
[:div
[:h1 (str "hello, " (get-name-of-the-dragon db))]
[:h2 (str "The count is: " app-count)]
[:button {:on-click #(set-count! owner count-id (inc app-count))}
"Increment The Count"]])))))
(om/root
om-datascript-app conn
{:shared {:conn conn}
:target (.getElementById js/document "content")})
(fw/watch-and-reload)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment