A migration file, in database/migrations: the steps that change the tables, run in order by kscript db migrate and undone by kscript db rollback. kscript db diff writes them from the models.
Declares a model: a <type> whose records are kept in a database table. User->where(role: admin)->get() reads them, User->create({...}) adds one, and $user->save() writes changes. Its fields are checked by their types and rules on every save.Declares a model: a type whose records are kept in a database table. User->where(role: admin)->get() reads them, User->create({...}) adds one, and $user->save() writes changes. Its fields are checked by their types and rules on every save.
Declares a persisted record. Write model Person(key: id) { id: int }. Model options follow the name and fields go in the body.
Holds statements.Holds statements in { }.
Attribute
Value
Description
namerequired
a new variable's name
The model's name, such as User: User->where(...) queries it, and its records have its fields.
fields
fields
A short model's fields on one line: name: string, email: email. Bigger models list one <field> per line.A short model's fields on one line: name: string, email: email. Bigger models list one name: type per line.
with
a string
The record types, models and mixins this model extends, in order.
mixin
true or false
Declares reusable fields, methods and options for other models. A mixin has no table.
auto_create
true or false
Creates this model's table and indexes on first use if the table is missing.
table
a string
The table its records are kept in. Default: the plural of the name in snake case (User is users, BlogPost blog_posts).
connection
a string
The database connection, from database.connections in your config. Default: the default connection (main).
key
a string
The field that is the key. Default: id, an int the database numbers, added when the model doesn't declare it.
timestamps
true or false
Retired: extend the Timestamps mixin instead. Only KScript.Data's own script, which declares the mixin, sets it.
soft_deletes
true or false
Retired: extend the SoftDeletes mixin instead. Only KScript.Data's own script, which declares the mixin, sets it.
unique
a string
Fields no two records may share: [email], or [email, [team_id, slug]] for a pair that is unique together. Saving a clash fails with UniqueViolation.
index
a string
Fields to index, so queries on them are fast: [team_id, [team_id, created_at]].
hidden
a string
Fields left out of JSON (and so out of route responses): [password].
guarded
a string
Fields ->create($form) and ->update($form) don't set, so a form can't change them: [role]. Set them on the record instead: user.role = 'admin'.
extra
drop|keep|error
Keys ->create() is given that the model doesn't have: drop them (the default), keep them, or make them an error.
description
a string
What the model is, shown on hover.
Option
Value
Description
mixin
true or false
Declares reusable fields, methods and options for other models. A mixin has no table.
auto_create
true or false
Creates this model's table and indexes on first use if the table is missing.
table
a string
The table its records are kept in. Default: the plural of the name in snake case (User is users, BlogPost blog_posts).
connection
a string
The database connection, from database.connections in your config. Default: the default connection (main).
key
a string
The field that is the key. Default: id, an int the database numbers, added when the model doesn't declare it.
timestamps
true or false
Retired: extend the Timestamps mixin instead. Only KScript.Data's own script, which declares the mixin, sets it.
soft_deletes
true or false
Retired: extend the SoftDeletes mixin instead. Only KScript.Data's own script, which declares the mixin, sets it.
unique
a string
Fields no two records may share: [email], or [email, [team_id, slug]] for a pair that is unique together. Saving a clash fails with UniqueViolation.
index
a string
Fields to index, so queries on them are fast: [team_id, [team_id, created_at]].
hidden
a string
Fields left out of JSON (and so out of route responses): [password].
guarded
a string
Fields ->create($form) and ->update($form) don't set, so a form can't change them: [role]. Set them on the record instead: user.role = 'admin'.
extra
drop|keep|error
Keys ->create() is given that the model doesn't have: drop them (the default), keep them, or make them an error.
KScript that runs at its place among the steps, in the migration's transaction: to move rows into a table or column made above it before an old one is dropped. db diff can't see what it does, and rollback needs a Down block to undo it.
Holds statements.Holds statements in { }. Scripts only. Goes inside <KScript.Migration>Migration or <Down>Down.
Runs its statements in a transaction: what they save is kept when the block ends, and undone if anything in it fails (the error then carries on). A transaction inside another is a savepoint, so only its own changes are undone.
Holds statements.Holds statements in { }.
AttributeOption
Value
Description
retries
expression
Tries the block again this many times when the database is busy or a deadlock stops it.
connection
a string
The database connection, from database.connections in your config. Default: the default connection.
A query on a table with no model (@db('users')->where(active: 1)->get() gives maps), or with no table the database itself. connection names one from database.connections in your config.
The SQL statements this run has sent, oldest first (the last 1000), each as { sql, bindings, database }. In a test, only the test's own: expect(@queries(), count_at_most: 4) catches a page that makes a query per row.
Runs SQL. Each in it is worked out and sent as a parameter, never pasted in: @sql("select * from users where email = {$email}"). A query gives its rows as maps (or as: a type); anything else gives how many rows it changed.
Arguments can be named: @sql(…, as: …, connection: …).
The default database connection: $db->table('users'), $db->tables(), $db->create_table(User). With no config it is SQLite in data/app.sqlite in the project folder; database.connections in your config sets others.
Models and mixins
KScript.Data ships these, written in KScript:
Timestamps, a mixin: Adds created_at and updated_at to a model. Both are set when a record is first saved, and updated_at changes every time it is saved again.
SoftDeletes, a mixin: Adds deleted_at to a model: ->delete() sets it instead of removing the row, and queries leave those records out. ->with_trashed() keeps them, ->restore() brings one back and…
Every option a model takes and every built-in mixin: Models and mixins.