KScript docs
EN

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.
ParameterTakes
field 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>
<let name="task" value="Task->find(1)" />
<do>$task->decrement(points)</do>
<echo>{ $task.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 = Task->find(1)
$task->decrement(points)
echo $task.points

#record->delete() → bool KScript.Data

Deletes the record. For a model that extends SoftDeletes it is only marked deleted (->restore() brings it back). True when it was deleted.
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->find(2)->delete()</do>
<echo>{ Task->count() } left, { Task->with_trashed()->count() } 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 }])
Task->find(2)->delete()
echo "{ Task->count() } left, { Task->with_trashed()->count() } in the table"

#record->dirty() → list KScript.Data

The names of the fields with changes that aren't saved.
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->find(1)" />
<do>$task.points = 4; $task.done = true</do>
<echo>{ $task->dirty() }</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->find(1)
$task.points = 4
$task.done = true
echo $task->dirty()

#record->force_delete() → bool KScript.Data

Deletes the record from the table, even for a model that extends SoftDeletes.
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->find(2)->force_delete()</do>
<echo>{ Task->with_trashed()->count() } 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 }])
Task->find(2)->force_delete()
echo "{ Task->with_trashed()->count() } in the table"

#record->fresh() → record KScript.Data

The record as the database has it now (a new record), or null if it was deleted.
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->find(1)" />
<do>Task->where(title: 'Write docs')->update(points: 9)</do>
<echo>{ $task.points }, then { $task->fresh().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 = 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.
ParameterTakes
field 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>
<let name="task" value="Task->find(1)" />
<do>$task->increment(points, by: 2)</do>
<echo>{ $task.points } { Task->find(1).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 = Task->find(1)
$task->increment(points, by: 2)
echo "{ $task.points } { Task->find(1).points }"

#record->is(other) → bool KScript.Data

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
<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>
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.
ParameterTakes
field 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>
<let name="task" value="Task->find(1)" />
<do>$task.points = 4</do>
<echo>{ $task->is_dirty() } { $task->is_dirty('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 = Task->find(1)
$task.points = 4
echo "{ $task->is_dirty() } { $task->is_dirty('title') }"

#record->is_saved() → bool KScript.Data

True when the record is in the database: it was read from it or has been saved.
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->find(1)" />
<echo>{ $task->is_saved() }</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->find(1)
echo $task->is_saved()

#record->load(relations...) → record KScript.Data

Loads relations onto the record now (again, if they were loaded): $post->load(author, tags). Returns the record.
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="post" value="Post->first()" />
<do>$post->load(author)</do>
<echo>{ $post.author.name }</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)
$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.
ParameterTakes
except 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>
<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>
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"

#record->restore() → record KScript.Data

Brings back a soft-deleted record. Returns the record.
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->find(2)" />
<do>$task->delete()</do>
<do>$task->restore()</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 }])
$task = Task->find(2)
$task->delete()
$task->restore()
echo "{ Task->count() } tasks"

#record->save() → record KScript.Data

Saves the record: adds it when it is new, otherwise writes the fields that changed (and updated_at). Returns the record.
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->find(1)" />
<do>$task.done = true</do>
<do>$task->save()</do>
<echo>{ Task->find(1).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 }])
$task = Task->find(1)
$task.done = true
$task->save()
echo Task->find(1).done

#record->touch() → record KScript.Data

Sets 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
<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>
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_at

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

Sets 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.
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>
<let name="task" value="Task->find(1)" />
<do>$task->update(points: 8)</do>
<echo>{ Task->find(1).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 = Task->find(1)
$task->update(points: 8)
echo Task->find(1).points

Methods from any

Methods from map