KScript docs
EN

Databases: KScript.Data

In the editor

The language server knows every model's fields. It completes them in ->where(, ->order_by(, ->create( and the rest, and relations in ->with(. A field the model doesn't have is reported as KS5013 (see errors). A where lambda's parameter is the model's record, so fn(p) => $p. completes its fields too.

The names in a model's options, such as hidden: [data] or index: [team_id], are the model's fields too. The editor completes them, and you can hover over one or go to its definition. A migration's column names work the same way. Index(columns: [‸]) and Key(columns: [‸]) offer the Fields of the CreateTable around them. A step that names a table, such as AddIndex(table: 'posts', columns: [‸]) or DropField(table: 'posts', name: ‸), offers the columns that table has at that point. Those come from the earlier migrations and the steps above it in the file, or from the model when no migration makes the table yet. A name the table doesn't have is reported as KS5013.

<KScript.Migration>
  <CreateTable name="note_files">
    <Field name="id" type="int" key="yes" auto="yes" />
    <Field name="note_id" type="int" references="board_notes" />
    <Index columns="note_id" />
  </CreateTable>
</KScript.Migration>
import KScript.Data

Migration {
  CreateTable(name: 'note_files') {
    Field(name: 'id', type: int, key, auto)
    Field(name: 'note_id', type: int, references: 'board_notes')
    Index(columns: [note_id])
  }
}

Hover over a query method to see roughly the SQL the chain sends, with ? for the values:

sql
SELECT * FROM posts
WHERE views > ? AND author_id = ?
ORDER BY created_at DESC
LIMIT ?

When the project has migrations, a model that the migrations don't make as it is now is reported as KS5014, with the first change kscript db diff would write, such as add field users.bio.

The migration side is checked too, as you type. A column's type: must be the one its model field needs, and an enum column's values: must be the field's enum's values. With color: NoteColor on BoardNote and enum NoteColor { yellow, pink, blue }, the column must be written like this:

<KScript.Migration>
  <CreateTable name="board_notes">
    <Field name="id" type="int" key="yes" auto="yes" />
    <Field name="color" type="enum" values="yellow, pink, blue" default="'yellow'" />
  </CreateTable>
</KScript.Migration>
import KScript.Data

Migration {
  CreateTable(name: 'board_notes') {
    Field(name: 'id', type: int, key, auto)
    Field(name: 'color', type: enum, values: [yellow, pink, blue], default: 'yellow')
  }
}

A word NoteColor doesn't have, such as values: [yellow, pink, ajcn], is an error at that word (with a fix when it looks like a typo), and the list is told it still needs blue. Once a later migration changes the column again, the earlier one is left alone: only the migration that last writes a column has to match the model.

An enum column can also take its words from the enum itself, so the list is never copied by hand. Name the enum in values:, imported like any other type, and default: offers and checks its words:

<KScript.Migration>
  <import path="../../types/note-color.kscript" as="colours" />

  <CreateTable name="board_notes">
    <Field name="id" type="int" key="yes" auto="yes" />
    <Field name="color" type="enum" values="colours.NoteColor" default="'yellow'" />
  </CreateTable>
</KScript.Migration>
import KScript.Data
import "../../types/note-color.kscript" as colours

Migration {
  CreateTable(name: 'board_notes') {
    Field(name: 'id', type: int, key, auto)
    Field(name: 'color', type: enum, values: colours.NoteColor, default: 'yellow')
  }
}

The migration keeps the words the enum has when it runs. A word added to the enum later still needs a new migration (kscript db diff writes it), since a migration that has run isn't run again. The default: must be one of the enum's words: if the enum drops that word, the editor marks the default and the migration stops with an error instead of making the column.