Skip to content

Commit fff7f4c

Browse files
gahrGusted
andauthored
Add post_connect to sql_pool (#422)
* Add post_connect to sql_pool Allow for a custom post_connect to run for new SQL connection, for example registering user functions for SQLite connections. * Adjust signature, skip standard_post_connect --------- Co-authored-by: Gusted <postmaster@gusted.xyz>
1 parent 1fbb7fd commit fff7f4c

3 files changed

Lines changed: 18 additions & 6 deletions

File tree

docs/web/postprocess/index.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1951,7 +1951,8 @@ let graphiql_expected = {|<div class="spec value" id="val-graphiql">
19511951
|}
19521952

19531953
let sql_pool_expected = {|<div class="spec value" id="val-sql_pool">
1954-
<a href="#val-sql_pool" class="anchor"></a><code><span><span class="keyword">val</span> sql_pool : <span>?size:int <span class="arrow">-&gt;</span></span> <span>string <span class="arrow">-&gt;</span></span> <a href="#type-middleware">middleware</a></span></code>
1954+
<a href="#val-sql_pool" class="anchor"></a><code><span><span class="keyword">val</span> sql_pool : <span>?size:int <span class="arrow">-&gt;</span></span>
1955+
<span>?post_connect:<span>(<span><span>(<span class="keyword">module</span> <span class="xref-unresolved">Caqti_lwt</span>.CONNECTION)</span> <span class="arrow">-&gt;</span></span> <span><span><span>(unit,&nbsp;<span class="xref-unresolved">Caqti_error</span>.t)</span> <span class="xref-unresolved">Stdlib</span>.result</span> <a href="#type-promise">promise</a></span>)</span> <span class="arrow">-&gt;</span></span> <span>string <span class="arrow">-&gt;</span></span> <a href="#type-middleware">middleware</a></span></code>
19551956
</div>
19561957
|}
19571958

src/dream.mli

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,11 +1752,16 @@ val graphiql : ?default_query:string -> string -> handler
17521752
{{:https://cheatsheetseries.owasp.org/cheatsheets/Database_Security_Cheat_Sheet.html}
17531753
OWASP {i Database Security Cheat Sheet}}. *)
17541754

1755-
val sql_pool : ?size:int -> string -> middleware
1755+
val sql_pool :
1756+
?size:int ->
1757+
?post_connect: (Caqti_lwt.connection -> unit promise) ->
1758+
string ->
1759+
middleware
17561760
(** Makes an SQL connection pool available to its inner handler. [?size] is the
17571761
maximum number of concurrent connections that the pool will support. The
17581762
default value is picked by the driver. Note that for SQLite, [?size] is
1759-
capped to [1]. *)
1763+
capped to [1]. [post_connect] is an optional callback, which is called for
1764+
every new connection that is opened to the database. *)
17601765

17611766
val sql : request -> (Caqti_lwt.connection -> 'a promise) -> 'a promise
17621767
(** Runs the callback with a connection from the SQL pool. See example

src/sql/sql.ml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ let foreign_keys_on =
2525
(Caqti_type.unit ->. Caqti_type.unit) "PRAGMA foreign_keys = ON"
2626
[@ocaml.warning "-3"]
2727

28-
let post_connect (module Db : Caqti_lwt.CONNECTION) =
28+
let standard_post_connect (module Db : Caqti_lwt.CONNECTION) =
2929
match Caqti_driver_info.dialect_tag Db.driver_info with
3030
| `Sqlite -> Db.exec foreign_keys_on ()
3131
| _ -> Lwt.return (Ok ())
3232

33-
let sql_pool ?size uri =
33+
let sql_pool ?size ?post_connect uri =
3434
let pool_cell = ref None in
3535
fun inner_handler request ->
3636

@@ -47,9 +47,15 @@ let sql_pool ?size uri =
4747
log.warning (fun log -> log ~request
4848
"Dream.sql_pool: \
4949
'sqlite' is not a valid scheme; did you mean 'sqlite3'?");
50+
let post_connect =
51+
match post_connect with
52+
| None -> standard_post_connect
53+
| Some f -> (fun db -> Lwt.map Result.ok (f db))
54+
in
5055
let pool =
5156
let pool_config = Caqti_pool_config.create ?max_size:size () in
52-
Caqti_lwt_unix.connect_pool ~pool_config ~post_connect parsed_uri in
57+
Caqti_lwt_unix.connect_pool ~pool_config ~post_connect parsed_uri
58+
in
5359
match pool with
5460
| Ok pool ->
5561
pool_cell := Some pool;

0 commit comments

Comments
 (0)