From b5674a169c3479fed5b9b2d1953c70ab74b51310 Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Fri, 17 Mar 2023 16:21:04 -0700 Subject: [PATCH] fix #237 by adding :init-fn option --- CHANGELOG.md | 1 + doc/getting-started.md | 6 ++++++ src/next/jdbc/connection.clj | 11 +++++++++-- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f410ee..ac196fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ Only accretive/fixative changes will be made from now on. * 1.3.next in progress * Address [#245](https://github.com/seancorfield/next-jdbc/issues/245) by not `locking` the `Connection` when `*nested-tx*` is bound to `:ignore` -- improving `clojure.java.jdbc` compatibility. + * Address [#237](https://github.com/seancorfield/next-jdbc/issues/237) by adding an `:init-fn` option to the `db-spec` argument for `next.jdbc.connection/component`. * 1.3.862 -- 2023-03-13 * Fix [#243](https://github.com/seancorfield/next-jdbc/issues/243) by ensuring URI properties become keywords. diff --git a/doc/getting-started.md b/doc/getting-started.md index ece9a3f..905563c 100644 --- a/doc/getting-started.md +++ b/doc/getting-started.md @@ -568,6 +568,12 @@ If you are using [Component](https://github.com/stuartsierra/component), a conne (component/stop ds))))) ``` +If you have want to either modify the connection pooled datasource after it is +created, or want to perform some database initialization, you can pass a +function as `:init-fn` in the `db-spec` hash map. The `component` function +will arrange for that initialization function to be invoked on the newly-created +datasource whenever `start` is called on the Component returned. + ## Working with Additional Data Types By default, `next.jdbc` relies on the JDBC driver to handle all data type conversions when reading from a result set (to produce Clojure values from SQL values) or setting parameters (to produce SQL values from Clojure values). Sometimes that means that you will get back a database-specific Java object that would need to be manually converted to a Clojure data structure, or that certain database column types require you to manually construct the appropriate database-specific Java object to pass into a SQL operation. You can usually automate those conversions using either the [`ReadableColumn` protocol](/doc/result-set-builders.md#readablecolumn) (for converting database-specific types to Clojure values) or the [`SettableParameter` protocol](/doc/prepared-statements.md#prepared-statement-parameters) (for converting Clojure values to database-specific types). diff --git a/src/next/jdbc/connection.clj b/src/next/jdbc/connection.clj index 674b738..db21c15 100644 --- a/src/next/jdbc/connection.clj +++ b/src/next/jdbc/connection.clj @@ -1,4 +1,4 @@ -;; copyright (c) 2018-2021 Sean Corfield, all rights reserved +;; copyright (c) 2018-2023 Sean Corfield, all rights reserved (ns next.jdbc.connection "Standard implementations of `get-datasource` and `get-connection`. @@ -288,6 +288,11 @@ called on it to shutdown the datasource (and return a new startable entity). + If `db-spec` contains `:init-fn`, that is assumed to be a function + that should be called on the newly-created datasource. This allows for + modification of (mutable) connection pooled datasource and/or some sort + of database initialization/setup to be called automatically. + By default, the datasource is shutdown by calling `.close` on it. If the datasource class implements `java.io.Closeable` then a direct, type-hinted call to `.close` will be used, with no reflection, @@ -305,7 +310,9 @@ (with-meta {} {'com.stuartsierra.component/start (fn [_] - (let [pool (->pool clazz db-spec)] + (let [init-fn (:init-fn db-spec) + pool (->pool clazz (dissoc db-spec :init-fn))] + (when init-fn (init-fn pool)) (with-meta (fn ^DataSource [] pool) {'com.stuartsierra.component/stop (fn [_]