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.
This commit is contained in:
Maurice
2026-11-23 15:02:54 +01:00
parent 48e54d5a62
commit 363889b97a
2 changed files with 19 additions and 0 deletions
+14
View File
@@ -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
+5
View File
@@ -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<T = unknown>(sql: string, ...args: unknown[]): Promise<T[]>;