Database URL Parser
7.7

Database URL Parser

The Twelve-Factor App conventions now followed by many web developers recommend that web applications gets their configuration from environment variables. Database connection parameters are usually given all in one environment variable in the form of a URL. This variable is normally called DATABASE_URL. This module provides procedures to translate database URLs into a form that Racket’s db module can use.

MySQL, PostgreSQL and SQLite URLs are currently supported.

procedure

(database-url-parse u)  (Values hash procedure)

  u : (one-of/c url string #f)
Parse u as a database URL. u can be a url object, a string, or #f. In case of #f the URL is read from the DATABASE_URL environment variable.

The procedure returns two values: a hash table of keyword arguments suitable for a database connect procedure from the db libray; and the right connect procedure to use.

For example:

> (database-url-parse "mysql://user:pass@localhost/dbname")
'#hash((#:database . "dbname")
       (#:password . "pass")
       (#:server . "localhost")
       (#:user . "user"))
|#<procedure:mysql-connect>|

procedure

(database-url-connector u)  (-> any)

  u : (one-of/c url string #f)
Like database-url-parse but instead of returning a connect procedure and its keyword arguments separately, returns a closure of no arguments that will call the right connector with the right arguments to connect to the database.

Most people will probably want to skip database-url-parse and use this procedure directly.