generated from FiV0/clj-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes #42 fixes #19 Handle vector row results Move data manipulation to server Remove beta button Add beta banner Add more logging Handle todos Add handler tests Refactor Reorg file structure Stop shadowing clojure.core/list Refactor Re-frame improvements fixes #44 Add cljs run test Tidy up Fix query not updating Fix nested data formats Stay backwards compatible for docs Remove confusing system time abstraction Tidy up Fix mobile footer and unstick non-mobile header Standardise where we update-url on editor updates Add update url interval Make cljs config more config Fix cljs tests
- Loading branch information
1 parent
2fa8501
commit 217e751
Showing
32 changed files
with
1,138 additions
and
585 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,3 +1,4 @@ | ||
*~ | ||
.cpcache | ||
.nrepl-port | ||
target | ||
|
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,9 @@ | ||
(ns xt-play.config) | ||
|
||
(def db | ||
{:dbtype "postgresql" | ||
:dbname "xtdb" | ||
:user "xtdb" | ||
:password "xtdb" | ||
:host "localhost" | ||
:port 5432}) |
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,117 @@ | ||
(ns xt-play.transactions | ||
(:require [clojure.string :as str] | ||
[clojure.data.json :as json] | ||
[clojure.instant :refer [read-instant-date]] | ||
[clojure.tools.logging :as log] | ||
[next.jdbc :as jdbc] | ||
[next.jdbc.result-set :as jdbc-res] | ||
[xt-play.config :as config] | ||
[xt-play.util :as util] | ||
[xtdb.api :as xt] | ||
[xtdb.node :as xtn])) | ||
|
||
(defn- encode-txs [tx-type txs] | ||
(case (keyword tx-type) | ||
:sql (->> (str/split txs #";") | ||
(remove str/blank?) | ||
(map #(do [:sql %])) | ||
(vec)) | ||
:xtql (util/read-edn (str "[" txs "]")) | ||
;;else | ||
txs)) | ||
|
||
(defn- prepare-statements | ||
"Takes a batch of transactions and prepares the jdbc execution args to | ||
be run sequentially" | ||
[tx-batches] | ||
(for [{:keys [txs system-time]} tx-batches] | ||
(remove nil? | ||
[(when system-time | ||
[(format "BEGIN AT SYSTEM_TIME TIMESTAMP '%s'" system-time)]) | ||
[txs] | ||
(when system-time | ||
["COMMIT"])]))) | ||
|
||
(defn format-system-time [s] | ||
(when s (read-instant-date s))) | ||
|
||
(defn- run!-tx [node tx-type tx-batches query] | ||
(let [tx-batches (->> tx-batches | ||
(map #(update % :system-time format-system-time)) | ||
(map #(update % :txs (partial encode-txs tx-type))))] | ||
(doseq [{:keys [system-time txs] :as batch} tx-batches] | ||
(log/info tx-type "running batch: " batch) | ||
(let [tx (xt/submit-tx node txs {:system-time system-time}) | ||
results (xt/q node '(from :xt/txs [{:xt/id $tx-id} xt/error]) | ||
{:args {:tx-id (:tx-id tx)}})] | ||
;; If any transaction fails, throw the error | ||
(log/info tx-type "batch complete:" tx ", results:" results) | ||
(when-let [error (-> results first :xt/error)] | ||
(throw error))))) | ||
(log/info tx-type "running query:" query) | ||
(let [res (xt/q node query (when (string? query) | ||
{:key-fn :snake-case-string}))] | ||
(log/info tx-type "XTDB query response:" res) | ||
res)) | ||
|
||
(defn- PGobject->clj [v] | ||
(if (= org.postgresql.util.PGobject (type v)) | ||
(json/read-str (.getValue v) :key-fn keyword) | ||
v)) | ||
|
||
(defn- parse-result [result] | ||
;; TODO - this shouldn't be needed, a fix is on the way in | ||
;; a later version of xtdb-jdb | ||
;; This will only pick up top level objects | ||
(mapv | ||
(fn [row] | ||
(mapv PGobject->clj row)) | ||
result)) | ||
|
||
(defn- run!-with-jdbc-conn [tx-batches query] | ||
(with-open [conn (jdbc/get-connection config/db)] | ||
(doseq [tx (prepare-statements tx-batches) | ||
statement tx] | ||
(log/info "beta executing statement:" statement) | ||
(jdbc/execute! conn statement)) | ||
(log/info "beta running query:" query) | ||
(let [res (jdbc/execute! conn [query] {:builder-fn jdbc-res/as-arrays})] | ||
(log/info "beta query resoponse" res) | ||
(parse-result res)))) | ||
|
||
(defn run!! | ||
"Given transaction batches, a query and the type of transaction to | ||
use, will run transaction batches and queries sequentially, | ||
returning the last query response in column format." | ||
[{:keys [tx-batches query tx-type]}] | ||
(let [query (if (= "xtql" tx-type) (util/read-edn query) query)] | ||
(try | ||
(with-open [node (xtn/start-node {})] | ||
(if (= "sql-beta" tx-type) | ||
(run!-with-jdbc-conn tx-batches query) | ||
(util/map-results->rows | ||
(run!-tx node tx-type tx-batches query)))) | ||
(catch Exception e | ||
(log/warn :submit-error {:e e}) | ||
(throw e))))) | ||
|
||
(defn docs-run!! | ||
"Given transaction batches and a query from the docs, will return the query | ||
response in map format. Assumes tx type is sql." | ||
[{:keys [tx-batches query]}] | ||
(try | ||
(with-open [node (xtn/start-node {})] | ||
(run!-tx node "sql" tx-batches query)) | ||
(catch Exception e | ||
(log/warn :submit-error {:e e}) | ||
(throw e)))) | ||
|
||
(comment | ||
(with-open [node (xtn/start-node {})] | ||
(doseq [st [#inst "2022" #inst "2021"]] | ||
(let [tx (xt/submit-tx node [] {:system-time st}) | ||
results (xt/q node '(from :xt/txs [{:xt/id $tx-id} xt/error]) | ||
{:basis {:at-tx tx} | ||
:args {:tx-id (:tx-id tx)}})] | ||
(when-let [error (-> results first :xt/error)] | ||
(throw (ex-info "Transaction error" {:error error}))))))) |
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,31 @@ | ||
(ns xt-play.view | ||
(:require [hiccup.page :as h] | ||
[xt-play.util :as util])) | ||
|
||
(def index | ||
(h/html5 | ||
[:head | ||
[:meta {:charset "utf-8"}] | ||
[:meta {:name "viewport" | ||
:content "width=device-width, initial-scale=1"}] | ||
[:meta {:name "description" | ||
:content ""}] | ||
[:link {:rel "stylesheet" | ||
:href "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/default.min.css"}] | ||
[:link {:rel "stylesheet" | ||
:type "text/css" | ||
:href "/public/css/main.css"}] | ||
[:script {:src "https://cdn.tailwindcss.com"}] | ||
[:script {:async true | ||
:defer true | ||
:data-website-id "aabeabcb-ad76-47a4-9b4b-bef3fdc39af4" | ||
:src "https://bunseki.juxt.pro/umami.js"}] | ||
[:title "XTDB Play"]] | ||
[:body | ||
[:div {:id "app"}] | ||
[:script {:type "text/javascript" | ||
:src "/public/js/compiled/app.js"}] | ||
[:script {:type "text/javascript"} | ||
(str "var xt_version = '" util/xt-version "';")] | ||
[:script {:type "text/javascript"} | ||
"xt_play.app.init()"]])) |
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,20 @@ | ||
(ns xt-play.util | ||
(:require #?(:clj [clojure.edn :as edn]))) | ||
|
||
#?(:clj | ||
(def xt-version | ||
(-> (slurp "deps.edn") | ||
(edn/read-string) | ||
(get-in [:deps 'com.xtdb/xtdb-core :mvn/version])))) | ||
|
||
#?(:clj | ||
(def read-edn | ||
(partial edn/read-string {:readers *data-readers*}))) | ||
|
||
(defn map-results->rows | ||
[results] | ||
(let [ks (keys (apply merge results))] | ||
(into [(vec ks)] | ||
(mapv (fn [row] | ||
(mapv #(get row %) ks)) | ||
results)))) |
Oops, something went wrong.