Skip to content

Commit

Permalink
Merge pull request #17 from parenthesin/refact/using-jdbc-url-instead…
Browse files Browse the repository at this point in the history
…-db-specs

refact(jdbc): using jdbc-url instead db-specs
  • Loading branch information
rafaeldelboni authored Nov 7, 2024
2 parents 118a6f3 + 73807f9 commit ce473a8
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 17 deletions.
6 changes: 5 additions & 1 deletion .clj-kondo/taoensso/encore/config.edn
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
{:hooks {:analyze-call {taoensso.encore/defalias taoensso.encore/defalias}}}
{:hooks
{:analyze-call
{taoensso.encore/defalias taoensso.encore/defalias
taoensso.encore/defn-cached taoensso.encore/defn-cached
taoensso.encore/defonce taoensso.encore/defonce}}}
57 changes: 46 additions & 11 deletions .clj-kondo/taoensso/encore/taoensso/encore.clj
Original file line number Diff line number Diff line change
@@ -1,16 +1,51 @@
(ns taoensso.encore
"I don't personally use clj-kondo, so these hooks are
kindly authored and maintained by contributors.
PRs very welcome! - Peter Taoussanis"
(:refer-clojure :exclude [defonce])
(:require
[clj-kondo.hooks-api :as hooks]))

(defn defalias [{:keys [node]}]
(defn defalias
[{:keys [node]}]
(let [[sym-raw src-raw] (rest (:children node))
src (if src-raw src-raw sym-raw)
sym (if src-raw
sym-raw
(symbol (name (hooks/sexpr src))))]
{:node (with-meta
(hooks/list-node
[(hooks/token-node 'def)
(hooks/token-node (hooks/sexpr sym))
(hooks/token-node (hooks/sexpr src))])
(meta src))}))
src (or src-raw sym-raw)
sym (if src-raw sym-raw (symbol (name (hooks/sexpr src))))]
{:node
(with-meta
(hooks/list-node
[(hooks/token-node 'def)
(hooks/token-node (hooks/sexpr sym))
(hooks/token-node (hooks/sexpr src))])
(meta src))}))

(defn defn-cached
[{:keys [node]}]
(let [[sym _opts binding-vec & body] (rest (:children node))]
{:node
(hooks/list-node
(list
(hooks/token-node 'def)
sym
(hooks/list-node
(list*
(hooks/token-node 'fn)
binding-vec
body))))}))

(defn defonce
[{:keys [node]}]
;; args = [sym doc-string? attr-map? init-expr]
(let [[sym & args] (rest (:children node))
[doc-string args] (if (and (hooks/string-node? (first args)) (next args)) [(hooks/sexpr (first args)) (next args)] [nil args])
[attr-map init-expr] (if (and (hooks/map-node? (first args)) (next args)) [(hooks/sexpr (first args)) (fnext args)] [nil (first args)])

attr-map (if doc-string (assoc attr-map :doc doc-string) attr-map)
sym+meta (if attr-map (with-meta sym attr-map) sym)
rewritten
(hooks/list-node
[(hooks/token-node 'clojure.core/defonce)
sym+meta
init-expr])]

{:node rewritten}))
9 changes: 6 additions & 3 deletions src/parenthesin/components/db/jdbc_hikari.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
(defrecord Database [config ^HikariDataSource datasource]
component/Lifecycle
(start [this]
(let [{:keys [host port dbtype] :as db-spec} (get-in config [:config :database])]
(logs/log :info :database :start {:host host :port port :dbtype dbtype})
(let [db-spec (get-in config [:config :database])
jdbc-url (connection/jdbc-url (dissoc db-spec :username :password))
db-auth (select-keys db-spec [:username :password])]
(logs/log :info :database :start jdbc-url)
(if datasource
this
(assoc this :datasource (connection/->pool HikariDataSource db-spec)))))
(assoc this :datasource (connection/->pool HikariDataSource (assoc db-auth :jdbcUrl jdbc-url))))))

(stop [this]
(logs/log :info :database :stop)
(if datasource
Expand Down
6 changes: 4 additions & 2 deletions src/parenthesin/helpers/migrations.clj
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
(ns parenthesin.helpers.migrations
(:require [migratus.core :as migratus]
[next.jdbc :as jdbc]
[next.jdbc.connection :as connection]
[parenthesin.components.config.aero :as config.aero])
(:gen-class))

(defn get-connection
([]
(get-connection {}))
([input-map]
(let [{:keys [username] :as db} (-> (config.aero/read-config input-map) :database)]
(jdbc/get-connection (assoc db :user username)))))
(let [{:keys [username] :as db-spec} (-> (config.aero/read-config input-map) :database)
jdbc-url (connection/jdbc-url (assoc db-spec :user username))]
(jdbc/get-connection {:jdbcUrl jdbc-url}))))

(def configuration
{:store :database
Expand Down

0 comments on commit ce473a8

Please sign in to comment.