database 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).
Fields Field Type Description .driverstring The kind of database: sqlite, postgres, mysql or sqlserver. .filestring The database file, for SQLite, or null. .is_in_transactionbool True while a transaction is open on this connection. .namestring The connection's name in kscript.json.
From KScript.Data These need <import package="KScript.Data" />import KScript.Data . See KScript.Data .
# database ->create_table (model: model ) → bool KScript.Data Makes a model's table and indexes, if the database doesn't have them yet, and the pivot tables of its many to many relations whose other table is there. Make the tables a model links to first. Good for scripts and tests; projects use migrations.
Parameter Takes model required model
Example
.kscript XML Copy
<import package="KScript.Data" />
<model name="Note" fields="text: string" />
<echo>{ $db->create_table(Note) } { $db->create_table(Note) }</echo>.kscript XML Copy
import KScript.Data
model Note {
text: string
}
echo "{ $db->create_table(Note) } { $db->create_table(Note) }"# database ->describe_table (table: model|string ) → map KScript.Data Returns columns, indexes, and keys for the named table or model.
Parameter Takes table required model|string
Example
.kscript XML Copy
<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="table" value="$db->describe_table(Task)" />
<echo>{ $table.columns->map(fn(c) => $c.name) }</echo>.kscript XML Copy
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 }])
$table = $db->describe_table(Task)
echo $table.columns->map(fn(c) => $c.name)Deletes a table and everything in it, if it is there: a name, or a model's table. True when there was one.
Example
.kscript XML Copy
<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>
<echo>{ $db->drop_table(Task) } { $db->has_table('tasks') }</echo>.kscript XML Copy
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 }])
echo "{ $db->drop_table(Task) } { $db->has_table('tasks') }"# database ->has_table (name: model|string ) → bool KScript.Data True when the database has a model's table or a table with the given name: $db->has_table(User) or $db->has_table('users').
Parameter Takes name required model|string
Example
.kscript XML Copy
<import package="KScript.Data" />
<model name="Note" fields="text: string" />
<echo>{ $db->has_table(Note) }</echo>
<do>$db->create_table(Note)</do>
<echo>{ $db->has_table('notes') }</echo>.kscript XML Copy
import KScript.Data
model Note {
text: string
}
echo $db->has_table(Note)
$db->create_table(Note)
echo $db->has_table('notes')# database ->sql (text , as? ) → any KScript.Data Runs SQL, like @sql, on this connection.
Arguments can be named: ->sql(…, as: …).
Example
.kscript XML Copy
<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="rows" value="$db->sql('select title from tasks order by points')" />
<echo>{ $rows->map(fn(r) => $r.title) }</echo>.kscript XML Copy
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 }])
$rows = $db->sql('select title from tasks order by points')
echo $rows->map(fn(r) => $r.title)A query on a table with no model: rows come back as maps.
Example
.kscript XML Copy
<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>
<echo>{ $db->table('tasks')->where(done: false)->count() } rows</echo>.kscript XML Copy
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 }])
echo "{ $db->table('tasks')->where(done: false)->count() } rows"The names of the tables in the database.
Example
.kscript XML Copy
<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>
<echo>{ $db->tables() }</echo>.kscript XML Copy
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 }])
echo $db->tables()# database ->transaction (work: fn , retries?: number ) → any KScript.Data Runs work in a transaction on this connection and returns what it returns: everything it saves is kept together, or (when it fails) not at all. retries tries again when the database is busy.
Parameter Takes work required fn retries optional number
Arguments can be named: ->transaction(…, retries: …).
Example
.kscript XML Copy
<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>$db->transaction(fn() => Task->where(done: false)->update(done: true))</do>
<echo>{ Task->where(done: true)->count() } done</echo>.kscript XML Copy
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 }])
$db->transaction(fn() => Task->where(done: false)->update(done: true))
echo "{ Task->where(done: true)->count() } done"Methods from any