Skip to content

Commit

Permalink
add additional db metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolap committed May 24, 2023
1 parent 82e369c commit 05dfd3d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/route_craft/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
(:require
[clojure.tools.logging :as log]
[donut.dbxray :as dbx]
[kit.edge.db.postgres]
[next.jdbc :as jdbc]
[next.jdbc.sql :as sql]
[ring.util.http-response :as http-response]
[kit.edge.db.postgres]))
[route-craft.xray-ext :as xray-ext]))

;; MVP

Expand Down Expand Up @@ -250,7 +251,7 @@
db-conn]
:as opts}]
(try
(let [db-xray (dbx/xray db-conn)]
(let [db-xray (-> db-conn (dbx/xray) (xray-ext/extend-db-xray))]
(routes-from-dbxray (update opts :malli-type-mappings #(merge base-malli-type-mappings %)) db-xray))
(catch Exception e
(log/error e "Failed to create reitit routes")
Expand Down
35 changes: 35 additions & 0 deletions src/route_craft/query_strings.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
(ns route-craft.query-strings)

;; Goal: Support JSON
;; Query string encodable
;; Secure

#_{:where {:fk_table_id {:eq fk_table_id}}
:joins {:user_id "left"}
:limit limit
:offset offset
:columns ["id" ...]
:order [{:id "asc"}]}

;; OPS
;; eq
;; ilike
;; gt
;; gte
;; lt
;; lte
;; in

;; TODO: logical ops, or, and?

;; table key
;; db-xray
;; refers-to permitted in joins
;; columns only permitted OR join columns only permitted
;; column checks on:
;; - where
;; - joins
;; - columns
;; - order keys
;; validate order vals as asc or desc
;; validate limit and offset as int
27 changes: 27 additions & 0 deletions src/route_craft/xray_ext.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
(ns route-craft.xray-ext
"Extended data given db-xray output")

(defn column-names
[tables table-prefix columns expand-references?]
(reduce-kv
(fn [acc k {:keys [refers-to]}]
(let [column-key (if table-prefix
(keyword (str (name table-prefix) "." (name k)))
k)]
(if (and expand-references? refers-to)
(let [refers-to-table (first refers-to)]
(apply conj acc column-key (column-names tables refers-to-table (get-in tables [refers-to-table :columns]) false)))
(conj acc column-key))))
#{}
columns))

(defn extend-db-xray
[db-xray]
(update db-xray :tables
(fn [tables]
(reduce-kv
(fn [acc table-kw {:keys [columns] :as table}]
(assoc acc table-kw
(assoc table :rc/permitted-columns (column-names tables nil columns true))))
{}
tables))))

0 comments on commit 05dfd3d

Please sign in to comment.