query 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 .
Links records through a many to many relation's pivot table: $post->tags()->attach([$a, $b]) or keys, ->attach([1, 2]). Links already there are kept. Returns how many were added.
Example
.kscript XML Copy
<import package="KScript.Data" />
<model name="Post" fields="title: string, tags: Tag[]" />
<model name="Tag" fields="name: string, posts: Post[]" />
<do>$db->create_table(Post); $db->create_table(Tag)</do>
<let name="post" value="Post->create(title: 'Hello')" />
<let name="news" value="Tag->create(name: 'news')" />
<let name="tips" value="Tag->create(name: 'tips')" />
<echo>{ $post->tags()->attach([$news, $tips]) } linked</echo>
<echo>{ $post->tags()->order_by(name)->pluck(name) }</echo>.kscript XML Copy
import KScript.Data
model Post {
title: string
tags: Tag[]
}
model Tag {
name: string
posts: Post[]
}
$db->create_table(Post)
$db->create_table(Tag)
$post = Post->create(title: 'Hello')
$news = Tag->create(name: 'news')
$tips = Tag->create(name: 'tips')
echo "{ $post->tags()->attach([$news, $tips]) } linked"
echo $post->tags()->order_by(name)->pluck(name)# query ->average (column: Self.fields ) → number KScript.Data The average of a column, or null when no rows match.
Parameter Takes column required Self.fields
Example
.kscript XML Copy
<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>.kscript XML Copy
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)# query ->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.
Parameter Takes size required number work required fn
Arguments can be named: ->chunk(…, work: …).
Example
.kscript XML Copy
<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>.kscript XML Copy
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)How many rows match.
Example
.kscript XML Copy
<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>.kscript XML Copy
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"# query ->create (values...: Self ) → Self KScript.Data Adds a record to the relation: $user->posts()->create(title: 'Hi') sets the post's author to the user. For a many to many relation it saves the record and links it.
Parameter Takes valuesSelf , any number of them
Example
.kscript XML Copy
<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="post" value="$ada->posts()->create(title: 'Hello')" />
<echo>{ $post.title } by { $post.author.name }</echo>.kscript XML Copy
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)
$post = $ada->posts()->create(title: 'Hello')
echo "{ $post.title } by { $post.author.name }"# query ->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.
Parameter Takes per_page optional number after optional any value key optional string descending optional a flag: descending or not descending
Arguments can be named: ->cursor_paginate(…, after: …, key: …).
Example
.kscript XML Copy
<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>.kscript XML Copy
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 }"# query ->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.
Parameter Takes column required Self.fields by optional number
Arguments can be named: ->decrement(…, by: …).
Example
.kscript XML Copy
<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>.kscript XML Copy
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)Deletes every row that matches: models that extend SoftDeletes only mark them deleted. Returns how many.
Example
.kscript XML Copy
<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>.kscript XML Copy
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"Unlinks records from a many to many relation, or every record when none are given. Returns how many links were removed.
Example
.kscript XML Copy
<import package="KScript.Data" />
<model name="Post" fields="title: string, tags: Tag[]" />
<model name="Tag" fields="name: string, posts: Post[]" />
<do>$db->create_table(Post); $db->create_table(Tag)</do>
<let name="post" value="Post->create(title: 'Hello')" />
<let name="news" value="Tag->create(name: 'news')" />
<let name="tips" value="Tag->create(name: 'tips')" />
<do>$post->tags()->attach([$news, $tips])</do>
<echo>{ $post->tags()->detach($news) } unlinked</echo>
<echo>{ $post->tags()->pluck(name) }</echo>.kscript XML Copy
import KScript.Data
model Post {
title: string
tags: Tag[]
}
model Tag {
name: string
posts: Post[]
}
$db->create_table(Post)
$db->create_table(Tag)
$post = Post->create(title: 'Hello')
$news = Tag->create(name: 'news')
$tips = Tag->create(name: 'tips')
$post->tags()->attach([$news, $tips])
echo "{ $post->tags()->detach($news) } unlinked"
echo $post->tags()->pluck(name)Leaves out rows that repeat an earlier one, in the columns selected.
Example
.kscript XML Copy
<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>.kscript XML Copy
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()True when any row matches.
Example
.kscript XML Copy
<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>.kscript XML Copy
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() }"The record with this key, or null.
Example
.kscript XML Copy
<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>.kscript XML Copy
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'# query ->find_or_fail (id ) → Self KScript.Data The record with this key, or a ModelNotFound error (a 404 in a route).
Example
.kscript XML Copy
<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>.kscript XML Copy
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 }"
}The first row, or null when none match.
Example
.kscript XML Copy
<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>.kscript XML Copy
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'# query ->first_or_fail () → Self KScript.Data The first row, or a ModelNotFound error (a 404 in a route) when none match.
Example
.kscript XML Copy
<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>.kscript XML Copy
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
}Deletes every row that matches, even for models that extend SoftDeletes. Returns how many.
Example
.kscript XML Copy
<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>.kscript XML Copy
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"Runs the query: a list of records (maps, for a table with no model).
Example
.kscript XML Copy
<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>.kscript XML Copy
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 }"
}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.
Parameter Takes columnsSelf.fields , any number of them
Example
.kscript XML Copy
<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>.kscript XML Copy
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()Reads the rows in a random order, a new one each time: Quote->in_random_order()->first().
Example
.kscript XML Copy
<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>.kscript XML Copy
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"# query ->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.
Parameter Takes column required Self.fields by optional number
Arguments can be named: ->increment(…, by: …).
Example
.kscript XML Copy
<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>.kscript XML Copy
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)Adds rows: a map, or a list of maps. On a model each one is checked like ->create(). Returns how many were added.
Example
.kscript XML Copy
<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>.kscript XML Copy
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"# query ->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.
Parameter Takes column optional Self.fields
Example
.kscript XML Copy
<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>.kscript XML Copy
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)Reads at most this many rows.
Parameter Takes count required number
Example
.kscript XML Copy
<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>.kscript XML Copy
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)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
.kscript XML Copy
<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>.kscript XML Copy
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"# query ->max (column: Self.fields ) → Self[column] KScript.Data The largest value of a column, or null when no rows match.
Parameter Takes column required Self.fields
Example
.kscript XML Copy
<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>.kscript XML Copy
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)# query ->min (column: Self.fields ) → Self[column] KScript.Data The smallest value of a column, or null when no rows match.
Parameter Takes column required Self.fields
Example
.kscript XML Copy
<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>.kscript XML Copy
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)Skips this many rows first.
Parameter Takes count required number
Example
.kscript XML Copy
<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>.kscript XML Copy
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)# query ->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.
Parameter Takes column optional Self.fields
Example
.kscript XML Copy
<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>.kscript XML Copy
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)For models that extend SoftDeletes: reads only the deleted records.
Example
.kscript XML Copy
<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>.kscript XML Copy
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)# query ->or_where (conditions...: Self ) → query KScript.Data Like ->where(), joined with or: rows that match the conditions so far, or these.
Parameter Takes conditionsSelf , any number of them
Example
.kscript XML Copy
<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>.kscript XML Copy
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)# query ->order_by (columns...: Self.fields ) → query KScript.Data Sorts the rows: ->order_by(name), ->order_by(created_at: desc, name: asc).
Parameter Takes columnsSelf.fields , any number of them
Example
.kscript XML Copy
<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>.kscript XML Copy
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)# query ->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.
Parameter Takes per_page optional number page optional any value
Arguments can be named: ->paginate(…, page: …).
Example
.kscript XML Copy
<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>.kscript XML Copy
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"# query ->pluck (column: Self.fields ) → Self[column][] KScript.Data One column of every row that matches, as a list: User->pluck(email).
Parameter Takes column required Self.fields
Example
.kscript XML Copy
<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>.kscript XML Copy
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)For models that extend SoftDeletes: brings back the deleted records that match. Returns how many.
Example
.kscript XML Copy
<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>.kscript XML Copy
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"# query ->select (columns...: Self.fields ) → query KScript.Data Reads only these columns: ->select(id, name). Records get null for the others.
Parameter Takes columnsSelf.fields , any number of them
Example
.kscript XML Copy
<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>.kscript XML Copy
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' }"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
.kscript XML Copy
<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>.kscript XML Copy
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
}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
.kscript XML Copy
<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>.kscript XML Copy
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
}The SQL the query runs and its parameters, without running it: { sql, params }.
Example
.kscript XML Copy
<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>.kscript XML Copy
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)The total of a column over the rows that match (0 when none do).
Parameter Takes column required Self.fields
Example
.kscript XML Copy
<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>.kscript XML Copy
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"Links exactly these records through a many to many relation, unlinking the rest: $post->tags()->sync($form.tag_ids). Returns { attached, detached }, the keys added and removed.
Example
.kscript XML Copy
<import package="KScript.Data" />
<model name="Post" fields="title: string, tags: Tag[]" />
<model name="Tag" fields="name: string, posts: Post[]" />
<do>$db->create_table(Post); $db->create_table(Tag)</do>
<let name="post" value="Post->create(title: 'Hello')" />
<let name="news" value="Tag->create(name: 'news')" />
<let name="tips" value="Tag->create(name: 'tips')" />
<do>$post->tags()->attach($news)</do>
<let name="changes" value="$post->tags()->sync([$tips])" />
<echo>linked { $changes.attached }, unlinked { $changes.detached }</echo>
<echo>{ $post->tags()->pluck(name) }</echo>.kscript XML Copy
import KScript.Data
model Post {
title: string
tags: Tag[]
}
model Tag {
name: string
posts: Post[]
}
$db->create_table(Post)
$db->create_table(Tag)
$post = Post->create(title: 'Hello')
$news = Tag->create(name: 'news')
$tips = Tag->create(name: 'tips')
$post->tags()->attach($news)
$changes = $post->tags()->sync([$tips])
echo "linked { $changes.attached }, unlinked { $changes.detached }"
echo $post->tags()->pluck(name)Links the records given that aren't linked and unlinks the ones that are. Returns { attached, detached }.
Example
.kscript XML Copy
<import package="KScript.Data" />
<model name="Post" fields="title: string, tags: Tag[]" />
<model name="Tag" fields="name: string, posts: Post[]" />
<do>$db->create_table(Post); $db->create_table(Tag)</do>
<let name="post" value="Post->create(title: 'Hello')" />
<let name="news" value="Tag->create(name: 'news')" />
<let name="tips" value="Tag->create(name: 'tips')" />
<do>$post->tags()->attach($news)</do>
<do>$post->tags()->toggle([$news, $tips])</do>
<echo>{ $post->tags()->pluck(name) }</echo>.kscript XML Copy
import KScript.Data
model Post {
title: string
tags: Tag[]
}
model Tag {
name: string
posts: Post[]
}
$db->create_table(Post)
$db->create_table(Tag)
$post = Post->create(title: 'Hello')
$news = Tag->create(name: 'news')
$tips = Tag->create(name: 'tips')
$post->tags()->attach($news)
$post->tags()->toggle([$news, $tips])
echo $post->tags()->pluck(name)Sets columns on every row that matches, without reading them: ->update(active: false). Returns how many rows changed.
Parameter Takes valuesSelf , any number of them
Example
.kscript XML Copy
<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>.kscript XML Copy
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"# query ->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.
Parameter Takes row required map key optional string
Arguments can be named: ->upsert(…, key: …).
Example
.kscript XML Copy
<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>.kscript XML Copy
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"# query ->value (column: Self.fields ) → Self[column] KScript.Data One column of the first row, or null.
Parameter Takes column required Self.fields
Example
.kscript XML Copy
<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>.kscript XML Copy
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)# query ->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))).
Parameter Takes test required any value then required fn otherwise optional fn
Arguments can be named: ->when(…, then: …, otherwise: …).
Example
.kscript XML Copy
<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>.kscript XML Copy
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)# query ->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).
Parameter Takes conditionsSelf , any number of them
Example
.kscript XML Copy
<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>.kscript XML Copy
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()# query ->where_not (conditions...: Self ) → query KScript.Data Leaves out the rows that match.
Parameter Takes conditionsSelf , any number of them
Example
.kscript XML Copy
<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>.kscript XML Copy
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)# query ->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.
Parameter Takes relationsSelf.fields(relations: only) , any number of them
Example
.kscript XML Copy
<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>.kscript XML Copy
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 }"
}# query ->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.
Parameter Takes relationsSelf.fields(relations: only) , any number of them
Example
.kscript XML Copy
<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>.kscript XML Copy
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"For models that extend SoftDeletes: keeps the deleted records, which queries leave out by default.
Example
.kscript XML Copy
<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>.kscript XML Copy
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