From 363889b97aa41f0123308506e1b71426ee9a989b Mon Sep 17 00:00:00 2001 From: Maurice Date: Mon, 23 Nov 2026 15:02:54 +0100 Subject: [PATCH] docs(plugin-sdk): state the transaction-control and NUL rules of ctx.db A plugin's own database refuses BEGIN/COMMIT in every call and NUL in SQL, bound values and migration ids on every engine; the guide and the ctx.db types now say so and point at ctx.db.tx() and Buffer binding instead. --- plugin-sdk/README.md | 14 ++++++++++++++ plugin-sdk/src/index.ts | 5 +++++ 2 files changed, 19 insertions(+) diff --git a/plugin-sdk/README.md b/plugin-sdk/README.md index a085f37c7..c077a9da1 100644 --- a/plugin-sdk/README.md +++ b/plugin-sdk/README.md @@ -127,6 +127,20 @@ Your plugin runs in an **isolated child process**. `ctx` is the only way to reac TREK, and it grants exactly the permissions your `trek-plugin.json` declares — an ungranted call throws `PERMISSION_DENIED`. +Two rules of your own database (`db:own`) hold on every engine, SQLite included, and in +`dev` and the mock host: + +- **No transaction control in your SQL.** Each `query`, `exec`, `tx` and `migrate` call runs + in a transaction of its own, so `BEGIN`, `START`, `COMMIT`, `END`, `ROLLBACK`, `ABORT`, + `SAVEPOINT` and `RELEASE` are refused in every statement with + `PLUGIN_DATA_ERRORS.txControl`. An `exec('BEGIN; INSERT ...; UPDATE ...; COMMIT;')` script, + which TREK before 5.2 ran on SQLite, now throws: pass those statements to one `ctx.db.tx([...])` + call instead, which commits them all or none. +- **No NUL (U+0000) in SQL, bound values or `migrate` ids.** Such a call is refused rather + than stored, since Postgres text cannot hold the character and TREK will not change a value + you asked to store. Bind binary data as a `Buffer` and use another separator than `'\0'` in + composite keys. Rows an older TREK already stored with a NUL are still read as they are. + ### What a ctx call returns Every ctx method falls into one of three kinds, listed per method in the exported diff --git a/plugin-sdk/src/index.ts b/plugin-sdk/src/index.ts index 025719def..12612a1f2 100644 --- a/plugin-sdk/src/index.ts +++ b/plugin-sdk/src/index.ts @@ -84,6 +84,11 @@ export interface PluginContext { * before its next statement). An `exec` with bound args (one statement) and * `migrate` are not timed, and no single statement can be stopped midway (a read * only between its rows). + * Every call runs in a transaction of its own, so transaction control (`BEGIN`, `COMMIT`, + * `ROLLBACK`, `SAVEPOINT`, ...) is refused in every statement of every call, on both + * engines; batch atomic writes with `tx()` instead of a `BEGIN; ...; COMMIT;` script. + * SQL, a bound value or a `migrate` id holding the character U+0000 (NUL) is refused on + * both engines too: bind binary data as a Buffer and keep NUL out of composite keys. * See `tx()` below for the atomic-batch cap and README § Runtime limits. */ db: { query(sql: string, ...args: unknown[]): Promise;