-
-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
12 changed files
with
255 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,28 @@ | ||
/target | ||
/bench/target | ||
/classes | ||
/checkouts | ||
pom.xml | ||
pom.xml.asc | ||
*.jar | ||
*.class | ||
*.iml | ||
*.jar | ||
.cljs_node_repl | ||
.cpcache | ||
.DS_Store | ||
.idea | ||
.vscode | ||
/.lein-* | ||
/.nrepl-port | ||
/.repl* | ||
web/out | ||
web/*.js | ||
release-js/datascript*.js | ||
release-js/npm-* | ||
web/target-cljs | ||
.idea | ||
*.iml | ||
.vscode | ||
.cpcache | ||
/bench/target | ||
/checkouts | ||
/classes | ||
/out | ||
/target | ||
dev/playground.clj | ||
.DS_Store | ||
TODO.txt | ||
.cljs_node_repl | ||
node_modules | ||
package-lock.json | ||
pom.xml | ||
pom.xml.asc | ||
reading | ||
release-js/datascript*.js | ||
release-js/npm-* | ||
TODO.txt | ||
web/*.js | ||
web/out | ||
web/target-cljs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Return maps | ||
|
||
``` | ||
return-map = (return-keys | return-syms | return-strs) | ||
return-keys = ':keys' symbol+ | ||
return-syms = ':syms' symbol+ | ||
return-strs = ':strs' symbol+ | ||
``` | ||
|
||
If you need a query to return a set of maps instead set of tuples, specify one of `:keys` / `:syms` / `:strs`, followed by a list of symbols: | ||
|
||
``` | ||
[:find ?name ?age ?email, ...] | ||
=> #{["Ivan" 10 "ivan@"] | ||
["Oleg" 20 "oleg@"] | ||
["Sergey" 30 "sergey@"]} | ||
[:find ?name ?age ?email | ||
:keys name A e-mail | ||
...] | ||
=> #{{:name "Ivan", :A 10, :e-mail "ivan@"} | ||
{:name "Oleg", :A 20, :e-mail "oleg@"} | ||
{:name "Sergey", :A 30, :e-mail "sergey@"}} | ||
``` | ||
|
||
`:strs` forces map to have string keys, `:syms` for symbols. | ||
|
||
Return maps are only compatible with normal find and tuple-returning find: | ||
|
||
``` | ||
[:find [?name ?age ?email], ...] | ||
=> {:name "Ivan", :A 10, :e-mail "ivan@"} | ||
``` | ||
|
||
The amount of keys must match the amount of find elements. | ||
|
||
Datomic docs: https://docs.datomic.com/on-prem/query.html#return-maps |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/bin/zsh -euo pipefail | ||
|
||
cd "`dirname $0`/.." | ||
|
||
npm i ws |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/bin/zsh -euo pipefail | ||
|
||
cd "`dirname $0`/.." | ||
|
||
clojure -A:test -m kaocha.runner clj --watch "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/bin/zsh -euo pipefail | ||
|
||
cd "`dirname $0`/.." | ||
|
||
clojure -A:test -m kaocha.runner cljs --watch "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
(ns datascript.test.parser-return-map | ||
(: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.parser :as dp] | ||
[datascript.db :as db] | ||
[datascript.test.core :as tdc])) | ||
|
||
#?(:cljs | ||
(def Throwable js/Error)) | ||
|
||
(deftest test-parse-return-map | ||
(is (= (:qreturn-map (dp/parse-query '[:find ?a ?b :keys x y :where [?a ?b]])) | ||
(dp/->ReturnMap :keys [:x :y]))) | ||
|
||
(is (= (:qreturn-map (dp/parse-query '[:find ?a :syms x :where [?a]])) | ||
(dp/->ReturnMap :syms ['x]))) | ||
|
||
(is (= (:qreturn-map (dp/parse-query '[:find ?a ?b ?c :strs x y z :where [?a ?b ?c]])) | ||
(dp/->ReturnMap :strs ["x" "y" "z"]))) | ||
|
||
|
||
(testing "with find specs" | ||
(is (= (:qreturn-map (dp/parse-query '[:find [?a ?b] :keys x y :where [?a ?b]])) | ||
(dp/->ReturnMap :keys [:x :y]))) | ||
|
||
(is (thrown-msg? ":keys does not work with collection :find" | ||
(dp/parse-query '[:find [?a ...] :keys x :where [?a]]))) | ||
|
||
(is (thrown-msg? ":keys does not work with single-scalar :find" | ||
(dp/parse-query '[:find ?a . :keys x y :where [?a]])))) | ||
|
||
|
||
(testing "errors" | ||
(is (thrown-msg? "Only one of :keys/:syms/:strs must be present" | ||
(dp/parse-query '[:find ?a ?b :keys x y :strs zt :where [?a ?b]]))) | ||
|
||
(is (thrown-msg? "Count of :keys must match count of :find" | ||
(dp/parse-query '[:find ?a ?b :keys x y z :where [?a ?b]]))) | ||
|
||
(is (thrown-msg? "Count of :syms must match count of :find" | ||
(dp/parse-query '[:find ?a ?b :syms x :where [?a ?b]]))) | ||
|
||
(is (thrown-msg? "Count of :strs must match count of :find" | ||
(dp/parse-query '[:find ?a ?b :strs x :where [?a ?b]]))) | ||
|
||
(is (thrown-msg? "Count of :keys must match count of :find" | ||
(dp/parse-query '[:find [?a ?b] :keys x :where [?a ?b]])))) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
(ns datascript.test.query-return-map | ||
(: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 test-db | ||
(d/db-with (d/empty-db) | ||
[[:db/add 1 :name "Petr"] | ||
[:db/add 1 :age 44] | ||
[:db/add 2 :name "Ivan"] | ||
[:db/add 2 :age 25] | ||
[:db/add 3 :name "Sergey"] | ||
[:db/add 3 :age 11]])) | ||
|
||
(deftest test-find-specs | ||
(is (= (d/q '[:find ?name ?age | ||
:keys n a | ||
:where [?e :name ?name] | ||
[?e :age ?age]] | ||
test-db) | ||
#{{:n "Petr" :a 44} {:n "Ivan" :a 25} {:n "Sergey" :a 11}})) | ||
(is (= (d/q '[:find ?name ?age | ||
:syms n a | ||
:where [?e :name ?name] | ||
[?e :age ?age]] | ||
test-db) | ||
#{{'n "Petr" 'a 44} {'n "Ivan" 'a 25} {'n "Sergey" 'a 11}})) | ||
(is (= (d/q '[:find ?name ?age | ||
:strs n a | ||
:where [?e :name ?name] | ||
[?e :age ?age]] | ||
test-db) | ||
#{{"n" "Petr" "a" 44} {"n" "Ivan" "a" 25} {"n" "Sergey" "a" 11}})) | ||
|
||
(is (= (d/q '[:find [?name ?age] | ||
:keys n a | ||
:where [?e :name ?name] | ||
[(= ?name "Ivan")] | ||
[?e :age ?age]] | ||
test-db) | ||
{:n "Ivan" :a 25}))) | ||
|
||
|