KScript docs
EN

Databases: KScript.Data

Introduction

KScript.Data lets you keep records in a database and read them back without writing SQL. You describe each table as a model: a type whose records are stored as rows. KScript.Data checks every value against its field's type before it reaches the database, and turns your queries into SQL for you.

Your first model needs nothing set up. SQLite ships with KScript, and the database is the file data/app.sqlite in your project folder (the folder with kscript.json, or the script's own folder). When you're ready for Postgres, MySQL or MariaDB, you only change a URL: see Connections.

To get started, import the package and declare a model:

<import package="KScript.Data" />

<model name="Note">
  <field name="text" type="string" />
</model>

<do>$db->create_table(Note)</do>

<do>Note->create(text: 'Buy milk')</do>
<echo value="Note->count()" />
import KScript.Data

model Note {
  text: string
}

$db->create_table(Note)

Note->create(text: 'Buy milk')
echo Note->count()

$db->create_table() makes the table, which is all a script needs. A project whose tables already hold data changes them with migrations instead.