Protocol
The page, the host and the helper speak one protocol of JSON messages. @adecore/database/protocol holds its types and pure helpers and imports nothing, so a page, a preload and a backend can all use it.
import { PROTOCOL_VERSION, valueOfCell, type DatabaseRequest, type DatabaseResponse } from '@adecore/database/protocol';A request carries an id, which its response repeats and a cancel names:
{
"id": "r4",
"method": "rows",
"params": { "session": "s1", "schema": "main", "table": "users", "where": "id > 1", "orderBy": "email DESC", "offset": 0, "limit": 2, "cellLimit": 16 }
}{
"id": "r4",
"ok": true,
"result": {
"columns": [
{ "name": "id", "type": "INTEGER", "kind": "integer" },
{ "name": "email", "type": "TEXT", "kind": "text" }
],
"rows": [
[3, "zoe@example.com"],
[2, { "kind": "longText", "preview": "a.very.long.addr", "length": 41 }]
],
"hasMore": true,
"elapsedMs": 0.4
}
}A failed request answers with ok: false and an error:
{ "id": "r9", "ok": false, "error": { "code": "conflict", "message": "The change matched no row.", "change": 2 } }DatabaseMethods maps each method to its params and its result, and DatabaseMethod is a method's name. DatabaseParams<M> and DatabaseResult<M> pick out one method's params and result. DatabaseRequest<M> and DatabaseResponse<M> are the messages above, for one method or any.
Methods
A session is what open returns. Every method except open, test, sample, discover and cancel names one in session.
| Method | Params | Result |
|---|---|---|
open | connection | { session, server } |
close | session | null |
test | connection | { server } |
schemas | session | { schemas } |
tables | session, schema | { tables } |
structure | session, schema, table | TableStructure |
rows | session, schema, table, offset, limit, where?, orderBy?, cellLimit? | RowsResult |
count | session, schema, table, where? | { count } |
cell | session, schema, table, key, column | { value } |
apply | session, schema, table, changes | { affected } |
execute | session, sql, schema?, limit?, cellLimit? | { results, inTransaction } |
page | session, sql, offset, limit, schema?, cellLimit? | RowsResult |
transaction | session, action | { active } |
export | session, source, format, path, header?, tableName? | { rows, bytes, elapsedMs } |
sample | path, format, header, limit? | { columns, rows } |
import | session, schema, table, path, format, header, columns | { rows, elapsedMs } |
discover | kind, context? | { containers } |
cancel | request | { cancelled } |
openandtesttake aConnectionConfigand report aServerInfo: theflavor(sqlite,mysqlormariadb) and the server'sversion.testcloses the connection again.schemaslistsSchemaInfo: aname, andsystemfor a schema the server keeps for itself. A schema is a MySQL database; SQLite hasmainand one per attached file.tableslistsTableInfo: thename, thekind(aTableKind,tableorview), arowEstimatefrom server statistics (rough, ornull) and thecomment.structurereturns aTableStructure:ColumnInfo,IndexInfoandForeignKeyInfolists, theprimaryKey, therowKeyand theddl. TherowKeyis the primary key, else the first unique index over non-null columns. Without one it isnulland the table is read only. A table also hastriggers,TriggerInfowithname,timing(BEFORE,AFTERorINSTEAD OF) andevent(INSERT,UPDATEorDELETE). On MySQL and MariaDB it haschecks,CheckInfowithnameand theexpressionwithoutCHECKand parentheses. A view has neither field. SQLite tables and MySQL servers before 8.0.16 have nochecks.rowsreturns one page: theResultColumnlist, rows ofCellvalues,hasMoreandelapsedMs.limitis at most 10000.cellLimitis the characters of text, or bytes of binary, a cell holds before it becomes a preview, 1024 by default.maxBytesbounds the JSON size of a response's rows: 8388608 (8 MiB) by default, at most 67108864. Rows stop before the one that would pass it andhasMoreis true, so a page can hold fewer rows thanlimit. Start the next page atoffsetplus the rows received. The first row of a result always comes, so paging always advances.whereandorderByare SQL as typed after those keywords. See Security.countcounts the rows that matchwhere.cellreturns the wholeValueof one cell.keyis aRowKey: the row key's columns with their values.applyruns a list ofRowChangevalues in one transaction. Aninserthasvalues, anupdatehas akeyandvalues, adeletehas akey. A value is anEditValue: aValue, or{ kind: 'default' }for the column default, which SQLite takes only in an insert. An update or delete that matches zero or several rows rolls everything back and fails withconflict, with the zero-based index inchange.executeruns one or more statements separated by semicolons and answers oneStatementResultper statement:rows,done(withaffectedandlastInsertId) orerror. A failed statement ends the list.limitis the rows per result, 500 by default and at most 10000.cellLimitdefaults to 65536.maxBytescovers all statements together.schemais selected first and stays selected for the session.inTransactionsays whether a transaction is open afterwards. On MySQL aLIMITin a statement overrideslimit.cancelstops the request with that id and answers whether it was still running. That request fails withcancelled, except anexecutethat had begun. It answers itsresultsup to the stopped statement, anerrorwith codecancelled, and the realinTransaction, so completed writes and open transactions stay visible.
A ResultColumn of rows, page and execute has a name, the engine's type, its kind, and a source (a ColumnSource of schema, table and column) when the server names the table column it holds. An expression column has none. SQLite looks through views and subqueries to the table. MariaDB names a derived table as a table, and a view as the view.
Reading a result in pages
page returns one page of a single reading statement, so a console can read past its first page. The statement must start with SELECT or WITH; anything else, including several statements, fails with unsupported. A page keeps the statement's order on every engine. cellLimit defaults to 65536, and maxBytes works as for rows.
Transactions
transaction takes begin, commit or rollback and answers whether a transaction is open afterwards. Beginning while one is open, or ending none, changes nothing. While one is open, execute runs in it, and apply and import run as a savepoint inside it. Closing the session rolls back an open transaction. execute also notices SQL that begins or ends a transaction itself.
Files
export, sample and import name a file by an absolute path on the helper's machine. The host asks the app first; see Files.
exportstreams every row of asource, anExportSource, to a file: a table ({ kind: 'table', schema, table, where?, orderBy? }) or one reading statement ({ kind: 'query', sql, schema? }). Theformatis aFileFormat:csv,tsv,jsonorsql.header(default true) puts column names on the first line of CSV and TSV.tableNamenames the table in theINSERTstatements ofsql. See the formats.samplereads the firstlimitlines (default 20) of a CSV or TSV file. It answers the column names, orcolumn1tocolumnNwithout a header, and the rows as strings, short lines padded with empty strings.importinserts the lines of a CSV or TSV file in one transaction.columnshas one entry per field: the target column, ornullto skip it. An empty field or\Nbecomes NULL.
Discovering containers
discover with kind: 'docker' lists running containers that look like database servers: an image name with mysql, mariadb or percona, or an exposed 3306. context picks another Docker context. Without Docker or a running daemon it fails with unsupported.
Each entry is a DockerContainer:
| Field | |
|---|---|
id, name, image | As docker ps shows them. |
engine | What the image or its ports suggest (mysql for MySQL and MariaDB), or null. |
ports | The ports inside the container, each with the host port it is published on, or null. |
project, service | From the Compose labels, or null. |
suggested | user, password and database from the environment: MYSQL_USER, MYSQL_PASSWORD and MYSQL_DATABASE or their MARIADB_ variants, else root with MYSQL_ROOT_PASSWORD or MARIADB_ROOT_PASSWORD. |
Values
A Value is null, a boolean, a number, a string or a BinaryValue ({ kind: 'binary', hex }, lowercase hex). Integers beyond Number.MAX_SAFE_INTEGER, decimals, dates and times arrive as the server's text.
A Cell is a Value when it fits the cell limit. Otherwise it is a LongTextCell ({ kind: 'longText', preview, length }, length in characters) or a BinaryCell ({ kind: 'binary', hex, length }, length in bytes). valueOfCell(cell) returns the Value for a row key or update, or undefined for a preview.
ValueKind classifies a value across engines: integer, decimal, float, boolean, text, binary, date, time, datetime, json or other.
Connections
A ConnectionConfig is a SqliteConnectionConfig or a MysqlConnectionConfig, and Engine is their engine. See Connections.
| Field | SQLite | MySQL or MariaDB |
|---|---|---|
engine | 'sqlite' | 'mysql' |
path | The absolute path of the file | |
create | Creates the file when it does not exist | |
host | The server's host name, as the far end of a tunnel sees it. Ignored, and may be empty, with a Docker tunnel | |
port | 3306 when left out | |
socket | A Unix socket instead of host and port. A tunnel overrides it | |
user, password | The account | |
database | The schema a session starts in. Without it a session sees every schema and has none selected | |
tls | A MysqlTlsMode: disable, prefer (the default), require or verify | |
tunnel | A Tunnel: an SshTunnel or a DockerTunnel | |
readOnly | Opens the connection read only | Opens the connection read only |
| Tunnel | Fields |
|---|---|
SshTunnel (kind: 'ssh') | host (a host name or a Host of ~/.ssh/config), port?, user?, identityFile? |
DockerTunnel (kind: 'docker') | container (a name or an id), port? (inside the container, 3306 when left out), context? |
A tunnel lives as long as its session, and a MySQL cancel uses it too.
Error codes
A DatabaseError has a code (a DatabaseErrorCode), a message, the five-character sqlState when the server sent one, and the change of a conflict. The client raises it as a DatabaseRequestError.
| Code | Sent by | Meaning |
|---|---|---|
invalid-request | Host, helper | The request does not have the shape of the protocol, or its id is already running. |
unknown-session | Host, helper | The session was closed, belongs to another owner, belonged to a helper that has exited, or lost its connection to the server. |
connect-failed | Helper | The server or the file could not be reached or opened. |
auth-failed | Helper | The server turned the credentials down. |
tunnel-failed | Helper | The SSH or Docker tunnel did not come up. message holds what ssh or docker said. |
query-failed | Helper | The server turned the SQL down. sqlState and message say why. |
read-only | Helper | A write on a connection opened read only, an import included. |
no-row-key | Helper | An update or a delete on a table without a row key. |
conflict | Helper | An update or a delete matched no row or more than one, so the transaction was rolled back. |
cancelled | Helper, host, client | The request was cancelled. |
unsupported | Helper | The engine cannot do what was asked, such as a statement page cannot wrap, or discover without Docker. |
file-failed | Helper | A file could not be read or written, or a line of it is broken. |
forbidden | Host | authorize, authorizeFile or authorizeDiscovery turned the request down. |
helper-exited | Host | The helper exited while the request was running. |
helper-unavailable | Host, client | The helper could not be started, did not become ready, speaks another protocol version or could not be written to. The client uses it when the transport rejects. |
internal | Host, helper | Something the protocol does not describe went wrong. |
The helper's wire
The host talks to the helper over its standard streams, one JSON message per line. A test can speak the same wire to the binary directly.
- Before it reads a request, the helper writes a ready line:
{"event":"ready","protocol":5,"version":"0.1.0"}.protocolis itsPROTOCOL_VERSIONandversionits release. - The host stops a helper whose
protocoldiffers from its own, or that sends no ready line withinreadyTimeoutMs(10000 by default). The request that started it fails withhelper-unavailable. - Then one request per line on stdin and one response per line on stdout. The helper ignores empty lines. An invalid line gets
invalid-request, with its id when readable. - Requests run concurrently, so responses can arrive out of order; the
idmatches them. Requests on one session queue in arrival order on its single connection. - When a MySQL or MariaDB session loses its connection (killed,
wait_timeout, a dropped tunnel), the session ends. The request that hit it and every request queued behind it answerunknown-session. cancelinterrupts the statement on SQLite and runsKILL QUERYfrom a second connection on MySQL. The cancelled request answerscancelled, except a completed write, which answers its result, and anexecutethat had begun.- stderr carries log lines for people, outside the protocol.
spawnHelperhands each toonLog. - A line can be megabytes, such as a page of rows with long cells.
- When stdin closes, the helper gives running requests up to two seconds, cancels the rest, rolls back open transactions, closes its sessions and exits with code 0.
Versions
PROTOCOL_VERSION is 5. It rises whenever a message changes shape, so a host refuses a helper of another release instead of misreading it. Each release ships a matching host and helper. An app that ships its own helper rebuilds it when it updates the package.