From 360f7622e8cc07cce05497895eaf7959b0c7093d Mon Sep 17 00:00:00 2001 From: Richard Newman Date: Wed, 14 Sep 2016 16:08:13 -0700 Subject: [PATCH] Add handling of simple schemas. Fixes #53. --- src/common/datomish/db.cljc | 4 ++ src/common/datomish/js.cljs | 40 ++++++++++++++-- src/common/datomish/schema.cljc | 1 + src/common/datomish/simple_schema.cljc | 66 ++++++++++++++++++++++++++ src/node/datomish/core.cljs | 7 +-- test/datomish/db_test.cljc | 34 +++++++++++++ 6 files changed, 143 insertions(+), 9 deletions(-) create mode 100644 src/common/datomish/simple_schema.cljc diff --git a/src/common/datomish/db.cljc b/src/common/datomish/db.cljc index 16b74700..5fd28115 100644 --- a/src/common/datomish/db.cljc +++ b/src/common/datomish/db.cljc @@ -713,6 +713,10 @@ Returns a transduced channel of [result err] pairs. Closes the channel when fully consumed." [db find options] + (let [unexpected (seq (clojure.set/difference (set (keys options)) #{:limit :order-by :inputs}))] + (when unexpected + (raise "Unexpected options: " unexpected {:bad-options unexpected}))) + (let [{:keys [limit order-by inputs]} options parsed (query/parse find) context (-> db diff --git a/src/common/datomish/js.cljs b/src/common/datomish/js.cljs index c67f7854..0fd7aa5a 100644 --- a/src/common/datomish/js.cljs +++ b/src/common/datomish/js.cljs @@ -14,6 +14,7 @@ [datomish.db-factory :as db-factory] [datomish.pair-chan] [datomish.sqlite :as sqlite] + [datomish.simple-schema :as simple-schema] [datomish.js-sqlite :as js-sqlite] [datomish.transact :as transact])) @@ -29,6 +30,38 @@ ;; Public API. +(defn ^:export db [conn] + (transact/db conn)) + +(defn ^:export q [db find options] + (let [find (cljs.reader/read-string find) + options (js->clj options)] + (take-pair-as-promise! + (db/clj simple-schema)] + (println "simple-schema: " (pr-str simple-schema)) + (take-pair-as-promise! + (transact/schema simple-schema))))) + +(defn js->tx-data [tx-data] + ;; Objects to maps. + ;; Arrays to arrays. + ;; RHS stringsā€¦ well, some of them will be richer types. + ;; TODO + (println "Converting" (pr-str tx-data) "to" (pr-str (js->clj tx-data :keywordize-keys true))) + (println "Converting" (pr-str tx-data) "to" (pr-str (js->clj tx-data :keywordize-keys true))) + (js->clj tx-data)) + +(defn ^:export transact [conn tx-data] + ;; Expects a JS array as input. + (let [tx-data (js->tx-data tx-data)] + (take-pair-as-promise! + (transact/js {:conn c + :ensureSchema (fn [simple-schema] (ensure-schema c simple-schema)) + :transact (fn [tx-data] (transact c tx-data)) + :q (fn [find options] (q (transact/db c) find options)) :close (fn [] (db/close-db db)) :toString (fn [] (str "#")) :path path})))))) - -(defn ^:export q [query & sources] - (let [query (cljs.reader/read-string query)] - (clj->js query))) diff --git a/src/common/datomish/schema.cljc b/src/common/datomish/schema.cljc index b4524d7a..664a34b6 100644 --- a/src/common/datomish/schema.cljc +++ b/src/common/datomish/schema.cljc @@ -208,3 +208,4 @@ {:pre [(or (nil? schema) (map? schema))]} (map->Schema {:schema (validate-schema schema) :rschema (rschema schema)})) + diff --git a/src/common/datomish/simple_schema.cljc b/src/common/datomish/simple_schema.cljc new file mode 100644 index 00000000..2bba32d7 --- /dev/null +++ b/src/common/datomish/simple_schema.cljc @@ -0,0 +1,66 @@ +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. + +(ns datomish.simple-schema + #?(:cljs + (:require-macros + [datomish.pair-chan :refer [go-pair !]]]) + #?@(:cljs [[datomish.pair-chan] + [cljs.core.async :as a :refer [chan !]]]))) + +(defn- name->ident [name] + (when-not (and (string? name) + (not (empty? name))) + (raise "Invalid name " name {:error :invalid-name :name name})) + (keyword name)) + +(defn simple-schema-attributes->schema-parts [attrs] + (let [{:keys [cardinality type name unique doc fulltext]} attrs + value-type (when type (keyword (str "db.type/" type)))] + + (when-not (and value-type + (contains? ds/value-type-map value-type)) + (raise "Invalid type " type {:error :invalid-type :type type})) + + (let [unique + (case unique + "identity" :db.unique/identity + "value" :db.unique/value + nil nil + (raise "Invalid unique " unique + {:error :invalid-unique :unique unique})) + + cardinality + (case cardinality + "one" :db.cardinality/one + "many" :db.cardinality/many + nil nil + (raise "Invalid cardinality " cardinality + {:error :invalid-cardinality :cardinality cardinality}))] + + (util/assoc-if + {:db/valueType value-type + :db/ident (name->ident name) + :db/id (db/id-literal :db.part/user) + :db.install/_attribute :db.part/db} + :db/doc doc + :db/unique unique + :db/fulltext fulltext + :db/cardinality cardinality)))) + +(defn simple-schema->schema [simple-schema] + (let [{:keys [name attributes]} simple-schema] + (map simple-schema-attributes->schema-parts attributes))) + diff --git a/src/node/datomish/core.cljs b/src/node/datomish/core.cljs index 61e7b573..7b77da5a 100644 --- a/src/node/datomish/core.cljs +++ b/src/node/datomish/core.cljs @@ -5,9 +5,6 @@ (ns datomish.core (:require [cljs.nodejs :as nodejs])) -(nodejs/enable-util-print!) - -(defn -main [& args] - (println "Hello world!")) - +(defn -main [& args]) (set! *main-cli-fn* -main) +(nodejs/enable-util-print!) diff --git a/test/datomish/db_test.cljc b/test/datomish/db_test.cljc index 2d972461..1dea8c66 100644 --- a/test/datomish/db_test.cljc +++ b/test/datomish/db_test.cljc @@ -13,6 +13,7 @@ [datomish.db.debug :refer [schema in)) + expected))))) + + #_ (time (t/run-tests))