Core\Db\Schema::toArray
Returns the schema as the array that describes it.
Core\Db\Schema::toArray gives you back the array form of a Core\Db\Schema. It is the same shape Core\Db\Schema::fromArray reads, so a schema can go from a value to an array and back as often as you like. Core\Json::encode turns that array into the text nvs schema dump writes.
The array you get is normalized. The tables are in name order, and every key a description left out is written down: a column always has null and identity, and a table always has primary_key, unique and indexes. This means one schema has exactly one array form. Two programs that describe the same tables in different words get the same array from this method, so you can compare two schemas by comparing their arrays.
The array is the value the schema already holds, so calling this method twice costs nothing the second time. Writing into the array you got back does not change the schema.
The examples below read the tables a schema describes, show that two descriptions of one schema give one array, and check that a schema file stored next to a program is still up to date.
Return value
Returns array<mixed>. The array that fromArray reads back as the same schema. Tables are in name order. Columns are in the order they were declared, because create table keeps that order. Constraints and indexes are in name order, because the order they were added in has no effect.
Examples
Read the tables a schema describes
<?nvs// Shows how to read what a schema says. `toArray` returns the schema as an// array, so a program can walk the tables and columns it describes.
use Core\Db\Schema;
var $schema = Schema::fromArray([ 'tables' => [ [ 'name' => 'notes', 'columns' => [ ['name' => 'id', 'type' => 'int64', 'identity' => true], ['name' => 'title', 'type' => 'text(200)'], ['name' => 'body', 'type' => 'text', 'null' => true], ], 'primary_key' => ['id'], ], [ 'name' => 'guests', 'columns' => [ ['name' => 'id', 'type' => 'int64', 'identity' => true], ['name' => 'email', 'type' => 'text(320)'], ], 'primary_key' => ['id'], ], ],]);
array<mixed> $form = $schema->toArray();array<mixed> $tables = $form['tables'] as array<mixed>;
// The tables come in name order, so `guests` is printed first. This prints// `guests: 2 columns` and then `notes: 3 columns`.foreach ($tables as mixed $entry) { array<mixed> $table = $entry as array<mixed>; array<mixed> $columns = $table['columns'] as array<mixed>; echo $table['name'], ': ', Core\Arr::count($columns), " columns\n";}guests: 2 columnsnotes: 3 columnsTwo descriptions of one schema give one array
<?nvs// Shows that one schema always has one array form. The two descriptions below// are written differently and describe the same table.
use Core\Db\Schema;
var $short = Schema::fromArray([ 'tables' => [ [ 'name' => 'notes', 'columns' => [ ['name' => 'id', 'type' => 'int64', 'identity' => true], ['name' => 'title', 'type' => 'text(200)'], ], 'primary_key' => ['id'], ], ],]);
// The same table, with the keys in another order and every optional key// written down.var $long = Schema::fromArray([ 'tables' => [ [ 'primary_key' => ['id'], 'columns' => [ ['type' => 'int64', 'name' => 'id', 'null' => false, 'identity' => true], ['type' => 'text(200)', 'name' => 'title', 'null' => false, 'identity' => false], ], 'unique' => [], 'name' => 'notes', 'indexes' => [], ], ],]);
var $first = Core\Json::encode($short->toArray());var $second = Core\Json::encode($long->toArray());
// Both arrays are the same, so this prints `the two descriptions agree`.if ($first == $second) { echo "the two descriptions agree\n";} else { echo "the two descriptions differ\n";}
echo $first, "\n";the two descriptions agree{"tables":[{"name":"notes","columns":[{"name":"id","type":"int64","null":false,"identity":true},{"name":"title","type":"text(200)","null":false,"identity":false}],"primary_key":["id"],"unique":[],"indexes":[]}]}Check the schema file next to your code
<?nvs// Shows how a release step checks that the schema file stored next to a// program still matches the tables the program needs. The check compares the// two array forms, and prints the new one when they differ.
use Core\Db\Schema;
// The tables the program needs today.var $needed = Schema::fromArray([ 'tables' => [ [ 'name' => 'notes', 'columns' => [ ['name' => 'id', 'type' => 'int64', 'identity' => true], ['name' => 'title', 'type' => 'text(200)'], ['name' => 'written_at', 'type' => 'instant'], ], 'primary_key' => ['id'], ], ],]);
// What the file holds. `nvs schema dump` writes this text.string $stored = '{"tables":[{"name":"notes","columns":[{"name":"id","type":"int64","identity":true},{"name":"title","type":"text(200)"}],"primary_key":["id"]}]}';var $onFile = Schema::fromArray(Core\Json::decode($stored) as array<mixed>);
var $wanted = Core\Json::encode($needed->toArray());
// The file is missing the `written_at` column, so this prints// `the schema file is out of date` and then the text to save.if ($wanted == Core\Json::encode($onFile->toArray())) { echo "the schema file is up to date\n";} else { echo "the schema file is out of date\n"; echo $wanted, "\n";}the schema file is out of date{"tables":[{"name":"notes","columns":[{"name":"id","type":"int64","null":false,"identity":true},{"name":"title","type":"text(200)","null":false,"identity":false},{"name":"written_at","type":"instant","null":false,"identity":false}],"primary_key":["id"],"unique":[],"indexes":[]}]}