Skip to content

Commit 6fe24a0

Browse files
author
Andrey Melnikov
committed
feat(core): add transit and up version of datascript
1 parent 852ccaf commit 6fe24a0

5 files changed

Lines changed: 54 additions & 5 deletions

File tree

project.clj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(defproject datascript-mori "0.15.4"
1+
(defproject datascript-mori "0.16.2"
22
:description "Wrapper for datascript interplay mori"
33
:url "https://github.com/typeetfunc/datascript-mori"
44
:license {:name "Eclipse Public License"
@@ -8,7 +8,8 @@
88

99
:dependencies [[org.clojure/clojure "1.7.0"]
1010
[org.clojure/clojurescript "1.7.170"]
11-
[datascript "0.15.4"]]
11+
[datascript "0.16.1"]
12+
[datascript-transit "0.2.2"]]
1213

1314
:plugins [[lein-cljsbuild "1.1.2" :exclusions [[org.clojure/clojure]]]
1415
[lein-git-deps "0.0.2-SNAPSHOT"]]
@@ -24,6 +25,7 @@
2425
:compiler {
2526
:output-to "release-js/datascript-mori.bare.js"
2627
:main datascript-mori.core
28+
:externs ["release-js/datascript-core.extern.js"]
2729
:optimizations :advanced
2830
:pretty-print false
2931
}

release-js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "datascript-mori",
3-
"version": "0.16.1",
3+
"version": "0.17.0",
44
"description": "Wrapper for datascript interplay mori",
55
"homepage": "https://github.com/typeetfunc/datascript-mori",
66
"author": "Andrei Melnikov (https://github.com/typeetfunc)",

release-js/test/combineJsAndCljsApi.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,16 @@ describe('Use regular JS API for create connection and add data to DB', () => {
4949
assert(isSet(result), 'result is mori set')
5050
assert(equals(result, set([vector("Igor")])), 'result equals #{["Igor"]}')
5151
})
52+
53+
it('cas is just work', () => {
54+
var conn = djs.create_conn();
55+
djs.transact(conn, [
56+
[':db/add', 1, 'weight', 200]
57+
]);
58+
djs.transact(conn, [
59+
[':db.fn/cas', 1, 'weight', 200, 300]
60+
]);
61+
var e = djs.entity(djs.db(conn), 1);
62+
assert(e.get('weight') === 300, 'CAS changed weight correctly');
63+
});
5264
})

release-js/test/onlyCljsApiUsage.spec.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import {assert} from 'chai'
33
var d = datascript.core // use datascript_mori.datascript.core API
44
var djs = datascript.js
55
var {hashMap, vector, parse, toJs, equals, isMap, hasKey, isSet, set, getIn, get} = mori
6-
var {DB_VALUE_TYPE, DB_TYPE_REF, DB_ADD, DB_ID, TEMPIDS} = helpers
6+
var {DB_VALUE_TYPE, DB_TYPE_REF, DB_ADD, DB_ID, TEMPIDS, DB_FN_CAS, stringify_db,
7+
parse_db} = helpers
78

89
describe('add data to DB and query them', () => {
910
// scheme must be a mori structure or use helpers.schema_to_clj({friend: {":db/valueType": ":db.type/ref"}})
@@ -112,3 +113,34 @@ describe('create conn and use transact API', () => {
112113
})
113114
d.unlisten_BANG_(conn, "main") //or djs.unlisten(conn, "main", callback) is fully equal definition
114115
});
116+
117+
118+
describe('CAS', () => {
119+
it('cas is just work', () => {
120+
var conn = d.create_conn();
121+
d.transact_BANG_(conn, vector(
122+
vector(DB_ADD, 1, 'weight', 200)
123+
));
124+
d.transact_BANG_(conn, vector(
125+
vector(DB_FN_CAS, 1, 'weight', 200, 300)
126+
));
127+
var e = d.entity(d.db(conn), 1);
128+
assert(get(e, 'weight') === 300, 'CAS changed weight correctly');
129+
});
130+
});
131+
132+
describe('serialization/deserialization', () => {
133+
it('serialize', () => {
134+
var db = d.empty_db();
135+
var dbWithItem = d.db_with(db, vector(
136+
vector(DB_ADD, 1, 'weight', 200)
137+
));
138+
assert(stringify_db(dbWithItem) === '["~#datascript/DB",["^ ","~:schema",null,"~:datoms",["~#list",[["~#datascript/Datom",[1,"weight",200,536870913]]]]]]', 'stringify db works correctly');
139+
});
140+
141+
it('deserialize', () => {
142+
var db = parse_db('["~#datascript/DB",["^ ","~:schema",null,"~:datoms",["~#list",[["~#datascript/Datom",[1,"weight",200,536870913]]]]]]', 'stringify db works correctly');
143+
var e = d.entity(db, 1);
144+
assert(get(e, 'weight') === 200, 'deserialize works correctly');
145+
})
146+
});

src/datascript_mori/core.cljs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
[mori :as m]
88
[mori.extra :as me]
99
[cljs.reader :as reader]
10+
[datascript.transit :as dt]
1011
[datascript.lru :as dlru]))
1112

1213
(def ^:export schema_to_clj djs/schema->clj)
@@ -31,9 +32,11 @@
3132
(let [qp (reader/read-string q)]
3233
(vswap! query_cache assoc q qp)
3334
qp)))
34-
35+
(def ^:export stringify_db dt/write-transit-str)
36+
(def ^:export parse_db dt/read-transit-str)
3537
(def ^:export DB_ID :db/id)
3638
(def ^:export DB_FN_CALL :db.fn/call)
39+
(def ^:export DB_FN_CAS :db.fn/cas)
3740
(def ^:export DB_BEFORE :db-before)
3841
(def ^:export DB_AFTER :db-after)
3942
(def ^:export TX_DATA :tx-data)

0 commit comments

Comments
 (0)