clj-r2dbc.middleware
Functional middleware extensions for clj-r2dbc.
Middleware are higher-order functions (HOFs) of the form:
(fn [execute-fn] (fn [ctx] task)).
They compose left-to-right via (reduce comp identity middleware-seq): the
first middleware in the seq is the outermost wrapper. Unlike interceptors,
which use a data-driven enter/leave/error model, middleware are plain
function closures - simpler to write but without the structured error-phase
separation that interceptors provide.
Pass a seq of middleware fns via the :middleware option.
Provides:
with-logging - logs SQL timing, row count, and errors.
with-transaction - factory; wraps execution in begin/commit/rollback.
with-logging
added in 0.1
(with-logging execute-fn)
Wrap execute-fn to log SQL execution timing and row/update counts.
Params are intentionally omitted from logs.
Args:
execute-fn - 1-arity fn [ctx] -> task; the next handler in the chain.
Returns a 1-arity fn [ctx] -> task.
with-transaction
added in 0.1
(with-transaction & {:as tx-opts})
Return a middleware function that wraps execute-fn in begin/commit/rollback.
When :clj-r2dbc/in-transaction? is already true in the context, passes
through without modifying the transaction (join semantics).
Args:
tx-opts - (optional) transaction options map; defaults to {}.
:isolation - isolation level keyword
(see impl/transaction isolation-map).
:read-only? - boolean; default false.
:name - string savepoint or transaction name.
:lock-wait-timeout-ms - maximum time to wait for a lock, in ms.
Accepts a map, keyword arguments, or both.
Returns a middleware function [execute-fn] -> [ctx] -> task.
Example:
(with-transaction)
(with-transaction {:isolation :read-committed})
(with-transaction :isolation :read-committed)