denetorus / galaxy-db
Independent tabular data access with a structured array query API.
Requires
- php: >=8.4
Requires (Dev)
None
Suggests
- ext-pdo_pgsql: Required only for PostgresAdapter.
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-30 09:49:33 UTC
README
GalaxyDB reads and changes tabular data through one PHP interface. You describe the tables and pass queries as arrays. Use JSON for local data or pass an open PDO connection to the PostgreSQL adapter.
Run your first JSON query
Use PHP 8.4 or later. In your project, install version 0.1.0 from Packagist with composer require denetorus/galaxy-db:0.1.0. Then create employees.json next to vendor/ with this sample data.
[
{"id": 1, "name": "Alex", "gross": 10000},
{"id": 2, "name": "Anna", "gross": 8000}
]
Save the following as demo.php next to employees.json and run php demo.php.
<?php declare(strict_types=1); use Galaxy\DB\Adapter\JsonAdapter; use Galaxy\DB\ColumnSchema; use Galaxy\DB\DataType; use Galaxy\DB\TableSchema; require __DIR__ . '/vendor/autoload.php'; $db = new JsonAdapter([ [ 'file' => __DIR__ . '/employees.json', 'schema' => new TableSchema('employees', [ new ColumnSchema('id', DataType::INTEGER), new ColumnSchema('name', DataType::STRING), new ColumnSchema('gross', DataType::INTEGER), ], primaryKey: ['id']), ], ]); $rows = $db->selectRows([ 'table' => 'employees', 'columns' => ['name', 'gross'], 'filter' => ['gross', '>', 9000], 'orderBy' => [['gross', 'desc'], ['id', 'asc']], 'limit' => 1, ]); foreach ($rows as $row) { echo $row['name'], ': ', $row['gross'], PHP_EOL; }
The output is Alex: 10000. The query filters for amounts above 9000, sorts the matches and returns the name and amount of the first row. The additional sort by id makes the order stable when amounts tie.
Insert, update and delete a row
Continue with the same $db and the sample file.
$db->insertRow([ 'table' => 'employees', 'values' => ['id' => 3, 'name' => 'Sam', 'gross' => 8500], ]); $result = $db->updateRows([ 'table' => 'employees', 'filter' => ['id', '=', 3], 'values' => ['gross' => 9500], ]); $row = $db->findRowByPrimaryKey([ 'table' => 'employees', 'key' => ['id' => 3], ]); $db->deleteRowByPrimaryKey([ 'table' => 'employees', 'key' => ['id' => 3], ]);
After the update, $result->affectedRows is 1 and $row['gross'] is 9500. Key-based operations take the values of every primary-key column in key. Pass a filter to updateRows() and deleteRows(). To intentionally affect all rows, use 'filter' => ['all' => true].
Choose an adapter
| Source | What to provide | What to expect |
|---|---|---|
| JSON | A file containing an array of objects and a TableSchema for each table. |
The adapter reads the full file. Writers must cooperate with its file lock. An I/O failure during a write can leave incomplete contents. upsertRow() is unavailable. |
| PostgreSQL | pdo_pgsql, an open PDO connection and definitions for existing tables. |
Atomic primary-key upsertRow() is available. The caller controls any external transaction. PostgreSQL setup |
Reference
selectRow() returns the first row or null. hasMatchingRows() and countRows() check and count matches. listSchemas() and listTables() list schema and table references. Use getTableSchema() for a table definition and getSourceDescription() for adapter capabilities.
Write a comparison as ['gross', '>=', 9000], a list condition as ['id', 'in', [1, 3]] and a null check as ['name', 'is_null']. Group conditions with and, or and not. Queries filter, sort, apply offset and limit, then select columns. A limit of zero produces an empty result. Use a unique final sort key for predictable pagination.
Value types are checked strictly. Pass exact decimals as strings. The JSON adapter requires UTF‑8 and reads omitted nullable fields as null. Date and time types, generated columns and defaults are unavailable on this adapter. Joins and arbitrary SQL are outside the array query API.
Run local checks with php tests/run.php and php examples/select.php tests/autoload.php.