Skip to content

Commit

Permalink
Merge pull request #10 from xtdb/change-store-and-share
Browse files Browse the repository at this point in the history
Change store and share
  • Loading branch information
Akeboshiwind committed Mar 6, 2024
2 parents 95bae2e + 9e72dc9 commit df9e8e9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 53 deletions.
9 changes: 4 additions & 5 deletions src/server.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@

(s/def ::txs string?) ; Always EDN
(s/def ::query string?) ; Either XTQL or SQL
(s/def ::type string?)
(s/def ::db-run (s/keys :req-un [::txs ::query ::type]))
(s/def ::db-run (s/keys :req-un [::txs ::query]))


(defn- handle-ex-info [ex req]
Expand Down Expand Up @@ -60,7 +59,7 @@
;; TODO: Filter for only the reader required?
txs (edn/read-string {:readers *data-readers*} txs)
query (edn/read-string {:readers *data-readers*} query)]
#_(log/info :requst-data {:txs txs :query query :type type})
#_(log/info :requst-data {:txs txs :query query})
(try
(with-open [node (xtn/start-node {})]
(xt/submit-tx node txs)
Expand Down Expand Up @@ -120,7 +119,7 @@
:as :string
:request-method :post
:content-type :json
:form-params {:txs txs :query query :type "xtql"}
:form-params {:txs txs :query query}
:url "http://localhost:8000/db-run"
:throw-exceptions? false} {})
:body)
Expand All @@ -134,7 +133,7 @@
:as :string
:request-method :post
:content-type :json
:form-params {:txs txs :query query :type "sql"}
:form-params {:txs txs :query query}
:url "http://localhost:8000/db-run"
:throw-exceptions? false} {})
:body))
Expand Down
73 changes: 25 additions & 48 deletions src/xt_fiddle/client.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,14 @@
{:glogi/root :info}) ;; Set a root logger level, this will be inherited by all loggers
;; 'my.app.thing :trace ;; Some namespaces you might want detailed logging

(defn strip-surrounding [s]
(subs s 1 (dec (count s))))

(defn decode-txs [s q-type]
(case q-type
:sql (->> (re-seq #"\[:sql \"(.*?)\"\]" s)
(map second)
(str/join ";\n"))
:xtql s))

(defn decode-query [s q-type]
(case q-type
:sql (strip-surrounding s)
:xtql s))

(rf/reg-event-fx
:app/init
[(rf/inject-cofx ::query-params/get)]
(fn [{:keys [_db query-params]} _]
(let [q-type (keyword (or (:type query-params) "sql"))]
(merge {:db {:type q-type
:txs (some-> (:txs query-params)
js/atob
(decode-txs q-type))
:query (some-> (:query query-params)
js/atob
(decode-query q-type))}}))))
(fn [{:keys [_db] {:keys [type txs query]} :query-params}
_]
{:db {:type (if type (keyword type) :sql)
:txs (when txs (js/atob txs))
:query (when query (js/atob query))}}))

(rf/reg-event-fx
:share
Expand All @@ -65,29 +46,11 @@
(fn [db [_ txs]]
(assoc db :txs txs)))

(rf/reg-event-fx
:set-xtql-txs
(fn [_ [_ txs]]
{:dispatch [:set-txs txs]}))

(rf/reg-event-fx
:set-sql-txs
(fn [_ [_ txs]]
{:dispatch [:set-txs (->> (str/split txs #";")
(map str/trim)
(remove str/blank?)
(map #(str [:sql %]))
(str/join " "))]}))
(rf/reg-event-db
:set-query
(fn [db [_ query]]
(assoc db :query query)))

(rf/reg-event-db
:set-sql-query
(fn [db [_ query]]
(assoc db :query (pr-str query))))

(rf/reg-sub
:get-type
(fn [db _]
Expand Down Expand Up @@ -121,6 +84,21 @@
(dissoc :show-twirly)
(assoc :failure response))))

(defn encode-txs [txs type]
(str "["
(case type
:sql (->> (str/split txs #";")
(remove str/blank?)
(map #(str [:sql %]))
str/join)
:xtql txs)
"]"))

(defn encode-query [query type]
(case type
:sql (pr-str query)
:xtql query))

(rf/reg-event-fx
:db-run
(fn [{:keys [db]} _]
Expand All @@ -130,9 +108,8 @@
(dissoc :failure :results))
:http-xhrio {:method :post
:uri "/db-run"
:params {:txs (str "[" (:txs db) "]")
:query (:query db)
:type "xtql"}
:params {:txs (encode-txs (:txs db) (:type db))
:query (encode-query (:query db) (:type db))}
:timeout 3000
:format (ajax/json-request-format)
:response-format (ajax/json-response-format {:keywords? true})
Expand Down Expand Up @@ -266,18 +243,18 @@
[editor/clj-editor (or @(rf/subscribe [:txs]) default-dml)
{:change-callback (fn [txs] (rf/dispatch [:set-txs txs]))}]
[editor/sql-editor (or @(rf/subscribe [:txs]) default-sql-insert)
{:change-callback (fn [txs] (rf/dispatch [:set-sql-txs txs]))}])]
{:change-callback (fn [txs] (rf/dispatch [:set-txs txs]))}])]
[:div {:class "flex flex-1 border overflow-scroll"}
(if (= :xtql @(rf/subscribe [:get-type]))
[editor/clj-editor (or @(rf/subscribe [:query]) default-xtql-query)
{:change-callback (fn [query] (rf/dispatch [:set-query query]))}]
[editor/sql-editor (or @(rf/subscribe [:query]) default-sql-query)
{:change-callback (fn [query] (rf/dispatch [:set-sql-query query]))}])]]
{:change-callback (fn [query] (rf/dispatch [:set-query query]))}])]]
[:section {:class "h-1/2 border p-2 overflow-auto"}
"Results:"
(if @(rf/subscribe [:twirly?])
[spinner]
(let [{:keys [results failure] :as res} @(rf/subscribe [:results-or-failure])]
(let [{:keys [results failure]} @(rf/subscribe [:results-or-failure])]
(if failure
[display-error failure]
[display-edn results])))]]]])
Expand Down

0 comments on commit df9e8e9

Please sign in to comment.