fix #237 by adding :init-fn option

This commit is contained in:
Sean Corfield 2023-03-17 16:21:04 -07:00
parent 8f372917a4
commit b5674a169c
3 changed files with 16 additions and 2 deletions

View file

@ -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.

View file

@ -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).

View file

@ -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 [_]