On this page:
database?
make-database-factory
call-with-database-connection
call-with-database-transaction
with-database-connection
with-database-transaction
7.1 Database URL
parse-database-url

7 Database

 (require koyo/database) package: koyo-lib

This module provides a database component and functionality for working with database connections.

procedure

(database? v)  boolean?

  v : any/c
Returns #t when v is a database component.

procedure

(make-database-factory 
  connector 
  [#:max-connections max-connections 
  #:max-idle-connections max-idle-connections]) 
  (-> database?)
  connector : (-> connection?)
  max-connections : exact-positive-integer? = 16
  max-idle-connections : exact-positive-integer? = 2
Returns a function that will create a database component containing a DB connection pool of size #:max-connections which connects to the database using the connector.

procedure

(call-with-database-connection database    
  proc)  any
  database : database?
  proc : (-> connection? any)
Retrieves a database connection from the pool and passes it to proc. Once proc completes, the connection is returned to the pool.

Nested calls to call-with-database-connection reuse the same connection.

procedure

(call-with-database-transaction database    
  proc    
  [#:isolation isolation])  any
  database : database?
  proc : (-> connection? any)
  isolation : 
(or/c false/c
      'serializable
      'repeatable-read
      'read-committed
      'read-uncommitted)
 = #f
Retrieves a database connection from the pool, enters a transaction with the requested #:isolation level and passes the connection to proc. If proc completes successfully, the transaction is committed, otherwise it is rolled back.

Nested calls to call-with-database-transaction reuse the same connection and, if the database supports it, create nested transactions.

syntax

(with-database-connection [id database]
  e ...+)
 
  database : database?

syntax

(with-database-transaction [id database]
  maybe-isolation
  e ...+)
 
maybe-isolation = 
  | #:isolation isolation
 
  database : database?
  isolation : 
(or/c false/c
      'serializable
      'repeatable-read
      'read-committed
      'read-uncommitted)
These forms are syntactic sugar for calling call-with-database-connection and call-with-database-transaction, respectively, with an anonymous thunk.

For example, the following forms are equivalent:

(with-database-connection [c the-db]
  (query-value c "select 42"))
(call-with-database-connection the-db
  (lambda (c)
    (query-value c "select 42")))

7.1 Database URL

 (require koyo/database-url) package: koyo-lib

This module provides a function for parsing DATABASE_URL-style connection strings.

Parses a 12 Factor App-style DATABASE_URL into six values:

Examples:

> (parse-database-url "sqlite3:///:memory:")

#<procedure:sqlite3-connect>

#f

#f

'memory

#f

#f

> (parse-database-url "sqlite3:///db.sqlite3")

#<procedure:sqlite3-connect>

#f

#f

"db.sqlite3"

#f

#f

> (parse-database-url "sqlite3:////path/to/db.sqlite3")

#<procedure:sqlite3-connect>

#f

#f

"/path/to/db.sqlite3"

#f

#f

> (parse-database-url "postgres:///example")

#<procedure:postgresql-connect>

"127.0.0.1"

5432

"example"

#f

#f

> (parse-database-url "postgres://127.0.0.1:15432/example")

#<procedure:postgresql-connect>

"127.0.0.1"

15432

"example"

#f

#f

> (parse-database-url "postgres://user:password@127.0.0.1:15432/example")

#<procedure:postgresql-connect>

"127.0.0.1"

15432

"example"

"user"

"password"