Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

feat(plugins): store plugin data on the engine TREK runs on

Maurice910c98b97192
diff --git a/plugin-sdk/package-lock.json b/plugin-sdk/package-lock.json
index f9c6c4c2a..06e57f3bd 100644
--- a/plugin-sdk/package-lock.json
+++ b/plugin-sdk/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "trek-plugin-sdk",
- "version": "1.9.1",
+ "version": "1.10.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "trek-plugin-sdk",
- "version": "1.9.1",
+ "version": "1.10.0",
"license": "MIT",
"dependencies": {
"@clack/prompts": "^1.8.1",
diff --git a/plugin-sdk/package.json b/plugin-sdk/package.json
index f0379e9aa..2d19cf97a 100644
--- a/plugin-sdk/package.json
+++ b/plugin-sdk/package.json
@@ -1,6 +1,6 @@
{
"name": "trek-plugin-sdk",
- "version": "1.9.1",
+ "version": "1.10.0",
"description": "SDK for building TREK plugins: types, definePlugin, a mock host, manifest validation, and the create-trek-plugin / trek-plugin CLIs.",
"type": "module",
"license": "MIT",
diff --git a/plugin-sdk/src/cli/dev.ts b/plugin-sdk/src/cli/dev.ts
index 1b52855ee..8f057e437 100644
--- a/plugin-sdk/src/cli/dev.ts
+++ b/plugin-sdk/src/cli/dev.ts
@@ -203,6 +203,8 @@ function createDevContext(id: string, grants: Set<string>, fx: Fixtures, db: Plu
query: (sql: string, ...a: unknown[]) => { need('db:own', 'db.query'); return db.query(sql, ...a); },
exec: (sql: string, ...a: unknown[]) => { need('db:own', 'db.exec'); return db.exec(sql, ...a); },
migrate: (mid: string, sql: string) => { need('db:own', 'db.migrate'); return db.migrate(mid, sql); },
+ // `dev` always runs your plugin on SQLite; test the Postgres dialect against a TREK on Postgres.
+ engine: async () => { need('db:own', 'db.engine'); return 'sqlite' as const; },
// A functional (not strictly atomic) tx over the real dev db so read-modify-write
// batches work in dev; production runs it in one better-sqlite3 transaction.
tx: async (ops: Array<{ sql: string; args?: unknown[] }>) => {
diff --git a/plugin-sdk/src/generated/host-facts.ts b/plugin-sdk/src/generated/host-facts.ts
index 19af6d228..297c9dd4d 100644
--- a/plugin-sdk/src/generated/host-facts.ts
+++ b/plugin-sdk/src/generated/host-facts.ts
@@ -100,6 +100,7 @@ export const METHOD_PERMISSION: Readonly<Record<string, string>> = {
'db.query': 'db:own',
'db.migrate': 'db:own',
'db.tx': 'db:own',
+ 'db.engine': 'db:own',
'trips.getById': 'db:read:trips',
'trips.getPlaces': 'db:read:trips',
'trips.getReservations': 'db:read:trips',
@@ -214,6 +215,7 @@ export const KNOWN_METHODS: string[] = [
'db.query',
'db.migrate',
'db.tx',
+ 'db.engine',
'trips.getById',
'trips.getPlaces',
'trips.getReservations',
@@ -927,6 +929,7 @@ export const PLUGIN_METHOD_RESULT: Readonly<Record<string, string>> = {
'db.query': 'host',
'db.migrate': 'host',
'db.tx': 'host',
+ 'db.engine': 'host',
'trips.getById': 'trip',
'trips.getPlaces': 'place[]',
'trips.getReservations': 'reservation[]',
diff --git a/plugin-sdk/src/index.ts b/plugin-sdk/src/index.ts
index 876c56749..025719def 100644
--- a/plugin-sdk/src/index.ts
+++ b/plugin-sdk/src/index.ts
@@ -74,8 +74,10 @@ export interface PluginContext {
settings: {
get(key: string): Promise<unknown>;
};
- /** Your OWN sqlite database (`db:own`): a separate file the plugin never gets a
- * path or connection to directly. Quota: 256 MB per plugin (writes past it fail);
+ /** Your OWN database (`db:own`): a SQLite file of its own, or, when TREK runs on
+ * Postgres, a schema of its own in TREK's database, which your SQL is confined to
+ * (`engine()` says which, and README § Your own database on Postgres has the rules).
+ * The plugin never gets a path or connection to it directly. Quota: 256 MB per plugin (writes past it fail);
* result sets are capped at 100,000 rows, and a `query`, a `tx` or an `exec` script
* without args gets 2 s of wall-clock time, checked between the rows it reads and
* the statements it runs (past it, it throws, a `tx` rolls back and a script stops
@@ -87,6 +89,9 @@ export interface PluginContext {
query<T = unknown>(sql: string, ...args: unknown[]): Promise<T[]>;
exec(sql: string, ...args: unknown[]): Promise<{ changes: number }>;
migrate(id: string, sql: string): Promise<{ applied: boolean }>;
+ /** The engine your SQL runs on: 'sqlite' (the default), or 'postgres' when TREK runs on
+ * Postgres. Your SQL is passed through as written, so write it in that engine's dialect. */
+ engine(): Promise<'sqlite' | 'postgres'>;
/** Run several statements atomically on your OWN db — all commit or all roll
* back. Each op is one statement; reads see the batch's own earlier writes.
* A read returns `{ rows }`, a write `{ changes }`. Max 100 statements. */
diff --git a/plugin-sdk/src/mock-host.ts b/plugin-sdk/src/mock-host.ts
index 519946030..35d24eb43 100644
--- a/plugin-sdk/src/mock-host.ts
+++ b/plugin-sdk/src/mock-host.ts
@@ -48,6 +48,8 @@ export interface MockHostOptions {
users?: Record<number, unknown>;
/** Optional canned db.query results, keyed by the exact sql string. */
queryResults?: Record<string, unknown[]>;
+ /** What `db.engine()` answers; 'sqlite' when unset. Set 'postgres' to test a plugin's Postgres SQL path. */
+ dbEngine?: 'sqlite' | 'postgres';
/** The host-bound acting user for costs.* (a job/onLoad has none → refused). */
actingUserId?: number;
/** Trip bound to the mock plugin UI's `session` helper. Required for scope: 'trip'. */
@@ -451,6 +453,10 @@ export function createMockHost(opts: MockHostOptions = {}): MockHost {
guardSql(sql);
return { applied: true };
},
+ async engine() {
+ need('db:own', 'db.engine');
+ return opts.dbEngine ?? 'sqlite';
+ },
async tx(ops) {
need('db:own', 'db.tx');
// Same batch guards as the host's PluginDataDb: an op cap, the forbidden
diff --git a/plugin-sdk/test/sdk.test.ts b/plugin-sdk/test/sdk.test.ts
index 51e9f279b..f22a8bbf5 100644
--- a/plugin-sdk/test/sdk.test.ts
+++ b/plugin-sdk/test/sdk.test.ts
@@ -511,6 +511,12 @@ describe('createMockHost', () => {
await expect(ctx.db.tx(Array.from({ length: 101 }, () => ({ sql: 'SELECT 1' })))).rejects.toThrow(/at most 100/);
});
+ it('db.engine says SQLite unless the mock is told the host runs Postgres, and needs db:own', async () => {
+ await expect(createMockHost({ grants: ['db:own'] }).ctx.db.engine()).resolves.toBe('sqlite');
+ await expect(createMockHost({ grants: ['db:own'], dbEngine: 'postgres' }).ctx.db.engine()).resolves.toBe('postgres');
+ await expect(createMockHost({ grants: [] }).ctx.db.engine()).rejects.toThrow(/PERMISSION_DENIED/);
+ });
+
it('member writes need member_manage + protect the owner; users.getById serves only public fields', async () => {
const host = createMockHost({
grants: ['db:write:members', 'db:read:users'],
diff --git a/server/src/nest-rpc/rpc-kit/registry.service.ts b/server/src/nest-rpc/rpc-kit/registry.service.ts
index 207b0254f..94bb0703c 100644
--- a/server/src/nest-rpc/rpc-kit/registry.service.ts
+++ b/server/src/nest-rpc/rpc-kit/registry.service.ts
@@ -37,7 +37,7 @@ export class PluginRpcRegistryService extends PluginRpcRegistry implements OnMod
* Separate from validate() because the two answer different questions: this one is
* about discovery (which providers were found, and once each), validate() is about
* the surface being complete and correctly declared. Keeping them apart lets the
- * discovery behaviour be tested without assembling all 113 methods first.
+ * discovery behaviour be tested without assembling all 115 methods first.
*/
scanProviders(): void {
// A provider can be wrapped once per module that lists it, so dedupe by instance
diff --git a/server/src/nest-rpc/rpc-kit/types.ts b/server/src/nest-rpc/rpc-kit/types.ts
index 00a2846a6..e45e572f0 100644
--- a/server/src/nest-rpc/rpc-kit/types.ts
+++ b/server/src/nest-rpc/rpc-kit/types.ts
@@ -1,4 +1,4 @@
-import type { PluginDataDb } from '../../nest/plugins/host/plugin-data.service';
+import type { PluginDataStore } from '../../nest/plugins/host/plugin-data.service';
import type { HookKey, HookPermission, KnownMethod, UnconditionalMethod } from '../../nest/plugins/protocol/envelope';
/**
@@ -18,7 +18,7 @@ import type { HookKey, HookPermission, KnownMethod, UnconditionalMethod } from '
export interface PluginRpcContext {
readonly pluginId: string;
readonly actingUserId: number | undefined;
- readonly data: PluginDataDb;
+ readonly data: PluginDataStore;
/**
* The inter-plugin capabilities. PER HOST, like everything else on this context:
* the router that backs them is created per plugin and binds THIS plugin as the
diff --git a/server/src/nest/plugins/host/plugin-data.backend.ts b/server/src/nest/plugins/host/plugin-data.backend.ts
new file mode 100644
index 000000000..e632fc310
--- /dev/null
+++ b/server/src/nest/plugins/host/plugin-data.backend.ts
@@ -0,0 +1,109 @@
+import type { DbSettings } from '../../../app-config';
+import type { EngineId } from '../../../app-config/db-settings';
+import { postgresTarget, type DatabaseEngine } from '../../../db/engine';
+import type { PluginDataStore } from '../../../db/engine/plugin-data';
+import {
+ dropPluginSchema,
+ PluginDataSlots,
+ pluginDataSlots,
+ PostgresPluginDataStore,
+} from '../../../db/engine/postgres/plugin-data-store';
+import { DATABASE_ENGINE, DATABASE_SETTINGS } from '../../database/database-engine.module';
+import { pluginDataDir } from '../paths';
+import { PluginDataDb, removePluginData } from './plugin-data.service';
+import { MikroORM } from '@mikro-orm/core';
+import type { AbstractSqlConnection } from '@mikro-orm/sql';
+import { Inject, Injectable, type Provider } from '@nestjs/common';
+
+import type { Kysely } from 'kysely';
+import fs from 'node:fs';
+
+/**
+ * Where plugins keep their own data (`db:own`), bound to the engine TREK runs
+ * on (`PLUGIN_DATA`): a SQLite file per plugin beside TREK's own file, or a
+ * schema per plugin in TREK's Postgres database, so a Postgres install
+ * writes no SQLite file to the data volume. The host opens a plugin's store
+ * through it and erases a plugin's data through it on uninstall.
+ */
+export interface PluginDataBackend {
+ /** The engine a plugin's SQL runs on, which `db.engine` tells the plugin. */
+ readonly engine: EngineId;
+ /** A store for the plugin `pluginId`, opened on first use. */
+ open(pluginId: string): PluginDataStore;
+ /** Deletes everything the plugin `pluginId` stored: its database and its data directory. */
+ erase(pluginId: string): Promise<void>;
+}
+
+export const PLUGIN_DATA = Symbol('PLUGIN_DATA');
+
+/** Each plugin's own SQLite file under the plugin data directory (`plugin-data.service.ts`). */
+@Injectable()
+export class SqlitePluginDataBackend implements PluginDataBackend {
+ readonly engine = 'sqlite' as const;
+
+ open(pluginId: string): PluginDataStore {
+ return new PluginDataDb(pluginId);
+ }
+
+ async erase(pluginId: string): Promise<void> {
+ removePluginData(pluginId);
+ }
+}
+
+/**
+ * Each plugin's own schema in TREK's Postgres database
+ * (`db/engine/postgres/plugin-data-store.ts`), reached through the ORM's
+ * pool. The plugin's data directory is still created, for the files a plugin
+ * keeps beside its database, and it is what marks a plugin uninstalled with
+ * its data kept (the erasure queue reads it).
+ */
+@Injectable()
+export class PostgresPluginDataBackend implements PluginDataBackend {
+ readonly engine = 'postgres' as const;
+ /** Shared by every plugin's store; made on first use, since on SQLite this provider is built but never used. */
+ private slots: PluginDataSlots | null = null;
+
+ constructor(
+ private readonly orm: MikroORM,
+ @Inject(DATABASE_SETTINGS) private readonly settings: DbSettings,
+ ) {}
+
+ open(pluginId: string): PluginDataStore {
+ fs.mkdirSync(pluginDataDir(pluginId), { recursive: true });
+ return new PostgresPluginDataStore(pluginId, this.client(), this.sharedSlots());
+ }
+
+ async erase(pluginId: string): Promise<void> {
+ await dropPluginSchema(this.client(), pluginId);
+ removePluginData(pluginId);
+ }
+
+ private sharedSlots(): PluginDataSlots {
+ if (this.slots === null) {
+ const target = postgresTarget(this.settings);
+ if (!target) throw new Error('The Postgres plugin data store was used without TREK_DB_URL.');
+ this.slots = new PluginDataSlots(pluginDataSlots(target.poolMax));
+ }
+ return this.slots;
+ }
+
+ /** The Kysely client on the ORM's pool. */
+ private client(): Kysely<object> {
+ return (this.orm.em.getConnection() as AbstractSqlConnection).getClient<object>();
+ }
+}
+
+/** The two backends, and `PLUGIN_DATA` bound to the one for the engine in use. */
+export const PLUGIN_DATA_PROVIDERS: Provider[] = [
+ SqlitePluginDataBackend,
+ PostgresPluginDataBackend,
+ {
+ provide: PLUGIN_DATA,
+ useFactory: (
+ engine: DatabaseEngine,
+ sqlite: SqlitePluginDataBackend,
+ postgres: PostgresPluginDataBackend,
+ ): PluginDataBackend => ({ sqlite, postgres })[engine.id],
+ inject: [DATABASE_ENGINE, SqlitePluginDataBackend, PostgresPluginDataBackend],
+ },
+];
diff --git a/server/src/nest/plugins/host/plugin-data.service.ts b/server/src/nest/plugins/host/plugin-data.service.ts
index f7d523057..5a75b1599 100644
--- a/server/src/nest/plugins/host/plugin-data.service.ts
+++ b/server/src/nest/plugins/host/plugin-data.service.ts
@@ -1,4 +1,5 @@
import { openDatabase } from '../../../db/connection';
+import { PLUGIN_DATA_CAPS, type PluginDataStore } from '../../../db/engine/plugin-data';
import { pluginDataDir, pluginDbFile, pluginsDataRoot } from '../paths';
import { splitAfterSemicolons } from './sql-script';
@@ -6,8 +7,13 @@ import type { Database as Db, Statement } from 'better-sqlite3';
import fs from 'node:fs';
import path from 'node:path';
+// The port, re-exported for the RPC kit, which may import from this file only (eslint.config.mjs).
+export type { PluginDataStore };
+
/**
- * A plugin's own sqlite database (#plugins, db:own). The HOST owns the handle;
+ * A plugin's own sqlite database (#plugins, db:own): the SQLite side of the
+ * `PluginDataStore` port (`db/engine/plugin-data.ts`; the Postgres side is a
+ * schema per plugin, `db/engine/postgres/plugin-data-store.ts`). The HOST owns the handle;
* the plugin child never gets a path or a connection — it can only reach this
* through RPC (db.exec / db.query / db.migrate). Because it is a SEPARATE FILE,
* containment is a filesystem fact: the plugin physically cannot read trek.db,
@@ -19,7 +25,7 @@ import path from 'node:path';
* script are held to a time budget (below).
*/
-const MAX_SQL_LENGTH = 100_000;
+const MAX_SQL_LENGTH = PLUGIN_DATA_CAPS.maxSqlLength;
// RECURSIVE is the one construct that generates unbounded rows/CPU independent of
// the (capped) data size — a `WITH RECURSIVE …` can spin the synchronous host
// forever even with an empty database, which neither the size quota nor the
@@ -39,11 +45,11 @@ const TX_CONTROL = /^\s*(BEGIN|COMMIT|ROLLBACK|SAVEPOINT|RELEASE|END)\b/i;
// the file (writes past it fail SQLITE_FULL, contained to the plugin) and bounds
// the worst-case scan cost. Result sets are additionally row-capped below so a
// recursive CTE / cartesian product can't materialize an unbounded array.
-const QUOTA_BYTES = 256 * 1024 * 1024;
-const MAX_ROWS = 100_000;
+const QUOTA_BYTES = PLUGIN_DATA_CAPS.quotaBytes;
+const MAX_ROWS = PLUGIN_DATA_CAPS.maxRows;
// Cap statements per atomic batch so a single tx() can't monopolise the synchronous
// host — generous for real write batches, far below anything abusive.
-const MAX_TX_OPS = 100;
+const MAX_TX_OPS = PLUGIN_DATA_CAPS.maxTxOps;
// Wall-clock budget for one query(), one whole tx() batch or one exec() script.
// better-sqlite3 has no interrupt or progress handler, so the budget is checked
// between the rows a statement yields and between the statements of a batch or a
@@ -55,7 +61,7 @@ const MAX_TX_OPS = 100;
// and the plugin-sdk README says so. migrate() is left unbudgeted on purpose: a
// migration is stopped part of the way only to fail the same way on every later
// start, which leaves a plugin that cannot update its schema at all.
-const TIME_BUDGET_MS = 2_000;
+const TIME_BUDGET_MS = PLUGIN_DATA_CAPS.timeBudgetMs;
// What better-sqlite3 throws from prepare() for a statement cut off before its end:
// in an exec() script, a trigger body whose semicolon split it.
const INCOMPLETE_INPUT = 'incomplete input';
@@ -90,9 +96,10 @@ export function checkpointAllPluginDataDbs(): void {
}
}
-export class PluginDataDb {
+export class PluginDataDb implements PluginDataStore {
private db: Db;
readonly pluginId: string;
+ readonly engine = 'sqlite' as const;
private readonly limits: PluginDataLimits;
constructor(pluginId: string, limits: Partial<PluginDataLimits> = {}) {
diff --git a/server/src/nest/plugins/host/plugin-host-state.ts b/server/src/nest/plugins/host/plugin-host-state.ts
index 801988214..b694c9450 100644
--- a/server/src/nest/plugins/host/plugin-host-state.ts
+++ b/server/src/nest/plugins/host/plugin-host-state.ts
@@ -1,6 +1,7 @@
+import type { PluginDataStore } from '../../../db/engine/plugin-data';
import type { PluginCapabilityAuditRepository } from '../../../db/repositories/PluginCapabilityAudit.repository';
import { DailyBudget, DEFAULT_DAILY_BUDGET } from './daily-budget';
-import { PluginDataDb } from './plugin-data.service';
+import type { PluginDataBackend } from './plugin-data.backend';
/**
* Process-wide plugin host state, deliberately module-level (NOT a Nest
@@ -17,9 +18,14 @@ import { PluginDataDb } from './plugin-data.service';
* to the database that no test can substitute.
*/
-const dataDbs = new Map<string, PluginDataDb>();
+const dataDbs = new Map<string, PluginDataStore>();
-export function getPluginDataDb(id: string): PluginDataDb {
+/**
+ * The open data store of plugin `id`, opened through `backend` (the one
+ * `PLUGIN_DATA` binds to the engine) when there is none or the cached one was
+ * closed.
+ */
+export function getPluginDataDb(id: string, backend: PluginDataBackend): PluginDataStore {
let d = dataDbs.get(id);
// A cached handle can be CLOSED without being evicted: the supervisor's terminal
// failure paths (activation timeout / load-error / crash auto-disable) call
@@ -27,7 +33,7 @@ export function getPluginDataDb(id: string): PluginDataDb {
// plain admin re-enable would then reuse the closed handle and every db.* call
// would throw 'database connection is not open'. Recreate when the handle is shut.
if (!d || !d.isOpen()) {
- d = new PluginDataDb(id);
+ d = backend.open(id);
dataDbs.set(id, d);
}
return d;
diff --git a/server/src/nest/plugins/host/plugin-rpc-host.factory.ts b/server/src/nest/plugins/host/plugin-rpc-host.factory.ts
index 05218757d..d0fa215ba 100644
--- a/server/src/nest/plugins/host/plugin-rpc-host.factory.ts
+++ b/server/src/nest/plugins/host/plugin-rpc-host.factory.ts
@@ -3,7 +3,8 @@ import type { PluginCapabilityAuditRepository } from '../../../db/repositories/P
import type { PluginRpcRegistry } from '../../../nest-rpc/rpc-kit/registry';
import { PluginRpcRegistryService } from '../../../nest-rpc/rpc-kit/registry.service';
import { appendAudit } from './plugin-audit';
-import { getPluginDataDb } from './plugin-host-state';
+import { PLUGIN_DATA, type PluginDataBackend } from './plugin-data.backend';
+import { closePluginDataDb, getPluginDataDb } from './plugin-host-state';
import { PluginRpcHost } from './rpc-host';
import { InjectRepository } from '@mikro-orm/nestjs';
import { Inject, Injectable } from '@nestjs/common';
@@ -27,7 +28,7 @@ export interface PluginCallRouter {
* injected here and handed to the router as a closure. The domains now own their own
* plugin surface — one `@PluginController()` provider per domain, discovered at boot
* by PluginRpcRegistryService — so all that is left is the three things that are
- * genuinely per-plugin and belong to nobody else: the plugin's own sqlite handle, the
+ * genuinely per-plugin and belong to nobody else: the plugin's own data store, the
* inter-plugin router bound with this plugin as the caller, and the audit sink.
*/
@Injectable()
@@ -37,9 +38,18 @@ export class PluginRpcHostFactory {
// Injected by its concrete token, held as the base class: a no-Nest test can then
// hand in a createTestPluginRegistry() built from the instances it cares about.
@Inject(PluginRpcRegistryService) private readonly registry: PluginRpcRegistry,
+ // Where a plugin's own data lives: a SQLite file, or a schema in TREK's Postgres database.
+ @Inject(PLUGIN_DATA) private readonly pluginData: PluginDataBackend,
) {}
+ /** Closes plugin `id`'s data store and deletes everything it stored (uninstall with "delete data"). */
+ async erasePluginData(id: string): Promise<void> {
+ closePluginDataDb(id);
+ await this.pluginData.erase(id);
+ }
+
create(id: string, granted: ReadonlySet<string>, router: PluginCallRouter): PluginRpcHost {
+ const pluginData = this.pluginData;
return new PluginRpcHost(
id,
granted,
@@ -51,10 +61,11 @@ export class PluginRpcHostFactory {
// under the NEW one (every db.* call then throws 'database connection is not
// open'). Resolving per call means the new host always uses the current, open
// handle — getPluginDataDb recreates one the moment a stale dispose closes it.
- // Safe because db.* is synchronous, so no call is ever mid-flight when a dispose
- // from another tick closes the handle.
+ // On SQLite db.* is synchronous, so no call is ever mid-flight when a dispose
+ // from another tick closes the handle; on Postgres a close only stops new calls,
+ // and one already running finishes on its own pool connection.
get data() {
- return getPluginDataDb(id);
+ return getPluginDataDb(id, pluginData);
},
// The router binds this host's plugin id as the caller/source.
callPlugin: (targetId, fn, args, actingUserId) => router.callPlugin(id, targetId, fn, args, actingUserId),
diff --git a/server/src/nest/plugins/host/rpc-host.ts b/server/src/nest/plugins/host/rpc-host.ts
index 6fa92d338..36e50941c 100644
--- a/server/src/nest/plugins/host/rpc-host.ts
+++ b/server/src/nest/plugins/host/rpc-host.ts
@@ -1,3 +1,4 @@
+import type { PluginDataStore } from '../../../db/engine/plugin-data';
import { BadParams, ForbiddenResource } from '../../../nest-rpc/rpc-errors';
import type { PluginRpcRegistry } from '../../../nest-rpc/rpc-kit/registry';
import {
@@ -10,7 +11,6 @@ import {
} from '../protocol/envelope';
import { shapePluginOutput } from '../protocol/output-contract';
import { auditResource, isAuditable } from './plugin-audit';
-import type { PluginDataDb } from './plugin-data.service';
// Both used to be declared here. They now live in rpc-errors.ts so decorated
// *.rpc.ts handlers can throw them without importing the router, and are re-exported
@@ -25,7 +25,7 @@ export { BadParams, ForbiddenResource };
* plugin cannot "call it anyway" — there is no shared object, only messages, and
* the host is the sole holder of the trek.db handle and the broadcast fns.
*
- * The handlers themselves no longer live here. Every one of the 113 wire methods is
+ * The handlers themselves no longer live here. Every one of the 115 wire methods is
* a `@PluginMethod` / `@PluginOpenMethod` on a `@PluginController()` provider in its
* own domain, and the registry binds the granted subset into the map below. What is
* left is the part that was never domain-specific: build the map, dispatch into it,
@@ -38,12 +38,12 @@ export { BadParams, ForbiddenResource };
/** What the router needs that is neither a domain service nor a permission check. */
export interface HostDeps {
/**
- * The plugin's own sqlite (db:own). A GETTER on the factory side, resolved per
- * access: disable()/re-enable builds a NEW host while the old one's dispose() may
+ * The plugin's own database (db:own): a SQLite file or a Postgres schema. A GETTER
+ * on the factory side, resolved per access: disable()/re-enable builds a NEW host while the old one's dispose() may
* still be closing the handle, so a captured one leaves the new host throwing
* 'database connection is not open'.
*/
- data: PluginDataDb;
+ data: PluginDataStore;
/** Call an export on another plugin (this host's plugin is the caller). Authorizes
* the dependency edge + the target's `provides` allowlist, forwards the acting user. */
callPlugin(targetId: string, fn: string, args: unknown, actingUserId: number | undefined): Promise<unknown>;
diff --git a/server/src/nest/plugins/host/rpc/db.rpc.ts b/server/src/nest/plugins/host/rpc/db.rpc.ts
index 16ab3830b..ec0b7798b 100644
--- a/server/src/nest/plugins/host/rpc/db.rpc.ts
+++ b/server/src/nest/plugins/host/rpc/db.rpc.ts
@@ -4,7 +4,8 @@ import { asArgs, asTxOps, str } from '../../../../nest-rpc/rpc-params';
import { PluginUserSettingsService } from '../../plugin-user-settings.service';
/**
- * A plugin's OWN sqlite, plus the three methods the router registers unconditionally.
+ * A plugin's OWN database (a SQLite file, or a schema of its own when TREK runs on
+ * Postgres), plus the three methods the router registers unconditionally.
*
* Every method reads `ctx.data` fresh. It must never be captured in a field or a
* closure: the handle is per plugin, and disable()/re-enable builds a new host while
@@ -36,6 +37,12 @@ export class DbRpc {
return ctx.data.tx(asTxOps(params.ops));
}
+ /** The engine the plugin's SQL runs on, so a plugin can write the dialect it takes. */
+ @PluginMethod('db.engine', { permission: 'db:own' })
+ engine(_params: Record<string, unknown>, ctx: PluginRpcContext): unknown {
+ return { engine: ctx.data.engine };
+ }
+
/**
* Inter-plugin call. No permission of its own: the router authorises it against
* the declared dependency edge and the target's `provides` allowlist. The acting
diff --git a/server/src/nest/plugins/plugin-runtime.service.ts b/server/src/nest/plugins/plugin-runtime.service.ts
index e18bec42a..e5179e98b 100644
--- a/server/src/nest/plugins/plugin-runtime.service.ts
+++ b/server/src/nest/plugins/plugin-runtime.service.ts
@@ -53,7 +53,6 @@ import path from 'node:path';
import { PluginRpcHostFactory } from './host/plugin-rpc-host.factory';
import { closePluginDataDb } from './host/plugin-host-state';
import { ForbiddenResource } from './host/rpc-host';
-import { removePluginData } from './host/plugin-data.service';
import { isKnownPermission } from './protocol/envelope';
import { discoverPlugins, type DiscoveryRepos } from './install/discovery';
import { enqueueHookUserDataErasures } from './user-erasure-enqueue';
@@ -1091,7 +1090,11 @@ export class PluginRuntimeService implements OnApplicationBootstrap, OnModuleDes
// leave a plugin without its row but with its settings, grants or tasks, which a
// later plugin reusing the id would inherit. The data dir goes after the commit.
await this.uow.transactional(() => this.dropPluginRows(id, deleteData));
- if (deleteData) removePluginData(id);
+ // The plugin's own database (a SQLite file, or its schema on Postgres) and its data dir.
+ if (deleteData) {
+ if (!this.hostFactory) throw new Error('PluginRpcHostFactory not provided: it is what erases the plugin data');
+ await this.hostFactory.erasePluginData(id);
+ }
}
private async dropPluginRows(id: string, deleteData: boolean): Promise<void> {
diff --git a/server/src/nest/plugins/plugins-runtime.module.ts b/server/src/nest/plugins/plugins-runtime.module.ts
index aa376d9c5..7ba23f6dc 100644
--- a/server/src/nest/plugins/plugins-runtime.module.ts
+++ b/server/src/nest/plugins/plugins-runtime.module.ts
@@ -30,6 +30,7 @@ import { BudgetModule } from '../budget/budget.module';
import { CategoriesModule } from '../categories/categories.module';
import { CollabModule } from '../collab/collab.module';
import { CollectionsModule } from '../collections/collections.module';
+import { DatabaseEngineModule } from '../database/database-engine.module';
import { DayNotesModule } from '../day-notes/day-notes.module';
import { DaysModule } from '../days/days.module';
import { FilesModule } from '../files/files.module';
@@ -47,6 +48,7 @@ import { TripMembershipModule } from '../trip-membership/trip-membership.module'
import { TripsModule } from '../trips/trips.module';
import { VacayModule } from '../vacay/vacay.module';
import { WeatherModule } from '../weather/weather.module';
+import { PLUGIN_DATA_PROVIDERS } from './host/plugin-data.backend';
import { PluginRpcHostFactory } from './host/plugin-rpc-host.factory';
import { DbRpc } from './host/rpc/db.rpc';
import { HostSurfaceRpc } from './host/rpc/host-surface.rpc';
@@ -153,8 +155,11 @@ import { DiscoveryModule } from '@nestjs/core';
NotificationsModule,
TripMembershipModule,
JournalRpcModule,
+ DatabaseEngineModule,
],
providers: [
+ // Where plugins keep their own data, bound to the engine TREK runs on.
+ ...PLUGIN_DATA_PROVIDERS,
PluginsService,
PluginUserSettingsService,
PluginRuntimeService,
diff --git a/server/src/nest/plugins/protocol/envelope.ts b/server/src/nest/plugins/protocol/envelope.ts
index 1430c3713..e288d8366 100644
--- a/server/src/nest/plugins/protocol/envelope.ts
+++ b/server/src/nest/plugins/protocol/envelope.ts
@@ -59,6 +59,7 @@ export const KNOWN_METHODS = [
'db.query',
'db.migrate',
'db.tx',
+ 'db.engine',
'trips.getById',
'trips.getPlaces',
'trips.getReservations',
@@ -194,6 +195,7 @@ export const METHOD_PERMISSION = {
'db.query': 'db:own',
'db.migrate': 'db:own',
'db.tx': 'db:own',
+ 'db.engine': 'db:own',
'trips.getById': 'db:read:trips',
'trips.getPlaces': 'db:read:trips',
'trips.getReservations': 'db:read:trips',
diff --git a/server/src/nest/plugins/protocol/output-contract.ts b/server/src/nest/plugins/protocol/output-contract.ts
index 317e56635..2db09861c 100644
--- a/server/src/nest/plugins/protocol/output-contract.ts
+++ b/server/src/nest/plugins/protocol/output-contract.ts
@@ -633,6 +633,7 @@ export const PLUGIN_METHOD_OUTPUT = {
'db.query': HOST,
'db.migrate': HOST,
'db.tx': HOST,
+ 'db.engine': HOST,
'trips.getById': row('trip'),
'trips.getPlaces': rows('place'),
'trips.getReservations': rows('reservation'),
diff --git a/server/src/nest/plugins/runtime/plugin-sdk.ts b/server/src/nest/plugins/runtime/plugin-sdk.ts
index 87d0edf53..3b848bbd0 100644
--- a/server/src/nest/plugins/runtime/plugin-sdk.ts
+++ b/server/src/nest/plugins/runtime/plugin-sdk.ts
@@ -24,6 +24,8 @@ export interface PluginContext {
query<T = unknown>(sql: string, ...args: unknown[]): Promise<T[]>;
exec(sql: string, ...args: unknown[]): Promise<{ changes: number }>;
migrate(id: string, sql: string): Promise<{ applied: boolean }>;
+ /** The engine your SQL runs on: 'sqlite' (the default), or 'postgres' when TREK runs on Postgres. */
+ engine(): Promise<'sqlite' | 'postgres'>;
/** Run several statements atomically on your OWN db — all commit or all roll
* back. Each op is one statement; reads see the batch's own earlier writes.
* A read returns `{ rows }`, a write `{ changes }`. Max 100 statements. */
@@ -824,6 +826,7 @@ export function createPluginContext(
exec: (sql, ...args) => t.rpc('db.exec', { sql, args }) as Promise<{ changes: number }>,
migrate: (mid, sql) => t.rpc('db.migrate', { id: mid, sql }) as Promise<{ applied: boolean }>,
tx: (ops) => t.rpc('db.tx', { ops }) as Promise<{ results: Array<{ changes?: number; rows?: unknown[] }> }>,
+ engine: () => (t.rpc('db.engine', {}) as Promise<{ engine: 'sqlite' | 'postgres' }>).then((r) => r.engine),
},
trips: {
getById: (tripId) => t.rpc('trips.getById', { tripId, _inv: invocationId }),
diff --git a/server/tests/helpers/plugin-host.ts b/server/tests/helpers/plugin-host.ts
index ee8a20d8a..228eec64e 100644
--- a/server/tests/helpers/plugin-host.ts
+++ b/server/tests/helpers/plugin-host.ts
@@ -63,6 +63,7 @@ import { PlacePhotoCacheService } from '../../src/nest/place-photos/place-photo-
import type { PlaceOriginsService } from '../../src/nest/places/place-origins.service';
import { PlacesRpc } from '../../src/nest/places/places.rpc';
import { PlacesService } from '../../src/nest/places/places.service';
+import { SqlitePluginDataBackend } from '../../src/nest/plugins/host/plugin-data.backend';
import { PluginRpcHostFactory } from '../../src/nest/plugins/host/plugin-rpc-host.factory';
import { DbRpc } from '../../src/nest/plugins/host/rpc/db.rpc';
import { HostSurfaceRpc } from '../../src/nest/plugins/host/rpc/host-surface.rpc';
@@ -662,7 +663,11 @@ export async function createPluginRpcHostParts(
];
const registry = createTestPluginRegistry(controllers);
return {
- factory: new PluginRpcHostFactory(pluginAuditRepo, registry as unknown as PluginRpcRegistryService),
+ factory: new PluginRpcHostFactory(
+ pluginAuditRepo,
+ registry as unknown as PluginRpcRegistryService,
+ new SqlitePluginDataBackend(),
+ ),
controllers,
};
}
diff --git a/server/tests/integration/plugins/boot-registry-order.test.ts b/server/tests/integration/plugins/boot-registry-order.test.ts
index cead7a413..c1f9d9614 100644
--- a/server/tests/integration/plugins/boot-registry-order.test.ts
+++ b/server/tests/integration/plugins/boot-registry-order.test.ts
@@ -44,6 +44,7 @@ import type { PluginRpcRegistryService } from '../../../src/nest-rpc/rpc-kit/reg
import { AddonsService } from '../../../src/nest/addons/addons.service';
import { AuditService } from '../../../src/nest/audit/audit.service';
import { UnitOfWork } from '../../../src/nest/database/unit-of-work';
+import { SqlitePluginDataBackend } from '../../../src/nest/plugins/host/plugin-data.backend';
import { PluginRpcHostFactory } from '../../../src/nest/plugins/host/plugin-rpc-host.factory';
import { DbRpc } from '../../../src/nest/plugins/host/rpc/db.rpc';
import { PluginRuntimeService } from '../../../src/nest/plugins/plugin-runtime.service';
@@ -131,6 +132,7 @@ describe('plugin boot vs registry scan ordering', () => {
const hostFactory = new PluginRpcHostFactory(
t.repo(PluginCapabilityAudit),
registry as unknown as PluginRpcRegistryService,
+ new SqlitePluginDataBackend(),
);
const userSettings = new PluginUserSettingsService(
(t as TestOrm).repo(PluginSettingsFields),
@@ -256,6 +258,7 @@ describe('plugin boot vs registry scan ordering', () => {
const hostFactory2 = new PluginRpcHostFactory(
t2.repo(PluginCapabilityAudit),
registry2 as unknown as PluginRpcRegistryService,
+ new SqlitePluginDataBackend(),
);
// Built directly on t2 (not createTestAddonsService's sharedTestOrm) so
// its repositories share t2's allowGlobalContext: false ORM.
diff --git a/server/tests/postgres/plugin-data.test.ts b/server/tests/postgres/plugin-data.test.ts
new file mode 100644
index 000000000..7ba7e2670
--- /dev/null
+++ b/server/tests/postgres/plugin-data.test.ts
@@ -0,0 +1,234 @@
+/**
+ * PGPLUG-001..008 — a plugin's own data (`db:own`) on Postgres, against the
+ * real app on the lane's database: each plugin gets a schema of its own and no
+ * SQLite file, its SQL keeps the SQLite store's semantics (migrate once per
+ * id, a script's statements committed one by one, an atomic tx, the row cap,
+ * the time budget, the quota), it cannot reach TREK's tables or another
+ * plugin's schema however it spells the name, and erasing a plugin drops its
+ * schema and nothing else.
+ */
+import { buildApp } from '../../src/bootstrap';
+import { PluginDataSlots, PostgresPluginDataStore } from '../../src/db/engine/postgres/plugin-data-store';
+import { withRequestContext } from '../../src/nest/database/request-context';
+import { PLUGIN_DATA, type PluginDataBackend } from '../../src/nest/plugins/host/plugin-data.backend';
+import { closePluginDataDb } from '../../src/nest/plugins/host/plugin-host-state';
+import { PluginRpcHostFactory } from '../../src/nest/plugins/host/plugin-rpc-host.factory';
+import { MikroORM } from '@mikro-orm/core';
+import type { AbstractSqlConnection } from '@mikro-orm/sql';
+import type { INestApplication } from '@nestjs/common';
+
+import fs from 'node:fs';
+import os from 'node:os';
+import path from 'node:path';
+import { afterAll, beforeAll, describe, expect, it } from 'vitest';
+
+let app: INestApplication;
+let orm: MikroORM;
+let data: PluginDataBackend;
+let dataDir: string;
+
+beforeAll(async () => {
+ dataDir = fs.mkdtempSync(path.join(os.tmpdir(), 'trek-pg-plugin-data-'));
+ process.env.TREK_PLUGINS_DATA_DIR = dataDir;
+ app = await buildApp();
+ orm = app.get(MikroORM);
+ data = app.get<PluginDataBackend>(PLUGIN_DATA, { strict: false });
+}, 60_000);
+
+afterAll(async () => {
+ await app?.close();
+ delete process.env.TREK_PLUGINS_DATA_DIR;
+ fs.rmSync(dataDir, { recursive: true, force: true });
+});
+
+/** The plugin schemas the database holds now. */
+async function pluginSchemas(): Promise<string[]> {
+ const rows = await withRequestContext(orm, () =>
+ orm.em
+ .getConnection()
+ .execute<Array<{ nspname: string }>>(
+ "select nspname from pg_namespace where starts_with(nspname, 'trek_plugin_') order by nspname",
+ [],
+ 'all',
+ ),
+ );
+ return rows.map((row) => row.nspname);
+}
+
+/** A store on the app's pool with limits small enough to reach in a test. */
+function cappedStore(id: string, limits: { maxRows?: number; timeBudgetMs?: number; quotaBytes?: number }) {
+ const client = (orm.em.getConnection() as AbstractSqlConnection).getClient<object>();
+ return new PostgresPluginDataStore(id, client, new PluginDataSlots(1), limits);
+}
+
+describe('plugin data on Postgres', () => {
+ it('PGPLUG-001: a plugin gets a schema of its own, migrates once per id, reads and writes there, and no SQLite file', async () => {
+ const store = data.open('pg-alpha');
+ expect(store.engine).toBe('postgres');
+ expect(await store.migrate('001', 'CREATE TABLE notes (id bigserial PRIMARY KEY, body text NOT NULL)')).toEqual({
+ applied: true,
+ });
+ expect(await store.migrate('001', 'CREATE TABLE never (x int)')).toEqual({ applied: false });
+ expect(await store.exec('INSERT INTO notes (body) VALUES ($1), ($2)', ['a', 'b'])).toEqual({ changes: 2 });
+ expect(await store.query('SELECT body FROM notes WHERE body <> $1 ORDER BY id', ['x'])).toEqual([
+ { body: 'a' },
+ { body: 'b' },
+ ]);
+
+ expect(await pluginSchemas()).toContain('trek_plugin_pg_alpha');
+ expect(fs.readdirSync(path.join(dataDir, 'pg-alpha'))).toEqual([]);
+ });
+
+ it('PGPLUG-002: a script commits statement by statement, a tx all or nothing', async () => {
+ const store = data.open('pg-script');
+ await store.migrate('001', 'CREATE TABLE kv (k text PRIMARY KEY, n int NOT NULL)');
+
+ await expect(store.exec("INSERT INTO kv VALUES ('one', 1); INSERT INTO kv VALUES ('one', 2)")).rejects.toThrow(
+ /duplicate key/,
+ );
+ expect(await store.query('SELECT k, n FROM kv')).toEqual([{ k: 'one', n: 1 }]);
+
+ await expect(
+ store.tx([
+ { sql: 'UPDATE kv SET n = n + 1 WHERE k = $1', args: ['one'] },
+ { sql: "INSERT INTO kv VALUES ('one', 9)" },
+ ]),
+ ).rejects.toThrow(/duplicate key/);
+ expect(await store.query('SELECT n FROM kv')).toEqual([{ n: 1 }]);
+
+ const batch = await store.tx([
+ { sql: "INSERT INTO kv VALUES ('two', 2)" },
+ { sql: 'SELECT k FROM kv ORDER BY k' },
+ { sql: "UPDATE kv SET n = 3 WHERE k = 'two' RETURNING n" },
+ ]);
+ expect(batch.results).toEqual([{ changes: 1 }, { rows: [{ k: 'one' }, { k: 'two' }] }, { rows: [{ n: 3 }] }]);
+ await expect(store.tx([{ sql: 'COMMIT' }])).rejects.toThrow(/transaction-control/);
+ await expect(store.query('SELECT n FROM kv WHERE k = ?', ['one'])).rejects.toThrow(/\$1, \$2/);
+ });
+
+ it("PGPLUG-003: a plugin cannot read TREK's tables, however it spells their name", async () => {
+ const store = data.open('pg-alpha');
+ // Unqualified, the name finds nothing: only the plugin's schema is on the search_path.
+ await expect(store.query('SELECT email FROM users')).rejects.toThrow(/does not exist/);
+ for (const sql of [
+ 'SELECT email FROM public.users',
+ 'SELECT email FROM "public"."users"',
+ 'SELECT email FROM PUBLIC . users',
+ 'SELECT email FROM public/* x */.users',
+ "SELECT query_to_xml('select email from public.users', true, true, '')",
+ "SELECT ts_stat('select to_tsvector(email) from public.users')",
+ 'SELECT * FROM pg_catalog.pg_tables',
+ 'SELECT * FROM pg_stats',
+ 'SELECT * FROM information_schema.tables',
+ "SELECT set_config('search_path', 'public', true)",
+ "SELECT setval('public.users_id_seq', 1)",
+ "SELECT 'public.users'::regclass",
+ 'SELECT database_to_xml(true, true, $$x$$)',
+ "SELECT E'\\'' || 'x'",
+ ]) {
+ await expect(store.query(sql), sql).rejects.toThrow(/not allowed for plugin databases/);
+ }
+ for (const sql of [
+ 'CREATE TEMP TABLE t (x int)',
+ 'SELECT 1 AS x INTO t2',
+ 'CREATE FUNCTION f() RETURNS int LANGUAGE sql AS $$ SELECT 1 $$',
+ 'ALTER TABLE notes SET SCHEMA public',
+ 'ALTER TABLE notes OWNER TO postgres',
+ 'CREATE SCHEMA mine',
+ 'DROP SCHEMA trek_plugin_pg_alpha CASCADE',
+ 'SET search_path TO public',
+ "COPY notes TO '/tmp/x'",
+ 'CREATE TABLE copy AS SELECT * FROM public.users',
+ 'CREATE VIEW v AS SELECT email FROM public.users',
+ 'GRANT SELECT ON notes TO PUBLIC',
+ 'DROP OWNED BY CURRENT_USER',
+ ]) {
+ await expect(store.exec(sql), sql).rejects.toThrow(/not allowed/);
+ }
+ // Nothing of TREK's was touched along the way.
+ const [{ n }] = await withRequestContext(orm, () =>
+ orm.em.getConnection().execute<Array<{ n: number }>>('select count(*) as n from users', [], 'all'),
+ );
+ expect(n).toBeGreaterThanOrEqual(0);
+ });
+
+ it("PGPLUG-004: two plugins never see each other's schema, even with tables of one name", async () => {
+ const alpha = data.open('pg-alpha');
+ const beta = data.open('pg-beta');
+ await beta.migrate('001', 'CREATE TABLE notes (id bigserial PRIMARY KEY, body text NOT NULL)');
+ await beta.exec('INSERT INTO notes (body) VALUES ($1)', ['beta secret']);
+
+ expect(await alpha.query('SELECT body FROM notes ORDER BY id')).toEqual([{ body: 'a' }, { body: 'b' }]);
+ expect(await beta.query('SELECT body FROM notes')).toEqual([{ body: 'beta secret' }]);
+ for (const sql of [
+ 'SELECT body FROM trek_plugin_pg_beta.notes',
+ 'SELECT body FROM "trek_plugin_pg_beta".notes',
+ 'SELECT body FROM trek_plugin_not_yet_installed.notes',
+ ]) {
+ await expect(alpha.query(sql), sql).rejects.toThrow(/another schema/);
+ }
+ // Its own schema, qualified, is fine.
+ expect(await alpha.query('SELECT count(*) AS n FROM trek_plugin_pg_alpha.notes')).toEqual([{ n: 2 }]);
+ });
+
+ it('PGPLUG-005: the row cap and the time budget hold, the budget even inside one statement', async () => {
+ const store = cappedStore('pg-capped', { maxRows: 5, timeBudgetMs: 300 });
+ await expect(store.query('SELECT x FROM generate_series(1, 10) AS g(x)')).rejects.toThrow(
+ 'query returned more than 5 rows',
+ );
+ expect(await store.query('SELECT x FROM generate_series(1, 5) AS g(x)')).toHaveLength(5);
+ await expect(
+ store.tx([{ sql: 'SELECT 1 FROM generate_series(1, 3)' }, { sql: 'SELECT 1 FROM generate_series(1, 3)' }]),
+ ).rejects.toThrow('tx returned more than 5 rows in total');
+ await expect(store.query('SELECT count(*) FROM generate_series(1, 2000000000)')).rejects.toThrow(
+ 'query exceeded its 300 ms time budget',
+ );
+ });
+
+ it('PGPLUG-006: a write that takes the schema past its quota is rolled back, a delete still runs', async () => {
+ const store = cappedStore('pg-quota', { quotaBytes: 64 * 1024 });
+ await store.migrate('001', 'CREATE TABLE blobs (body text)');
+ await store.exec("INSERT INTO blobs SELECT repeat('x', 100) FROM generate_series(1, 10)");
+ await expect(
+ store.exec('INSERT INTO blobs SELECT md5(x::text) FROM generate_series(1, 20000) AS g(x)'),
+ ).rejects.toThrow(/past its 0\.0625 MB quota/);
+ expect(await store.query('SELECT count(*) AS n FROM blobs')).toEqual([{ n: 10 }]);
+ expect(await store.exec('DELETE FROM blobs WHERE length(body) > $1', [0])).toEqual({ changes: 10 });
+ });
+
+ it('PGPLUG-007: erasing a plugin drops its schema and its data directory, and only its own', async () => {
+ data.open('pg-gone');
+ await data.open('pg-gone').migrate('001', 'CREATE TABLE t (x int)');
+ expect(fs.existsSync(path.join(dataDir, 'pg-gone'))).toBe(true);
+
+ await data.erase('pg-gone');
+
+ const schemas = await pluginSchemas();
+ expect(schemas).not.toContain('trek_plugin_pg_gone');
+ expect(schemas).toEqual(expect.arrayContaining(['trek_plugin_pg_alpha', 'trek_plugin_pg_beta']));
+ expect(fs.existsSync(path.join(dataDir, 'pg-gone'))).toBe(false);
+ // An id no schema can be made from has nothing to drop.
+ await expect(data.erase('Not_An_Id')).resolves.toBeUndefined();
+ });
+
+ it('PGPLUG-008: through the RPC host, db.* reaches the plugin schema and db.engine says postgres', async () => {
+ const router = { callPlugin: async () => undefined, emitPluginEvent: async () => {} };
+ const host = app.get(PluginRpcHostFactory).create('pg-rpc', new Set(['db:own']), router);
+ const call = (method: string, params: Record<string, unknown>) =>
+ host.dispatch({ k: 'req', id: method, method, params });
+
+ expect(await call('db.engine', {})).toMatchObject({ ok: true, result: { engine: 'postgres' } });
+ expect(await call('db.migrate', { id: '001', sql: 'CREATE TABLE t (v text)' })).toMatchObject({ ok: true });
+ expect(await call('db.exec', { sql: 'INSERT INTO t VALUES ($1)', args: ['x'] })).toMatchObject({
+ ok: true,
+ result: { changes: 1 },
+ });
+ expect(await call('db.query', { sql: 'SELECT v FROM t' })).toMatchObject({ ok: true, result: [{ v: 'x' }] });
+ expect(await call('db.query', { sql: 'SELECT email FROM public.users' })).toMatchObject({
+ ok: false,
+ error: { code: 'HOST_ERROR' },
+ });
+ expect(await pluginSchemas()).toContain('trek_plugin_pg_rpc');
+ closePluginDataDb('pg-rpc');
+ });
+});
diff --git a/server/tests/unit/plugins/host-rpc.test.ts b/server/tests/unit/plugins/host-rpc.test.ts
index b20377461..17d074daf 100644
--- a/server/tests/unit/plugins/host-rpc.test.ts
+++ b/server/tests/unit/plugins/host-rpc.test.ts
@@ -28,6 +28,7 @@ import type { LlmConfigResolver } from '../../../src/nest/llm-parse/llm-config.r
import type { NotificationsService } from '../../../src/nest/notifications/notifications.service';
import type { PermissionsService } from '../../../src/nest/permissions/permissions.service';
import { verifyChain } from '../../../src/nest/plugins/host/plugin-audit';
+import { SqlitePluginDataBackend } from '../../../src/nest/plugins/host/plugin-data.backend';
import { getPluginDataDb, closePluginDataDb } from '../../../src/nest/plugins/host/plugin-host-state';
import { PluginRpcHostFactory, type PluginCallRouter } from '../../../src/nest/plugins/host/plugin-rpc-host.factory';
import { DbRpc } from '../../../src/nest/plugins/host/rpc/db.rpc';
@@ -416,7 +417,8 @@ const registry = createTestPluginRegistry([
uow,
),
]);
-const factory = new PluginRpcHostFactory(pluginAuditRepo, registry as unknown as PluginRpcRegistryService);
+const sqliteData = new SqlitePluginDataBackend();
+const factory = new PluginRpcHostFactory(pluginAuditRepo, registry as unknown as PluginRpcRegistryService, sqliteData);
const stubRouter: PluginCallRouter = { callPlugin: async () => undefined, emitPluginEvent: async () => {} };
const makeHost = (id: string, ...perms: string[]) => factory.create(id, new Set(perms), stubRouter);
/**
@@ -457,7 +459,7 @@ afterAll(() => {
describe("DbRpc — the plugin's own sqlite", () => {
it('HOSTRPC-001 caches one data db per plugin id', () => {
- expect(getPluginDataDb('wired')).toBe(getPluginDataDb('wired'));
+ expect(getPluginDataDb('wired', sqliteData)).toBe(getPluginDataDb('wired', sqliteData));
});
it('HOSTRPC-002 a granted db:own call runs against the plugin db', async () => {
@@ -482,13 +484,19 @@ describe("DbRpc — the plugin's own sqlite", () => {
closePluginDataDb('wtx');
});
- it('HOSTRPC-004 without db:own not one of the four is reachable', async () => {
+ it('HOSTRPC-004 without db:own not one of the five is reachable', async () => {
const host = makeHost('wnogrant');
- for (const method of ['db.query', 'db.exec', 'db.migrate', 'db.tx']) {
+ for (const method of ['db.query', 'db.exec', 'db.migrate', 'db.tx', 'db.engine']) {
expect((await call(host, method, { sql: 'SELECT 1', id: '1', ops: [] })).error?.code).toBe('PERMISSION_DENIED');
}
});
+ it('HOSTRPC-004b db.engine names the engine the plugin SQL runs on', async () => {
+ const host = makeHost('wengine', 'db:own');
+ expect((await call(host, 'db.engine', {})).result).toEqual({ engine: 'sqlite' });
+ closePluginDataDb('wengine');
+ });
+
it("HOSTRPC-005 two plugins never see each other's tables", async () => {
// The regression this guards: DbRpc is a SINGLETON that serves every plugin, so a
// handle captured in a field or a closure would hand plugin B the rows of whichever
@@ -508,20 +516,20 @@ describe("DbRpc — the plugin's own sqlite", () => {
});
it('HOSTRPC-006 a handle closed WITHOUT eviction is recreated, not reused dead', () => {
- const a = getPluginDataDb('closedcache');
+ const a = getPluginDataDb('closedcache', sqliteData);
expect(a.isOpen()).toBe(true);
// The supervisor's dispose() path: close the handle but leave it cached.
a.close();
- const b = getPluginDataDb('closedcache');
+ const b = getPluginDataDb('closedcache', sqliteData);
expect(b).not.toBe(a);
expect(b.isOpen()).toBe(true);
closePluginDataDb('closedcache');
});
it('HOSTRPC-007 closePluginDataDb drops the cached handle', () => {
- const a = getPluginDataDb('transient');
+ const a = getPluginDataDb('transient', sqliteData);
closePluginDataDb('transient');
- const b = getPluginDataDb('transient');
+ const b = getPluginDataDb('transient', sqliteData);
expect(a).not.toBe(b);
closePluginDataDb('transient');
});
diff --git a/server/tests/unit/plugins/plugin-data-backend.test.ts b/server/tests/unit/plugins/plugin-data-backend.test.ts
new file mode 100644
index 000000000..f44d66827
--- /dev/null
+++ b/server/tests/unit/plugins/plugin-data-backend.test.ts
@@ -0,0 +1,104 @@
+/**
+ * The plugin data port (src/nest/plugins/host/plugin-data.backend.ts): the
+ * backend `PLUGIN_DATA` binds is the engine's, the SQLite one keeps a file per
+ * plugin, and the Postgres one opens a schema store and writes no SQLite file.
+ * The Postgres store against a real database is the Postgres lane's
+ * tests/postgres/plugin-data.test.ts.
+ */
+import type { DbSettings } from '../../../src/app-config';
+import type { DatabaseEngine } from '../../../src/db/engine';
+import { PostgresPluginDataStore } from '../../../src/db/engine/postgres/plugin-data-store';
+import {
+ PLUGIN_DATA,
+ PLUGIN_DATA_PROVIDERS,
+ PostgresPluginDataBackend,
+ SqlitePluginDataBackend,
+} from '../../../src/nest/plugins/host/plugin-data.backend';
+import { PluginDataDb } from '../../../src/nest/plugins/host/plugin-data.service';
+import type { MikroORM } from '@mikro-orm/core';
+import type { FactoryProvider } from '@nestjs/common';
+
+import fs from 'node:fs';
+import os from 'node:os';
+import path from 'node:path';
+import { afterAll, beforeAll, describe, expect, it } from 'vitest';
+
+let tmp: string;
+beforeAll(() => {
+ tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'trek-plugin-data-port-'));
+ process.env.TREK_PLUGINS_DATA_DIR = tmp;
+});
+afterAll(() => {
+ delete process.env.TREK_PLUGINS_DATA_DIR;
+ fs.rmSync(tmp, { recursive: true, force: true });
+});
+
+/** An ORM whose Kysely client records the SQL it is handed. */
+function fakeOrm(executed: string[]): MikroORM {
+ const client = {
+ executeQuery: async (query: { sql: string }) => {
+ executed.push(query.sql);
+ return { rows: [] };
+ },
+ };
+ return { em: { getConnection: () => ({ getClient: () => client }) } } as unknown as MikroORM;
+}
+
+const POSTGRES = { engine: 'postgres', url: 'postgres://trek@db/trek', poolMax: 4 } as unknown as DbSettings;
+const SQLITE = { engine: 'sqlite' } as unknown as DbSettings;
+
+describe('plugin data port', () => {
+ it('PLUGDATA-001: PLUGIN_DATA is the backend of the engine TREK runs on', () => {
+ const binding = PLUGIN_DATA_PROVIDERS.find(
+ (p): p is FactoryProvider => typeof p === 'object' && 'provide' in p && p.provide === PLUGIN_DATA,
+ );
+ expect(binding).toBeDefined();
+ const sqlite = new SqlitePluginDataBackend();
+ const postgres = new PostgresPluginDataBackend(fakeOrm([]), POSTGRES);
+ const pick = (id: 'sqlite' | 'postgres') => binding?.useFactory({ id } as DatabaseEngine, sqlite, postgres);
+ expect(pick('sqlite')).toBe(sqlite);
+ expect(pick('postgres')).toBe(postgres);
+ expect(sqlite.engine).toBe('sqlite');
+ expect(postgres.engine).toBe('postgres');
+ });
+
+ it("PLUGDATA-002: on SQLite a plugin's data is a file of its own, and erasing removes its directory", async () => {
+ const backend = new SqlitePluginDataBackend();
+ const store = backend.open('sqlite-one');
+ expect(store).toBeInstanceOf(PluginDataDb);
+ expect(store.engine).toBe('sqlite');
+ store.close();
+ expect(fs.existsSync(path.join(tmp, 'sqlite-one', 'plugin.db'))).toBe(true);
+ await backend.erase('sqlite-one');
+ expect(fs.existsSync(path.join(tmp, 'sqlite-one'))).toBe(false);
+ });
+
+ it('PLUGDATA-003: on Postgres a plugin gets a schema store and its data directory, but no SQLite file', async () => {
+ const executed: string[] = [];
+ const backend = new PostgresPluginDataBackend(fakeOrm(executed), POSTGRES);
+ const store = backend.open('pg-one');
+ expect(store).toBeInstanceOf(PostgresPluginDataStore);
+ expect(store.engine).toBe('postgres');
+ expect(fs.readdirSync(path.join(tmp, 'pg-one'))).toEqual([]);
+ expect(executed).toEqual([]);
+
+ await backend.erase('pg-one');
+ expect(executed).toEqual(['drop schema if exists "trek_plugin_pg_one" cascade']);
+ expect(fs.existsSync(path.join(tmp, 'pg-one'))).toBe(false);
+ });
+
+ it('PLUGDATA-004: the Postgres backend refuses to open a store without a Postgres database', () => {
+ const backend = new PostgresPluginDataBackend(fakeOrm([]), SQLITE);
+ expect(() => backend.open('pg-two')).toThrow(/without TREK_DB_URL/);
+ });
+
+ it('PLUGDATA-005: a closed Postgres store refuses new calls, so the host opens a fresh one', async () => {
+ const backend = new PostgresPluginDataBackend(fakeOrm([]), POSTGRES);
+ const store = backend.open('pg-three');
+ expect(store.isOpen()).toBe(true);
+ store.close();
+ expect(store.isOpen()).toBe(false);
+ await expect(store.query('SELECT 1')).rejects.toThrow('The database connection is not open');
+ await expect(store.query('SELECT * FROM public.users')).rejects.toThrow('The database connection is not open');
+ });
+});
diff --git a/server/tests/unit/plugins/rpc-coverage.test.ts b/server/tests/unit/plugins/rpc-coverage.test.ts
index 88128cd41..752629f40 100644
--- a/server/tests/unit/plugins/rpc-coverage.test.ts
+++ b/server/tests/unit/plugins/rpc-coverage.test.ts
@@ -46,13 +46,13 @@ describe('plugin RPC coverage ledger', () => {
expect(fromRegistry.size).toBe(KNOWN_METHODS.length + UNCONDITIONAL_METHODS.length);
});
- it('RPCLEDGER-005 the surface is the 114 methods the protocol declares', () => {
+ it('RPCLEDGER-005 the surface is the 115 methods the protocol declares', () => {
// Pinned rather than derived: a method DELETED from KNOWN_METHODS together with
// its handler would keep every test above green, and this is the line that makes
// that show up in the diff.
- expect(KNOWN_METHODS.length).toBe(111);
+ expect(KNOWN_METHODS.length).toBe(112);
expect(UNCONDITIONAL_METHODS.length).toBe(3);
- expect(fromRegistry.size).toBe(114);
+ expect(fromRegistry.size).toBe(115);
});
it('RPCLEDGER-006 boot-time total coverage is armed in production', () => {