record 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 .
# record ->decrement (field: Self.fields , by?: number ) → record KScript.Data Takes away from a number field in the database in one statement and sets the record's field to the new value (1 by default). Returns the record.
Parameter Takes field 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>
<let name="task" value="Task->find(1)" />
<do>$task->decrement(points)</do>
<echo>{ $task.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 = Task->find(1)
$task->decrement(points)
echo $task.pointsDeletes the record. For a model that extends SoftDeletes it is only marked deleted (->restore() brings it back). True when it was deleted.
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->find(2)->delete()</do>
<echo>{ Task->count() } left, { Task->with_trashed()->count() } 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 }])
Task->find(2)->delete()
echo "{ Task->count() } left, { Task->with_trashed()->count() } in the table"The names of the fields with changes that aren't saved.
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->find(1)" />
<do>$task.points = 4; $task.done = true</do>
<echo>{ $task->dirty() }</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->find(1)
$task.points = 4
$task.done = true
echo $task->dirty()Deletes the record from the table, even for a model that extends SoftDeletes.
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->find(2)->force_delete()</do>
<echo>{ Task->with_trashed()->count() } 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 }])
Task->find(2)->force_delete()
echo "{ Task->with_trashed()->count() } in the table"The record as the database has it now (a new record), or null if it was deleted.
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->find(1)" />
<do>Task->where(title: 'Write docs')->update(points: 9)</do>
<echo>{ $task.points }, then { $task->fresh().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 = Task->find(1)
Task->where(title: 'Write docs')->update(points: 9)
echo "{ $task.points }, then { $task->fresh().points }"# record ->increment (field: Self.fields , by?: number ) → record KScript.Data Adds to a number field in the database in one statement, so two requests at once both count, and sets the record's field to the new value (1 by default): $post->increment(views). Returns the record.
Parameter Takes field 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>
<let name="task" value="Task->find(1)" />
<do>$task->increment(points, by: 2)</do>
<echo>{ $task.points } { Task->find(1).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 = Task->find(1)
$task->increment(points, by: 2)
echo "{ $task.points } { Task->find(1).points }"True when the other record is the same row: a record of the same model with the same key, or that key itself. $post.author->is(@user()).
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->find(1)" />
<echo>{ $task->is(Task->first()) } { $task->is(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 }])
$task = Task->find(1)
echo "{ $task->is(Task->first()) } { $task->is(2) }"# record ->is_dirty (field?: Self.fields ) → bool KScript.Data True when the record has changes that aren't saved: any field, or the one named.
Parameter Takes field 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>
<let name="task" value="Task->find(1)" />
<do>$task.points = 4</do>
<echo>{ $task->is_dirty() } { $task->is_dirty('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 = Task->find(1)
$task.points = 4
echo "{ $task->is_dirty() } { $task->is_dirty('title') }"True when the record is in the database: it was read from it or has been saved.
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->find(1)" />
<echo>{ $task->is_saved() }</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->find(1)
echo $task->is_saved()Loads relations onto the record now (again, if they were loaded): $post->load(author, tags). Returns the record.
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="Post->first()" />
<do>$post->load(author)</do>
<echo>{ $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 = Post->first()
$post->load(author)
echo $post.author.name# record ->replicate (except?: Self.fields[] ) → record KScript.Data A copy of the record that isn't saved yet, without its key and timestamps (and the fields named in except): $post->replicate(except: [slug]). Call ->save() to keep it.
Parameter Takes except 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>
<let name="copy" value="Task->find(1)->replicate(except: [title])" />
<do>$copy.title = 'Write more docs'</do>
<do>$copy->save()</do>
<echo>{ $copy.id }: { $copy.title }, { $copy.points } 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 }])
$copy = Task->find(1)->replicate(except: [title])
$copy.title = 'Write more docs'
$copy->save()
echo "{ $copy.id }: { $copy.title }, { $copy.points } points"Brings back a soft-deleted record. Returns the record.
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->find(2)" />
<do>$task->delete()</do>
<do>$task->restore()</do>
<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 }])
$task = Task->find(2)
$task->delete()
$task->restore()
echo "{ Task->count() } tasks"Saves the record: adds it when it is new, otherwise writes the fields that changed (and updated_at). Returns the record.
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->find(1)" />
<do>$task.done = true</do>
<do>$task->save()</do>
<echo>{ Task->find(1).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 }])
$task = Task->find(1)
$task.done = true
$task->save()
echo Task->find(1).doneSets updated_at to now and saves, for a model that extends Timestamps: a parent whose list changed shows it was updated. Returns the record.
Example
.kscript XML Copy
<import package="KScript.Data" />
<model name="Note" with="Timestamps" fields="text: string" />
<do>$db->create_table(Note)</do>
<let name="note" value="Note->create(text: 'Hi')" />
<do>$note->touch()</do>
<echo>{ $note.updated_at >= $note.created_at }</echo>.kscript XML Copy
import KScript.Data
model Note extends Timestamps {
text: string
}
$db->create_table(Note)
$note = Note->create(text: 'Hi')
$note->touch()
echo $note.updated_at >= $note.created_atSets fields and saves: $user->update(name: 'Joan') or ->update($form). Each value is checked by its field's type; guarded fields are left out. Returns the record.
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>
<let name="task" value="Task->find(1)" />
<do>$task->update(points: 8)</do>
<echo>{ Task->find(1).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 = Task->find(1)
$task->update(points: 8)
echo Task->find(1).pointsMethods from any Methods from map ->all(fn(value, key?))->any(fn(value, key?))->count()->deep_merge(other: map)->entries()->filter(fn(value, key?))->find(fn(value, key?))->get(key, default?)->has(key)->is_empty()->keys()->map(fn(value, key?))->map_keys(fn(key, value?))->merge(other)->omit(keys: Self.fields[])->pick(keys: Self.fields[])->reduce(fn(total, value, key?), initial?)->reject(fn(value, key?))->remove(key)->set(key, value)->sort(by: fn(value, key?), order?: asc|desc)->values()