KScript docs
EN

database fields and methods

A field is a plain fact about the value, read with a dot and no brackets: $value.name. A method takes arguments or does work, and is called with ->: $value->name(arguments), or with the value piped in: $value |> name(arguments).

Fields

FieldTypeDescription
.driverstringThe kind of database: sqlite, postgres, mysql or sqlserver.
.filestringThe database file, for SQLite, or null.
.is_in_transactionboolTrue while a transaction is open on this connection.
.namestringThe connection's name in kscript.json.

From KScript.Data

These need <import package="KScript.Data" />import KScript.Data. See KScript.Data.

#database->create_table(model: model) → bool KScript.Data

Makes a model's table and indexes, if the database doesn't have them yet, and the pivot tables of its many to many relations whose other table is there. Make the tables a model links to first. Good for scripts and tests; projects use migrations.
ParameterTakes
model requiredmodel
Example
<import package="KScript.Data" />
<model name="Note" fields="text: string" />
<echo>{ $db->create_table(Note) } { $db->create_table(Note) }</echo>
import KScript.Data
model Note {
  text: string
}
echo "{ $db->create_table(Note) } { $db->create_table(Note) }"

#database->describe_table(table: model|string) → map KScript.Data

Returns columns, indexes, and keys for the named table or model.
ParameterTakes
table requiredmodel|string
Example
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<let name="table" value="$db->describe_table(Task)" />
<echo>{ $table.columns->map(fn(c) => $c.name) }</echo>
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
  title: string
  done: bool = false
  points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
$table = $db->describe_table(Task)
echo $table.columns->map(fn(c) => $c.name)

#database->drop_table(table) → bool KScript.Data

Deletes a table and everything in it, if it is there: a name, or a model's table. True when there was one.
Example
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<echo>{ $db->drop_table(Task) } { $db->has_table('tasks') }</echo>
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
  title: string
  done: bool = false
  points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
echo "{ $db->drop_table(Task) } { $db->has_table('tasks') }"

#database->has_table(name: model|string) → bool KScript.Data

True when the database has a model's table or a table with the given name: $db->has_table(User) or $db->has_table('users').
ParameterTakes
name requiredmodel|string
Example
<import package="KScript.Data" />
<model name="Note" fields="text: string" />
<echo>{ $db->has_table(Note) }</echo>
<do>$db->create_table(Note)</do>
<echo>{ $db->has_table('notes') }</echo>
import KScript.Data
model Note {
  text: string
}
echo $db->has_table(Note)
$db->create_table(Note)
echo $db->has_table('notes')

#database->sql(text, as?) → any KScript.Data

Runs SQL, like @sql, on this connection.

Arguments can be named: ->sql(…, as: …).

Example
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<let name="rows" value="$db->sql('select title from tasks order by points')" />
<echo>{ $rows->map(fn(r) => $r.title) }</echo>
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
  title: string
  done: bool = false
  points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
$rows = $db->sql('select title from tasks order by points')
echo $rows->map(fn(r) => $r.title)

#database->table(name) → query KScript.Data

A query on a table with no model: rows come back as maps.
Example
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<echo>{ $db->table('tasks')->where(done: false)->count() } rows</echo>
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
  title: string
  done: bool = false
  points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
echo "{ $db->table('tasks')->where(done: false)->count() } rows"

#database->tables() → list KScript.Data

The names of the tables in the database.
Example
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<echo>{ $db->tables() }</echo>
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
  title: string
  done: bool = false
  points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
echo $db->tables()

#database->transaction(work: fn, retries?: number) → any KScript.Data

Runs work in a transaction on this connection and returns what it returns: everything it saves is kept together, or (when it fails) not at all. retries tries again when the database is busy.
ParameterTakes
work requiredfn
retries optionalnumber

Arguments can be named: ->transaction(…, retries: …).

Example
<import package="KScript.Data" />
<model name="Task" with="SoftDeletes" fields="title: string, done: bool = false, points: int = 1" unique="title" />
<do>$db->create_table(Task); Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])</do>
<do>$db->transaction(fn() => Task->where(done: false)->update(done: true))</do>
<echo>{ Task->where(done: true)->count() } done</echo>
import KScript.Data
model Task(unique: [title]) extends SoftDeletes {
  title: string
  done: bool = false
  points: int = 1
}
$db->create_table(Task)
Task->insert([{ title: 'Write docs', points: 3 }, { title: 'Fix bug', done: true, points: 2 }, { title: 'Ship it', points: 5 }])
$db->transaction(fn() => Task->where(done: false)->update(done: true))
echo "{ Task->where(done: true)->count() } done"

Methods from any