Skip to content

Commit

Permalink
fix a few edge cases, add logging to dev/test, write some notes for l…
Browse files Browse the repository at this point in the history
…ater
  • Loading branch information
nikolap committed May 22, 2023
1 parent 7311f96 commit 82e369c
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 35 deletions.
43 changes: 42 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,44 @@
# route-craft

WIP experiments in route generation based on PostgreSQL schema. Not ready for any real use.
WIP experiments in route generation based on PostgreSQL schema. Not ready for any real use.

## Rationale

## Usage

### Configuration Options

### Supported handlers

- `:insert-one` => `POST /`
- `:get-by-pk` => `GET /:{pk}`
- `:update-by-pk` => `PUT /:{pk}`
- `:delete-by-pk` => `DELETE /:{pk}`

#### Planned handlers

- `:get-many` => `GET /`
- `:update-by-query` => `PUT /`
- `:delete-by-query` => `DELETE /`

#### Rough ideas for handlers

- `:insert-many` => `POST /` with header?
- `:upsert-by-pk` => `PUT /:{pk}` with header?

## Auth and Permissions

## Query Parameters

## When not to use this

- Very high throughput APIs
- There is overhead with permission checking and other things that could be avoided with a more direct approach
- More complex handlers should be managed via custom requests
- Goal of route-craft is to handle the "80%" case of CRUD operations on database tables while permitting extensibility for the rest

## License

Copyright © 2023

Released under the MIT license.
16 changes: 10 additions & 6 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@
{:build {:deps {io.github.clojure/tools.build {:git/tag "v0.9.4" :git/sha "76b78fe"}}
:ns-default build}
:dev {:extra-paths ["test"]
:extra-deps {lambdaisland/kaocha {:mvn/version "1.75.1190"}
migratus/migratus {:mvn/version "1.4.9"}
metosin/reitit {:mvn/version "0.6.0"}}}
:extra-deps {lambdaisland/kaocha {:mvn/version "1.75.1190"}
migratus/migratus {:mvn/version "1.4.9"}
metosin/reitit {:mvn/version "0.6.0"}
org.apache.logging.log4j/log4j-api {:mvn/version "2.20.0"}
org.apache.logging.log4j/log4j-core {:mvn/version "2.20.0"}}}
:test {:extra-paths ["test"]
:extra-deps {lambdaisland/kaocha {:mvn/version "1.75.1190"}
migratus/migratus {:mvn/version "1.4.9"}
metosin/reitit {:mvn/version "0.6.0"}}
:extra-deps {lambdaisland/kaocha {:mvn/version "1.75.1190"}
migratus/migratus {:mvn/version "1.4.9"}
metosin/reitit {:mvn/version "0.6.0"}
org.apache.logging.log4j/log4j-api {:mvn/version "2.20.0"}
org.apache.logging.log4j/log4j-core {:mvn/version "2.20.0"}}
:main-opts ["-m" "kaocha.runner"]}}}
58 changes: 30 additions & 28 deletions src/route_craft/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@
(if (= 1 (count pk-cols))
(first pk-cols)
(throw (ex-info "Unexpected primary key count, please define manually" {:count (count pk-cols)
:type ::unexpected-pk-count})))))
:type ::unexpected-pk-count})))))

(defn table->reitit-routes
[{:keys [table-definitions default-handlers throw-on-failure? malli-type-mappings]
Expand All @@ -190,33 +190,35 @@
table]
(let [table-opts (get table-definitions table)]
(when-not (:ignore? table-opts)
(try (let [dbtable (get tables table)
pk-key (or (:pk-key table-opts) (get-pk-from-table dbtable))
pk-key-path (str "/" pk-key)
generate-handler-fn (partial generate-handler {:table-name table
:table dbtable
:pk-key pk-key
:malli-type-mappings malli-type-mappings})]
(->> (reduce
(fn [out handler]
(let [path (case handler
:insert-one ["" :post]
:get-by-pk [pk-key-path :get]
:update-by-pk [pk-key-path :put]
:delete-by-pk [pk-key-path :delete]
(throw (ex-info "Unsupported handler" {:type ::unsupported-handler
:handler handler})))]
(assoc-in out path (generate-handler-fn handler))))
{}
(get-in table-definitions [table :handlers] default-handlers))
(vec)
(cons (str "/" (name table)))
(vec)))
(catch Exception e
(log/trace e "Route generation exception")
(log/warn "Failed to generate routes for table" table)
(when throw-on-failure?
(throw e)))))))
(let [handlers (get-in table-definitions [table :handlers] default-handlers)]
(when (seq handlers)
(try (let [dbtable (get tables table)
pk-key (or (:pk-key table-opts) (get-pk-from-table dbtable))
pk-key-path (str "/" pk-key)
generate-handler-fn (partial generate-handler {:table-name table
:table dbtable
:pk-key pk-key
:malli-type-mappings malli-type-mappings})]
(->> (reduce
(fn [out handler]
(let [path (case handler
:insert-one ["" :post]
:get-by-pk [pk-key-path :get]
:update-by-pk [pk-key-path :put]
:delete-by-pk [pk-key-path :delete]
(throw (ex-info "Unsupported handler" {:type ::unsupported-handler
:handler handler})))]
(assoc-in out path (generate-handler-fn handler))))
{}
handlers)
(vec)
(cons (str "/" (name table)))
(vec)))
(catch Exception e
(log/trace e "Route generation exception")
(log/warn "Failed to generate routes for table" table)
(when throw-on-failure?
(throw e)))))))))

(defn routes-from-dbxray
[opts {:keys [table-order] :as db-xray}]
Expand Down
10 changes: 10 additions & 0 deletions test/resources/log4j.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
status = trace
monitorInterval = 5

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %date %level %logger %message%n%throwable

rootLogger.level = info
rootLogger.appenderRef.stdout.ref = STDOUT

0 comments on commit 82e369c

Please sign in to comment.