Skip to content

Instantly share code, notes, and snippets.

@Ferossgp
Created November 2, 2020 17:56
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 Ferossgp/7509e14d22e8a3a555c82a7039dde760 to your computer and use it in GitHub Desktop.
Save Ferossgp/7509e14d22e8a3a555c82a7039dde760 to your computer and use it in GitHub Desktop.
(ns datascript.controller
(:require [datascript.protocols :as pt]
[keechma.next.controller :as ctrl]
[datascript.core :as d]))
(defn missing-pull-result [query]
(when (some #{:db/id} query)
{:db/id nil}))
(deftype DataScriptApi [ctrl conn*]
pt/IDataScriptApi
(transact! [_ attributes]
;; TODO: Inspect the usage of db-with instead of d/transact?
(ctrl/transact ctrl #(d/transact! conn* attributes))
nil)
(q [_ query args]
(apply (partial d/q query @conn*) args))
(entity [_ eid]
(d/entity @conn* eid))
(pull [_ query id]
(cond
(integer? id)
(d/pull @conn* query id)
(vector? id)
(if-let [eid (d/entid conn* id)]
(d/pull @conn* query eid)
(missing-pull-result query))
(nil? id)
(missing-pull-result query)))
(pull-many [_])
(filter [_ pred]
(d/filter @conn* pred)))
(defmethod ctrl/api :keechma/datascript [{:keys [state*] :as ctrl}]
(->DataScriptApi ctrl state*))
(defmethod ctrl/start :keechma/datascript [ctrl _ _ _]
;; TODO: Maybe integrate with datascript listeners to avoid extra query
(d/empty-db (:keechma.datascript/schema ctrl)))
(ns controllers.datascript
(:refer-clojure :exclude [filter])
(:require [datascript.protocols :as pt]
[keechma.next.protocols :as keechma-pt]
[datascript.core :as d]
[datascript.controller]))
(derive :keechma/datascript :keechma/controller)
(def transact! (keechma-pt/make-api-proxy pt/transact!))
(def q (keechma-pt/make-api-proxy pt/q))
(def entity (keechma-pt/make-api-proxy pt/entity))
(def pull (keechma-pt/make-api-proxy pt/pull))
(def pull-many (keechma-pt/make-api-proxy pt/pull-many))
(def filter (keechma-pt/make-api-proxy pt/filter))
(def touch d/touch)
(ns datascript.protocols
(:refer-clojure :exclude [filter]))
(defprotocol IDataScriptApi
(transact! [_ attributes])
(q [_ query args])
(entity [_ eid])
(pull [_ query id])
(pull-many [_])
(filter [_ pred]))
@Ferossgp
Copy link
Author

Ferossgp commented Nov 2, 2020

 {:keechma/controllers
   ;; PERF: Subscribe only to sub atoms to avoid all recomputation on some mutations
   {:datascript    {:keechma.controller/params true
                    :keechma.controller/type   :keechma/datascript
                    :keechma.datascript/schema datascript-schema}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment