KScript docs
EN

Databases: KScript.Data

Connections

Configuring connections

With no settings, the database is data/app.sqlite in the project folder, or the DATABASE_URL environment variable when it's set. To use other databases, list connections under database in the config section of kscript.json:

JSON
{
  "config": {
    "database": {
      "default": "main",
      "connections": {
        "main": { "url": "postgres://app:secret@localhost:5432/app" },
        "reports": { "url": "sqlite:data/reports.sqlite" }
      }
    }
  }
}

A model uses another connection with the connection: reports option, and @db(connection: reports) gives you that database.

Each database has its own URL:

  • SQLite: sqlite:data/app.sqlite, a file relative to the project folder, or sqlite::memory:, which lasts while kscript runs.
  • Postgres 12 or later: postgres://user:password@host:5432/app (or postgresql://).
  • MySQL 8.0.16 or later: mysql://user:password@host:3306/app.
  • MariaDB 10.6 or later: mariadb://user:password@host:3306/app.

In a URL, put driver options after ?, as in ?sslmode=require, and percent-encode characters such as @ and : in passwords.

You may also give a connection as a map of fields. Settings that come from environment variables go in config.kscript, which is laid over kscript.json (see Settings):

<fn name="configure" params="environment: string">
  <let name="url" value="@env('DATABASE_URL', default: '')" />
  <let name="main">{
      driver: 'postgres'
      host: @env('DB_HOST', default: 'localhost')
      port: @env('DB_PORT', default: '5432')
      database: @env('DB_NAME', default: 'app')
      username: @env('DB_USER', default: 'app')
      password: @env('DB_PASSWORD', default: '')
    }</let>
  <if !$url->is_blank()>
    <set name="main" value="{ url: $url }" />
  </if>
  <return value="{ database: { connections: { main: $main } } }" />
</fn>
fn configure(environment: string) {
  $url = @env('DATABASE_URL', default: '')
  $main = {
    driver: 'postgres'
    host: @env('DB_HOST', default: 'localhost')
    port: @env('DB_PORT', default: '5432')
    database: @env('DB_NAME', default: 'app')
    username: @env('DB_USER', default: 'app')
    password: @env('DB_PASSWORD', default: '')
  }
  if !$url->is_blank() => $main = { url: $url }
  return { database: { connections: { main: $main } } }
}

A password given as a field may contain @ and : as they are. Driver options go in options:, such as options: { sslmode: 'require' }.

Differences between databases

Models, queries, transactions and the database queue work the same on every database. KScript.Data smooths over the differences that would otherwise surprise you:

  • Text compares exactly, but emails don't mind case, so JOAN@example.com finds joan@example.com and a unique email can't be taken twice in different case. On Postgres, ->create_table() makes a case-insensitive collation for emails, kscript_nocase. On MySQL and MariaDB, tables use utf8mb4_bin and emails use utf8mb4_unicode_ci.
  • ->contains(), ->starts_with() and ->ends_with() don't mind case on any database, and ->matches() reads its flags (i, m, s, x) the same way everywhere.
  • Nulls sort first going up and last going down.
  • Dates are kept to the microsecond.

Raw SQL in @sql is passed to the database as it is, so write it in that database's own dialect.

Time zones

A date is stored as a clock time in the project's time zone: "timezone" in kscript.json, which is Europe/London unless you set another, such as America/New_York, UTC or any IANA name. A date is converted to that zone when it's written and back when it's read, so a row means the same moment on a laptop in one zone and a server in another.

You should choose the time zone before the project has data. Rows already stored keep the clock time they were written with. A database written before this setting existed holds the clock times of the computer that wrote it, so set "timezone" to that computer's zone ("UTC" for most servers and containers).

Edit this page on GitHub