KScript docs
EN

model 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).

From KScript.Data

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

#model->all() → Self[] KScript.Data

Every record (leaving out soft-deleted ones).
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>{ Task->all()->map(fn(t) => $t.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 }])
echo Task->all()->map(fn(t) => $t.title)

#model->average(column: Self.fields) → number KScript.Data

The average of a column, or null when no rows match.
ParameterTakes
column requiredSelf.fields
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>{ Task->average(points)->round(2) }</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 Task->average(points)->round(2)

#model->chunk(size: number, work: fn) KScript.Data

Reads the rows a batch at a time and calls work with each batch (and its number from 0): for going through more rows than fit in memory. Return false from work to stop.
ParameterTakes
size requirednumber
work requiredfn

Arguments can be named: ->chunk(…, work: …).

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>
<fn name="show" params="batch: list, n: int">
  <echo>{ $n }: { $batch->map(fn(t) => $t.title)->join(', ') }</echo>
</fn>
<do>Task->order_by(id)->chunk(2, @show)</do>
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 }])
fn show(batch: list, n: int) {
  echo "{ $n }: { $batch->map(fn(t) => $t.title)->join(', ') }"
}
Task->order_by(id)->chunk(2, @show)

#model->count() → number KScript.Data

How many rows match.
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>{ Task->count() } tasks, { Task->where(done: false)->count() } to do</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 "{ Task->count() } tasks, { Task->where(done: false)->count() } to do"

#model->create(values...: Self) → Self KScript.Data

Checks the values against the model, saves a new record and returns it: User->create({ name: 'Joan', email: 'joan@example.com' }) or User->create(name: 'Joan', ...). Guarded fields are left out.
ParameterTakes
valuesSelf, any number of them
Example
<import package="KScript.Data" />
<model name="Note" fields="text: string, pinned: bool = false" />
<do>$db->create_table(Note)</do>
<let name="note" value="Note->create(text: 'Buy milk')" />
<echo>{ $note.id }: { $note.text }, pinned: { $note.pinned }</echo>
import KScript.Data
model Note {
  text: string
  pinned: bool = false
}
$db->create_table(Note)
$note = Note->create(text: 'Buy milk')
echo "{ $note.id }: { $note.text }, pinned: { $note.pinned }"

#model->cursor_paginate(per_page?: number, after?, key?: string, descending?: bool) → map KScript.Data

Reads one page using a unique, non-null key (the model key by default). Returns { items, has_more, next }; pass next as after to continue.
ParameterTakes
per_page optionalnumber
after optionalany value
key optionalstring
descending optionala flag: descending or not descending

Arguments can be named: ->cursor_paginate(…, after: …, key: …).

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="page" value="Task->cursor_paginate(2)" />
<echo>{ $page.items->map(fn(t) => $t.title) }, more: { $page.has_more }</echo>
<let name="rest" value="Task->cursor_paginate(2, after: $page.next)" />
<echo>{ $rest.items->map(fn(t) => $t.title) }, more: { $rest.has_more }</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 }])
$page = Task->cursor_paginate(2)
echo "{ $page.items->map(fn(t) => $t.title) }, more: { $page.has_more }"
$rest = Task->cursor_paginate(2, after: $page.next)
echo "{ $rest.items->map(fn(t) => $t.title) }, more: { $rest.has_more }"

#model->decrement(column: Self.fields, by?: number) → number KScript.Data

Takes away from a number column on every row that matches (1 by default). Returns how many rows changed.
ParameterTakes
column requiredSelf.fields
by optionalnumber

Arguments can be named: ->decrement(…, by: …).

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>Task->where(title: 'Ship it')->decrement(points)</do>
<echo>{ Task->where(title: 'Ship it')->value(points) }</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 }])
Task->where(title: 'Ship it')->decrement(points)
echo Task->where(title: 'Ship it')->value(points)

#model->delete() → number KScript.Data

Deletes every row that matches: models that extend SoftDeletes only mark them deleted. Returns how many.
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>{ Task->where(done: true)->delete() } deleted</echo>
<echo>{ Task->count() } left, { Task->with_trashed()->count() } kept to restore</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 "{ Task->where(done: true)->delete() } deleted"
echo "{ Task->count() } left, { Task->with_trashed()->count() } kept to restore"

#model->distinct() → query KScript.Data

Leaves out rows that repeat an earlier one, in the columns selected.
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>{ Task->select(done)->distinct()->count() }</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 Task->select(done)->distinct()->count()

#model->exists() → bool KScript.Data

True when any row matches.
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>{ Task->where(done: true)->exists() } { Task->where(points: 99)->exists() }</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 "{ Task->where(done: true)->exists() } { Task->where(points: 99)->exists() }"

#model->fake(values...: Self) → Self KScript.Data

Saves a record with made-up values for the fields that need one, and these values: User->fake(role: 'admin'). Optional fields and fields with defaults are left to the model, and a required relation gets a fake record too. For tests and seeders.
ParameterTakes
valuesSelf, any number of them
Example
<import package="KScript.Data" />
<model name="Person" fields="name: string, email: email" />
<do>$db->create_table(Person)</do>
<let name="joan" value="Person->fake(name: 'Joan')" />
<echo>{ $joan.name } { $joan.email->ends_with('@example.com') }</echo>
import KScript.Data
model Person {
  name: string
  email: email
}
$db->create_table(Person)
$joan = Person->fake(name: 'Joan')
echo "{ $joan.name } { $joan.email->ends_with('@example.com') }"

#model->fake_many(count: number, values...: Self) → Self[] KScript.Data

Saves count fake records, each with these values, and returns them: Post->fake_many(3, author: $joan).
ParameterTakes
count requirednumber
valuesSelf, any number of them

Arguments can be named: ->fake_many(…, values: …).

Example
<import package="KScript.Data" />
<model name="Person" fields="name: string, email: email" />
<do>$db->create_table(Person)</do>
<let name="people" value="Person->fake_many(3)" />
<echo>{ $people->count() } made, { Person->count() } saved</echo>
import KScript.Data
model Person {
  name: string
  email: email
}
$db->create_table(Person)
$people = Person->fake_many(3)
echo "{ $people->count() } made, { Person->count() } saved"

#model->find(id) → Self? KScript.Data

The record with this key, or null.
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>{ Task->find(1).title }</echo>
<echo>{ Task->find(99) ?? 'none' }</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 Task->find(1).title
echo Task->find(99) ?? 'none'

#model->find_or_fail(id) → Self KScript.Data

The record with this key, or a ModelNotFound error (a 404 in a route).
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>
<try>
  <do>Task->find_or_fail(99)</do>
  <catch type="ModelNotFound" as="e">
    <echo>{ $e.type }: { $e.message }</echo>
  </catch>
</try>
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 }])
try {
  Task->find_or_fail(99)
} catch ModelNotFound as e {
  echo "{ $e.type }: { $e.message }"
}

#model->first() → Self? KScript.Data

The first row, or null when none match.
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="task" value="Task->order_by(points)->first()" />
<echo>{ $task.title }</echo>
<echo>{ Task->where(points: 99)->first() ?? 'none' }</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 }])
$task = Task->order_by(points)->first()
echo $task.title
echo Task->where(points: 99)->first() ?? 'none'

#model->first_or_create(match: map, values?: map) → Self KScript.Data

The first record that matches, or a new one made from both maps.
ParameterTakes
match requiredmap
values optionalmap

Arguments can be named: ->first_or_create(…, values: …).

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="plan" value="Task->first_or_create({ title: 'Plan' }, { points: 2 })" />
<let name="again" value="Task->first_or_create({ title: 'Plan' })" />
<echo>{ $plan.points } points; same record: { $again.id == $plan.id }; { Task->count() } 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 }])
$plan = Task->first_or_create({ title: 'Plan' }, { points: 2 })
$again = Task->first_or_create({ title: 'Plan' })
echo "{ $plan.points } points; same record: { $again.id == $plan.id }; { Task->count() } tasks"

#model->first_or_fail() → Self KScript.Data

The first row, or a ModelNotFound error (a 404 in a route) when none match.
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>
<try>
  <do>Task->where(points: 99)->first_or_fail()</do>
  <catch type="ModelNotFound" as="e">
    <echo>{ $e.type }</echo>
  </catch>
</try>
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 }])
try {
  Task->where(points: 99)->first_or_fail()
} catch ModelNotFound as e {
  echo $e.type
}

#model->first_or_new(match: map, values?: map) → Self KScript.Data

The first record that matches, or a new one made from both maps that isn't saved yet: call ->save() to keep it.
ParameterTakes
match requiredmap
values optionalmap

Arguments can be named: ->first_or_new(…, values: …).

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="plan" value="Task->first_or_new({ title: 'Plan' }, { points: 2 })" />
<echo>{ $plan->is_saved() }</echo>
<do>$plan->save()</do>
<echo>{ Task->count() } 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 }])
$plan = Task->first_or_new({ title: 'Plan' }, { points: 2 })
echo $plan->is_saved()
$plan->save()
echo "{ Task->count() } tasks"

#model->force_delete() → number KScript.Data

Deletes every row that matches, even for models that extend SoftDeletes. Returns how many.
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>{ Task->where(done: true)->force_delete() } removed</echo>
<echo>{ Task->with_trashed()->count() } left in the table</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 "{ Task->where(done: true)->force_delete() } removed"
echo "{ Task->with_trashed()->count() } left in the table"

#model->get() → Self[] KScript.Data

Runs the query: a list of records (maps, for a table with no model).
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>
<for each="task" in="Task->where(done: false)->order_by(title)->get()">
  <echo>{ $task.title }: { $task.points }</echo>
</for>
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 }])
for task in Task->where(done: false)->order_by(title)->get() {
  echo "{ $task.title }: { $task.points }"
}

#model->group_by(columns...: Self.fields) → grouped_query KScript.Data

Splits the rows into groups with the same values in these columns, for totals per group: Order->group_by(status)->count() gives { open: 3, paid: 5 }. With two columns each total is in a map in a map.
ParameterTakes
columnsSelf.fields, any number of them
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>{ Task->group_by(done)->count() }</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 Task->group_by(done)->count()

#model->in_random_order() → query KScript.Data

Reads the rows in a random order, a new one each time: Quote->in_random_order()->first().
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>{ Task->in_random_order()->count() } tasks, in a new order each time</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 "{ Task->in_random_order()->count() } tasks, in a new order each time"

#model->increment(column: Self.fields, by?: number) → number KScript.Data

Adds to a number column on every row that matches (1 by default). Returns how many rows changed.
ParameterTakes
column requiredSelf.fields
by optionalnumber

Arguments can be named: ->increment(…, by: …).

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>Task->where(title: 'Ship it')->increment(points, by: 2)</do>
<echo>{ Task->where(title: 'Ship it')->value(points) }</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 }])
Task->where(title: 'Ship it')->increment(points, by: 2)
echo Task->where(title: 'Ship it')->value(points)

#model->insert(rows) → number KScript.Data

Adds rows: a map, or a list of maps. On a model each one is checked like ->create(). Returns how many were added.
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>{ Task->insert([{ title: 'Plan' }, { title: 'Review', points: 2 }]) } added</echo>
<echo>{ Task->count() } 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 "{ Task->insert([{ title: 'Plan' }, { title: 'Review', points: 2 }]) } added"
echo "{ Task->count() } tasks"

#model->latest(column?: Self.fields) → query KScript.Data

Sorts newest first by the supplied column. Defaults to created_at, or the primary key for a model that doesn't extend Timestamps.
ParameterTakes
column optionalSelf.fields
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>{ Task->latest(points)->value(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 }])
echo Task->latest(points)->value(title)

#model->limit(count: number) → query KScript.Data

Reads at most this many rows.
ParameterTakes
count requirednumber
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>{ Task->order_by(title)->limit(2)->pluck(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 }])
echo Task->order_by(title)->limit(2)->pluck(title)

#model->lock_for_update() → query KScript.Data

Locks the rows read until the transaction ends, so nobody else changes them first. Use it inside a transaction. On SQLite, which locks the whole database for a transaction's writes instead, it changes nothing.
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>
<transaction>
  <let name="next" value="Task->where(done: false)->order_by(id)->lock_for_update()->first()" />
  <do>$next->update(done: true)</do>
</transaction>
<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 }])
transaction {
  $next = Task->where(done: false)->order_by(id)->lock_for_update()->first()
  $next->update(done: true)
}
echo "{ Task->where(done: true)->count() } done"

#model->max(column: Self.fields) → Self[column] KScript.Data

The largest value of a column, or null when no rows match.
ParameterTakes
column requiredSelf.fields
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>{ Task->max(points) }</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 Task->max(points)

#model->min(column: Self.fields) → Self[column] KScript.Data

The smallest value of a column, or null when no rows match.
ParameterTakes
column requiredSelf.fields
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>{ Task->min(points) }</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 Task->min(points)

#model->offset(count: number) → query KScript.Data

Skips this many rows first.
ParameterTakes
count requirednumber
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>{ Task->order_by(title)->offset(1)->limit(1)->pluck(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 }])
echo Task->order_by(title)->offset(1)->limit(1)->pluck(title)

#model->oldest(column?: Self.fields) → query KScript.Data

Sorts oldest first by the supplied column. Defaults to created_at, or the primary key for a model that doesn't extend Timestamps.
ParameterTakes
column optionalSelf.fields
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>{ Task->oldest(points)->value(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 }])
echo Task->oldest(points)->value(title)

#model->only_trashed() → query KScript.Data

For models that extend SoftDeletes: reads only the deleted records.
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>Task->where(done: true)->delete()</do>
<echo>{ Task->only_trashed()->pluck(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 }])
Task->where(done: true)->delete()
echo Task->only_trashed()->pluck(title)

#model->or_where(conditions...: Self) → query KScript.Data

Like ->where(), joined with or: rows that match the conditions so far, or these.
ParameterTakes
conditionsSelf, any number of them
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>{ Task->where(done: true)->or_where(points: 5)->pluck(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 }])
echo Task->where(done: true)->or_where(points: 5)->pluck(title)

#model->order_by(columns...: Self.fields) → query KScript.Data

Sorts the rows: ->order_by(name), ->order_by(created_at: desc, name: asc).
ParameterTakes
columnsSelf.fields, any number of them
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>{ Task->order_by(points: desc)->pluck(title) }</echo>
<echo>{ Task->order_by(done, title)->pluck(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 }])
echo Task->order_by(points: desc)->pluck(title)
echo Task->order_by(done, title)->pluck(title)

#model->paginate(per_page?: number, page?) → map KScript.Data

One page of rows: { items, page, per_page, total, last_page, from, to, has_more }. per_page is 15 by default; page (1 by default) can be a number or text such as $query.page, and text that isn't a number is page 1.
ParameterTakes
per_page optionalnumber
page optionalany value

Arguments can be named: ->paginate(…, page: …).

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="page" value="Task->order_by(id)->paginate(2, page: 1)" />
<echo>{ $page.items->map(fn(t) => $t.title) }: page { $page.page } of { $page.last_page }, { $page.total } in all</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 }])
$page = Task->order_by(id)->paginate(2, page: 1)
echo "{ $page.items->map(fn(t) => $t.title) }: page { $page.page } of { $page.last_page }, { $page.total } in all"

#model->pluck(column: Self.fields) → Self[column][] KScript.Data

One column of every row that matches, as a list: User->pluck(email).
ParameterTakes
column requiredSelf.fields
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>{ Task->order_by(title)->pluck(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 }])
echo Task->order_by(title)->pluck(title)

#model->query() → query KScript.Data

A query on the model with no conditions yet, to build on.
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="query" value="Task->query()" />
<let name="query" value="$query->where(done: false)" />
<echo>{ $query->count() } to do</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 }])
$query = Task->query()
$query = $query->where(done: false)
echo "{ $query->count() } to do"

#model->restore() → number KScript.Data

For models that extend SoftDeletes: brings back the deleted records that match. Returns how many.
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>Task->where(done: true)->delete()</do>
<echo>{ Task->only_trashed()->restore() } restored, { Task->count() } 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 }])
Task->where(done: true)->delete()
echo "{ Task->only_trashed()->restore() } restored, { Task->count() } tasks"

#model->select(columns...: Self.fields) → query KScript.Data

Reads only these columns: ->select(id, name). Records get null for the others.
ParameterTakes
columnsSelf.fields, any number of them
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="task" value="Task->select(id, title)->first()" />
<echo>{ $task.title }: { $task.points ?? 'not read' }</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 }])
$task = Task->select(id, title)->first()
echo "{ $task.title }: { $task.points ?? 'not read' }"

#model->skip_locked() → query KScript.Data

With ->lock_for_update(): skips rows another transaction has locked, instead of waiting. Good for taking jobs from a queue. On SQLite, which has no row locks, it changes nothing.
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>
<transaction>
  <let name="next" value="Task->where(done: false)->order_by(id)->lock_for_update()->skip_locked()->first()" />
  <echo>{ $next.title }</echo>
</transaction>
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 }])
transaction {
  $next = Task->where(done: false)->order_by(id)->lock_for_update()->skip_locked()->first()
  echo $next.title
}

#model->sole() → Self KScript.Data

The one row that matches: a ModelNotFound error (a 404 in a route) when none do, and a MultipleRecordsFound error when more than one does.
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>{ Task->where(title: 'Fix bug')->sole().points }</echo>
<try>
  <do>Task->where(done: false)->sole()</do>
  <catch type="MultipleRecordsFound" as="e">
    <echo>{ $e.type }</echo>
  </catch>
</try>
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 Task->where(title: 'Fix bug')->sole().points
try {
  Task->where(done: false)->sole()
} catch MultipleRecordsFound as e {
  echo $e.type
}

#model->sql() → map KScript.Data

The SQL the query runs and its parameters, without running it: { sql, params }.
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="query" value="Task->where(done: false)->order_by(title)->sql()" />
<echo>{ $query.sql }</echo>
<echo>{ $query.params->convert(to: json) }</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 }])
$query = Task->where(done: false)->order_by(title)->sql()
echo $query.sql
echo $query.params->convert(to: json)

#model->sum(column: Self.fields) → number KScript.Data

The total of a column over the rows that match (0 when none do).
ParameterTakes
column requiredSelf.fields
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>{ Task->sum(points) } points, { Task->where(done: false)->sum(points) } left</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 "{ Task->sum(points) } points, { Task->where(done: false)->sum(points) } left"

#model->update(values...: Self) → number KScript.Data

Sets columns on every row that matches, without reading them: ->update(active: false). Returns how many rows changed.
ParameterTakes
valuesSelf, any number of them
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>{ Task->where(done: false)->update(done: true) } changed</echo>
<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 }])
echo "{ Task->where(done: false)->update(done: true) } changed"
echo "{ Task->where(done: true)->count() } done"

#model->update_or_create(match: map, values?: map) → Self KScript.Data

Updates the first record that matches with the values, or makes one from both maps. Returns the record: Product->update_or_create({ sku: $item.sku }, { price: $item.price }).
ParameterTakes
match requiredmap
values optionalmap

Arguments can be named: ->update_or_create(…, values: …).

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="ship" value="Task->update_or_create({ title: 'Ship it' }, { points: 8 })" />
<let name="plan" value="Task->update_or_create({ title: 'Plan' }, { points: 1 })" />
<echo>{ $ship.points } points; { Task->count() } 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 }])
$ship = Task->update_or_create({ title: 'Ship it' }, { points: 8 })
$plan = Task->update_or_create({ title: 'Plan' }, { points: 1 })
echo "{ $ship.points } points; { Task->count() } tasks"

#model->upsert(row: map, key?: string) → number KScript.Data

Atomically inserts a row or updates supplied fields on unique conflict. The key must name a unique column. The returned count is the provider’s affected-row count.
ParameterTakes
row requiredmap
key optionalstring

Arguments can be named: ->upsert(…, key: …).

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>Task->upsert({ title: 'Ship it', points: 8 }, key: 'title'); Task->upsert({ title: 'Plan', points: 1 }, key: 'title')</do>
<echo>{ Task->where(title: 'Ship it')->value(points) } points, { Task->count() } 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 }])
Task->upsert({ title: 'Ship it', points: 8 }, key: 'title')
Task->upsert({ title: 'Plan', points: 1 }, key: 'title')
echo "{ Task->where(title: 'Ship it')->value(points) } points, { Task->count() } tasks"

#model->value(column: Self.fields) → Self[column] KScript.Data

One column of the first row, or null.
ParameterTakes
column requiredSelf.fields
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>{ Task->where(done: true)->value(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 }])
echo Task->where(done: true)->value(title)

#model->when(test, then: fn, otherwise?: fn) → query KScript.Data

Changes the query only when the test is true: ->when($search != null, fn(q) => $q->where(fn(u) => $u.name->contains($search))).
ParameterTakes
test requiredany value
then requiredfn
otherwise optionalfn

Arguments can be named: ->when(…, then: …, otherwise: …).

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="search" value="'docs'" />
<echo>{ Task->when($search != null, fn(q) => $q->where(fn(t) => $t.title->contains($search)))->pluck(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 }])
$search = 'docs'
echo Task->when($search != null, fn(q) => $q->where(fn(t) => $t.title->contains($search)))->pluck(title)

#model->where(conditions...: Self) → query KScript.Data

Keeps the rows that match: a lambda compiled to SQL, fn(u) => $u.age >= 18 and $u.name->starts_with('J'), or names and values, role: admin, team_id: 3 (a list means any of them; null means none).
ParameterTakes
conditionsSelf, any number of them
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>{ Task->where(done: false)->pluck(title) }</echo>
<echo>{ Task->where(fn(t) => $t.points >= 3)->count() }</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 Task->where(done: false)->pluck(title)
echo Task->where(fn(t) => $t.points >= 3)->count()

#model->where_not(conditions...: Self) → query KScript.Data

Leaves out the rows that match.
ParameterTakes
conditionsSelf, any number of them
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>{ Task->where_not(done: true)->pluck(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 }])
echo Task->where_not(done: true)->pluck(title)

#model->with(relations...: Self.fields(relations: only)) → query KScript.Data

Loads relations with the records, one query per relation rather than one per record: ->with(author, tags), ->with(posts.tags), or ->with(posts: fn(q) => $q->where(published: true)) to load only some.
ParameterTakes
relationsSelf.fields(relations: only), any number of them
Example
<import package="KScript.Data" />
<model name="Author" fields="name: string, posts: Post[]" />
<model name="Post" fields="title: string, published: bool = false, author: Author" />
<do>$db->create_table(Author); $db->create_table(Post)</do>
<let name="ada" value="Author->create(name: 'Ada')" />
<do>Post->create(title: 'Notes', published: true, author: $ada); Post->create(title: 'Draft', author: $ada)</do>
<for each="post" in="Post->with(author)->order_by(title)->get()">
  <echo>{ $post.title } by { $post.author.name }</echo>
</for>
import KScript.Data
model Author {
  name: string
  posts: Post[]
}
model Post {
  title: string
  published: bool = false
  author: Author
}
$db->create_table(Author)
$db->create_table(Post)
$ada = Author->create(name: 'Ada')
Post->create(title: 'Notes', published: true, author: $ada)
Post->create(title: 'Draft', author: $ada)
for post in Post->with(author)->order_by(title)->get() {
  echo "{ $post.title } by { $post.author.name }"
}

#model->with_count(relations...: Self.fields(relations: only)) → query KScript.Data

Counts a list relation for each record, as a field named after it: ->with_count(posts) gives each user posts_count. ->with_count(posts: fn(q) => $q->where(published: true)) counts only some.
ParameterTakes
relationsSelf.fields(relations: only), any number of them
Example
<import package="KScript.Data" />
<model name="Author" fields="name: string, posts: Post[]" />
<model name="Post" fields="title: string, published: bool = false, author: Author" />
<do>$db->create_table(Author); $db->create_table(Post)</do>
<let name="ada" value="Author->create(name: 'Ada')" />
<do>Post->create(title: 'Notes', published: true, author: $ada); Post->create(title: 'Draft', author: $ada)</do>
<let name="author" value="Author->with_count(posts)->first()" />
<echo>{ $author.name } has { $author.posts_count } posts</echo>
import KScript.Data
model Author {
  name: string
  posts: Post[]
}
model Post {
  title: string
  published: bool = false
  author: Author
}
$db->create_table(Author)
$db->create_table(Post)
$ada = Author->create(name: 'Ada')
Post->create(title: 'Notes', published: true, author: $ada)
Post->create(title: 'Draft', author: $ada)
$author = Author->with_count(posts)->first()
echo "{ $author.name } has { $author.posts_count } posts"

#model->with_trashed() → query KScript.Data

For models that extend SoftDeletes: keeps the deleted records, which queries leave out by default.
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>Task->where(done: true)->delete()</do>
<echo>{ Task->count() } of { Task->with_trashed()->count() }</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 }])
Task->where(done: true)->delete()
echo "{ Task->count() } of { Task->with_trashed()->count() }"

Methods from any