Models: options and built-in mixins
Every mixin that comes with a package, such as Timestamps, SoftDeletes and Ownable, every option a model takes, and the tables KScript keeps. How models work is in Databases: KScript.Data.
Built-in models and mixins
These come with a package, written in KScript, and you extend them rather than declare them. Import the package, then extend a mixin to give a model its fields and methods: model Note extends Ownable { ... }. Everything every model gets, such as ->where() and ->save(), is under model methods and record methods.
User model in KScript.Auth
Someone with an account. KScript.Auth keeps the users in this model when kscript.json says "http": { "auth": { "users": "User" } }: @register makes one, and @user() is the one signed in. It comes with KScript.Auth; kscript publish User copies it into the project's models folder, where its fields and its key can be changed.
<import package="KScript.Data" />
<import package="KScript.Auth" />
<let name="rows" value="User->all()" />import KScript.Data
import KScript.Auth
rows = User->all()Options: auto_create, unique: [email], hidden: [password], with: Timestamps.
| Field | Type | What it holds |
|---|---|---|
name | string | What they're called. |
email | email | The address they sign in with. |
email_verified_at? | date | When they proved the address is theirs (@verify_email), or null. |
password | password | Their password, kept as a hash. |
Guest model in KScript.Auth
Someone using the app without an account. Each browser gets one, kept in a long-lived cookie, and it is stored the first time it owns a record. Its key is what @guest() gives. It comes with KScript.Auth; kscript publish Guest copies it into the project's models folder, where its key and fields can be changed.
<import package="KScript.Data" />
<import package="KScript.Auth" />
<let name="rows" value="Guest->all()" />import KScript.Data
import KScript.Auth
rows = Guest->all()Options: auto_create, with: Timestamps.
| Field | Type | What it holds |
|---|---|---|
id | uuid | The guest's id, made when the browser is first given one. |
Ownable mixin in KScript.Auth
Rows that belong to one owner: the signed-in user, or the browser's guest. A model that extends Ownable gets a user and a guest, one of them filled in from who is making a record and never changed after, the mine() scope and ->is_mine(). It comes with KScript.Auth; kscript publish Ownable copies it into the project's models folder, where it can be changed.
<import package="KScript.Data" />
<import package="KScript.Auth" />
<model name="Note" with="Ownable">
<field name="text" type="string" />
</model>import KScript.Data
import KScript.Auth
model Note extends Ownable {
text: string
}Options: guarded: [user, guest].
| Field | Type | What it holds |
|---|---|---|
user? | User(on_delete: cascade) | The user the record belongs to, or null when a guest made it. Deleting the user deletes their records. |
guest? | Guest(on_delete: cascade) | The guest the record belongs to, or null when a signed-in user made it. |
| Method | What it does |
|---|---|
Note->mine(as: any = null) | A scope. Keeps the current owner's rows. Outside a request (a test, job or script), name the user: ->mine(as: $user). |
$record->is_mine() | True when the record belongs to whoever is making the request. |
Timestamps mixin in KScript.Data
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.
<import package="KScript.Data" />
<model name="Note" with="Timestamps">
<field name="text" type="string" />
</model>import KScript.Data
model Note extends Timestamps {
text: string
}SoftDeletes mixin in KScript.Data
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 ->force_delete() removes it for good.
<import package="KScript.Data" />
<model name="Note" with="SoftDeletes">
<field name="text" type="string" />
</model>import KScript.Data
model Note extends SoftDeletes {
text: string
}Options
Settings such as a model's table, key and unique fields are options in brackets after its name. An option that is on by itself is a bare word, and options combine: model Task(auto_create, unique: [title]) extends Timestamps { ... }. A mixin may set options too, and a model that extends it gets them.
fields
A short model's fields on one line: name: string, email: email. Bigger models list one <field> per line.
import KScript.Data
model Tag(name: string, colour: string)with
The record types, models and mixins this model extends, in order.
<import package="KScript.Data" />
<model name="Note" with="[Timestamps, SoftDeletes]">
<field name="text" type="string" />
</model>import KScript.Data
model Note extends [Timestamps, SoftDeletes] {
text: string
}mixin
Declares reusable fields, methods and options for other models. A mixin has no table.
<import package="KScript.Data" />
<model name="Titled" mixin="yes">
<field name="title" type="string(max: 200)" />
</model>
<model name="Post" with="Titled">
<field name="body" type="string" />
</model>import KScript.Data
model Titled(mixin) {
title: string(max: 200)
}
model Post extends Titled {
body: string
}auto_create
Creates this model's table and indexes on first use if the table is missing.
<import package="KScript.Data" />
<model name="Visit" auto_create="yes">
<field name="path" type="string" />
</model>import KScript.Data
model Visit(auto_create) {
path: string
}table
The table its records are kept in. Default: the plural of the name in snake case (User is users, BlogPost blog_posts).
<import package="KScript.Data" />
<model name="Person" table="people">
<field name="name" type="string" />
</model>import KScript.Data
model Person(table: 'people') {
name: string
}connection
The database connection, from database.connections in your config. Default: the default connection (main).
<import package="KScript.Data" />
<model name="Report" connection="reports">
<field name="title" type="string" />
</model>import KScript.Data
model Report(connection: 'reports') {
title: string
}key
The field that is the key. Default: id, an int the database numbers, added when the model doesn't declare it.
<import package="KScript.Data" />
<model name="Country" key="code">
<field name="code" type="string(max: 2)" />
<field name="name" type="string" />
</model>import KScript.Data
model Country(key: 'code') {
code: string(max: 2)
name: string
}unique
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.
<import package="KScript.Data" />
<model name="Member" unique="[email, [team_id, handle]]">
<field name="email" type="email" />
<field name="team_id" type="int" />
<field name="handle" type="string" />
</model>import KScript.Data
model Member(unique: [email, [team_id, handle]]) {
email: email
team_id: int
handle: string
}index
Fields to index, so queries on them are fast: [team_id, [team_id, created_at]].
<import package="KScript.Data" />
<model name="Order" index="[customer, [status, created_at]]" with="Timestamps">
<field name="customer" type="string" />
<field name="status" type="string" />
</model>import KScript.Data
model Order(index: [customer, [status, created_at]]) extends Timestamps {
customer: string
status: string
}hidden
Fields left out of JSON (and so out of route responses): [password].
<import package="KScript.Data" />
<model name="Account" hidden="[password]">
<field name="email" type="email" />
<field name="password" type="password" />
</model>import KScript.Data
model Account(hidden: [password]) {
email: email
password: password
}guarded
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'.
<import package="KScript.Data" />
<model name="Member" guarded="[role]">
<field name="email" type="email" />
<field name="role" type="string" default="'member'" />
</model>import KScript.Data
model Member(guarded: [role]) {
email: email
role: string = 'member'
}extra
Keys ->create() is given that the model doesn't have: drop them (the default), keep them, or make them an error.
<import package="KScript.Data" />
<model name="Event" extra="keep">
<field name="name" type="string" />
</model>import KScript.Data
model Event(extra: keep) {
name: string
}Tables KScript keeps
KScript makes these tables in your database itself, the first time it needs them. They aren't models, so you don't query them with ->where(): the commands and guides linked below read them for you. Give your own tables other names.
| Table | What it holds |
|---|---|
kscript_migrations | Every migration that has run, with the batch it ran in. kscript db migrate and kscript db rollback read and write it. More. |
kscript_jobs | Jobs waiting to run, and running, with the database queue driver. The connection's tables setting renames the queue's tables, and kscript publish queue writes their migration. More. |
kscript_failed_jobs | Jobs that failed every try, with the database queue driver. kscript queue failed lists them. More. |
kscript_job_locks | The keys of unique jobs, with the database queue driver. More. |
KScript.Auth keeps its users, sessions and tokens as files in the app's folders, and passkeys in a database file of its own in the _passkeys folder, so none of them are in your database unless you name a model of your own for the users: see Users in a model.