Skip to content

Commit

Permalink
Added reset-conn, conn-from-datoms and conn-from-db (closes ton…
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Jan 17, 2016
1 parent 6ed256f commit a8da9eb
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Can specify transaction number in `:db/add`
- Can put datoms into transaction directly (issue #121: supports both addition and retration datoms, keeps tx number)
- Added all `datascript.core` symbols to externs so they can be called directly from JS (e.g. in combination with mori, issue #139)
- Added `reset-conn`, `conn-from-datoms` and `conn-from-db` (issue #45)

# 0.14.0

Expand Down
30 changes: 25 additions & 5 deletions src/datascript/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,16 @@
:cljs (satisfies? cljs.core/IDeref conn))
(db/db? @conn)))

(defn ^:export conn-from-db [db]
(atom db :meta { :listeners (atom {}) }))

(defn ^:export conn-from-datoms
([datoms] (conn-from-db (init-db datoms)))
([datoms schema] (conn-from-db (init-db datoms schema))))

(defn ^:export create-conn
([] (create-conn db/default-schema))
([schema]
(atom (empty-db schema)
:meta { :listeners (atom {}) })))
([] (conn-from-db (empty-db)))
([schema] (conn-from-db (empty-db schema))))

(defn -transact! [conn tx-data tx-meta]
{:pre [(conn? conn)]}
Expand All @@ -112,7 +117,22 @@
(doseq [[_ callback] @(:listeners (meta conn))]
(callback report))
report)))


(defn ^:export reset-conn!
([conn db] (reset-conn! conn db nil))
([conn db tx-meta]
(let [report (db/map->TxReport
{ :db-before @conn
:db-after db
:tx-data (concat
(map #(assoc % :added false) (datoms @conn :eavt))
(datoms db :eavt))
:tx-meta tx-meta})]
(reset! conn db)
(doseq [[_ callback] @(:listeners (meta conn))]
(callback report))
db)))

(defn ^:export listen!
([conn callback] (listen! conn (rand) callback))
([conn key callback]
Expand Down
6 changes: 3 additions & 3 deletions src/datascript/db.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
(-nth [this i not-found] (nth-datom this i not-found))

IAssociative
(-assoc [d k v] (assoc-datom k))
(-assoc [d k v] (assoc-datom d k v))

IPrintWithWriter
(-pr-writer [d writer opts]
Expand All @@ -155,7 +155,7 @@
(equiv [d o] (and (instance? Datom o) (equiv-datom d o)))
(empty [d] (throw (UnsupportedOperationException. "empty is not supported on Datom")))
(count [d] 5)
(cons [d [k v]] (assoc-datom k v))
(cons [d [k v]] (assoc-datom d k v))

clojure.lang.Indexed
(nth [this i] (nth-datom this i))
Expand All @@ -168,7 +168,7 @@
clojure.lang.Associative
(entryAt [d k] (some->> (val-at-datom d k nil) (clojure.lang.MapEntry k)))
(containsKey [e k] (#{:e :a :v :tx :added} k))
(assoc [d k v] (assoc-datom k v))
(assoc [d k v] (assoc-datom d k v))
]))

(defn ^Datom datom
Expand Down
19 changes: 19 additions & 0 deletions src/datascript/js.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@
(defn ^:export create_conn [& [schema]]
(d/create-conn (schema->clj schema)))

(def ^:export conn_from_db d/conn-from-db)

(defn ^:export conn_from_datoms
([datoms] (conn_from_db (init_db datoms)))
([datoms schema] (conn_from_db (init_db datoms schema))))

(defn ^:export db [conn]
@conn)

Expand All @@ -108,6 +114,19 @@
(callback report))
report))

(defn ^:export reset_conn [conn db & [tx-meta]]
(let [report #js { :db_before @conn
:db_after db
:tx_data (into-array
(concat
(map #(assoc % :added false) (d/datoms @conn :eavt))
(d/datoms db :eavt)))
:tx_meta tx-meta }]
(reset! conn db)
(doseq [[_ callback] @(:listeners (meta conn))]
(callback report))
db))

(def ^:export listen d/listen!)

(def ^:export unlisten d/unlisten!)
Expand Down
1 change: 1 addition & 0 deletions test/datascript/test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

datascript.test.btset
datascript.test.components
datascript.test.conn
datascript.test.db
datascript.test.entity
datascript.test.explode
Expand Down
62 changes: 62 additions & 0 deletions test/datascript/test/conn.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
(ns datascript.test.conn
(:require
#?(:cljs [cljs.test :as t :refer-macros [is are deftest testing]]
:clj [clojure.test :as t :refer [is are deftest testing]])
[datascript.core :as d]
[datascript.db :as db]
[datascript.test.core :as tdc]))

(def schema { :aka { :db/cardinality :db.cardinality/many }})
(def datoms #{(d/datom 1 :age 17)
(d/datom 1 :name "Ivan")})

(deftest test-ways-to-create-conn
(let [conn (d/create-conn)]
(is (= #{} (set (d/datoms @conn :eavt))))
(is (= nil (:schema @conn))))

(let [conn (d/create-conn schema)]
(is (= #{} (set (d/datoms @conn :eavt))))
(is (= schema (:schema @conn))))

(let [conn (d/conn-from-datoms datoms)]
(is (= datoms (set (d/datoms @conn :eavt))))
(is (= nil (:schema @conn))))

(let [conn (d/conn-from-datoms datoms schema)]
(is (= datoms (set (d/datoms @conn :eavt))))
(is (= schema (:schema @conn))))

(let [conn (d/conn-from-db (d/init-db datoms))]
(is (= datoms (set (d/datoms @conn :eavt))))
(is (= nil (:schema @conn))))

(let [conn (d/conn-from-db (d/init-db datoms schema))]
(is (= datoms (set (d/datoms @conn :eavt))))
(is (= schema (:schema @conn)))))

(deftest test-reset-conn!
(let [conn (d/conn-from-datoms datoms schema)
report (atom nil)
_ (d/listen! conn #(reset! report %))
datoms' #{(d/datom 1 :age 20)
(d/datom 1 :sex :male)}
schema' { :email { :db/unique :db.unique/identity }}
db' (d/init-db datoms' schema')]
(d/reset-conn! conn db' :meta)
(is (= datoms' (set (d/datoms @conn :eavt))))
(is (= schema' (:schema @conn)))

(let [{:keys [db-before db-after tx-data tx-meta]} @report]
(is (= datoms (set (d/datoms db-before :eavt))))
(is (= schema (:schema db-before)))
(is (= datoms' (set (d/datoms db-after :eavt))))
(is (= schema' (:schema db-after)))
(is (= :meta tx-meta))
(is (= [[1 :age 17 false]
[1 :name "Ivan" false]
[1 :age 20 true]
[1 :sex :male true]]
(map (juxt :e :a :v :added) tx-data))))))


16 changes: 16 additions & 0 deletions test/js/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,21 @@ function test_tx_report() {
assert_eq(meta[0], {"some-meta": 1});
}

function test_conn() {
var datoms = [[1, "age", 17, tx0+1],
[1, "name", "Ivan", tx0+1]];
var conn = d.conn_from_datoms(datoms);
assert_eq_datoms(datoms, d.datoms(d.db(conn), ":eavt"));

conn = d.conn_from_db(d.init_db(datoms));
assert_eq_datoms(datoms, d.datoms(d.db(conn), ":eavt"));

var datoms2 = [[1, "age", 20, tx0+1],
[1, "sex", "male", tx0+1]];
d.reset_conn(conn, d.init_db(datoms2));
assert_eq_datoms(datoms2, d.datoms(d.db(conn), ":eavt"));
}

function test_entity() {
var schema = {"aka": {":db/cardinality": ":db.cardinality/many"}};
var db = d.db_with(d.empty_db(schema),
Expand Down Expand Up @@ -426,6 +441,7 @@ function test_datascript_js() {
test_dbfn_call,
test_schema,
test_tx_report,
test_conn,
test_entity,
test_entity_refs,
test_pull,
Expand Down

0 comments on commit a8da9eb

Please sign in to comment.