feat: add Database API — server-managed database for plugins#2409
feat: add Database API — server-managed database for plugins#2409dirkwa wants to merge 4 commits intoSignalK:masterfrom
Conversation
d383d4b to
1213e65
Compare
|
What do you think about using this database also for infernal storage, like: #2369 (review) ? Having it all at a central place could be very beneficial for backup and restore. |
|
NOTE: Researching for the Plugin developer CI I found that Victron Cerbo uses in latest 3.70.x node 20 I will look into a practical solution, not to loose this device. PR back to draft. |
Introduce a server-managed relational database API that gives each plugin its own isolated SQLite database. The API supports migrations, parameterized queries, transactions, and pluggable provider backends. Two built-in providers ship with the server: - _builtin: SQLite via better-sqlite3 (default) - _builtin_nodesqlite: SQLite via node:sqlite (Node >= 22.5.0) Plugins can register alternative providers (e.g. PostgreSQL) via app.registerDatabaseProvider() and consumers access the API via app.getDatabaseApi().getPluginDb(plugin.id).
On Node >= 22.5.0, use the built-in node:sqlite module as the default database provider. This eliminates the native addon dependency for most users. On Node 20 (e.g. Victron Cerbo), better-sqlite3 remains the default since node:sqlite is not available. Both providers use lazy require() with try/catch guards so the server boots cleanly regardless of what is available. Plugin providers now correctly override either built-in default.
Align with the naming convention proposed in SignalK#2417: Provider = what plugins implement, Api = what plugins consume. The registry interface follows the <D>ProviderRegistry pattern consistent with WeatherProviderRegistry, RadarProviderRegistry, etc.
1213e65 to
1f59a77
Compare
Add consumer-facing documentation (database_api.md) covering plugin usage, migrations, queries, transactions, and provider management REST endpoints. Add provider plugin documentation (database_provider_plugins.md) with interface requirements, registration example, and SQL portability notes. Update JSDoc in databaseapi.ts to reflect node:sqlite preference order.
1f59a77 to
7499b3c
Compare
|
The Cerbo node 20 topic is solved. I decided to keep this PR clean and will bring a follow up PR "on top" of this one for a separate server db as addressed in #2369 (review) to implement further improvements of the signalk-server while separating the plugin db and the server db while reusing the infrastructure. Ready for review. |
|
Additional samples where this would be a great fit:
Any alarm/notification plugin — storing alarm history as individual files is fragile |
Add a server-managed Database API that gives plugins access to an isolated relational database, eliminating the need for plugins to ship their own database binaries (better-sqlite3, DuckDB, etc.).
Follows the established provider pattern (History API, Resources API, Weather API) and the naming conventions from #2417.
{configPath}/plugin-db/{pluginId}.db_builtin_nodesqlite(node:sqlite, preferred on Node ≥22.5.0) and_builtin(better-sqlite3, fallback for Node <22.5.0)registerDatabaseProvider()Promise<>) supports both sync and async backends/signalk/v2/api/database/_providersfor provider managementPlugin API surface
Node.js compatibility
Tested all three versions with
nvm use, cleannpm install, and verified provider registration via REST API.Commits
Files
packages/server-api/src/databaseapi.ts— public interfaces (DatabaseApi,PluginDb,DatabaseProviderRegistry) with JSDocsrc/api/database/sqliteprovider.ts— better-sqlite3 provider (lazy require)src/api/database/nodesqliteprovider.ts— node:sqlite provider (lazy require)src/api/database/index.ts— registry, HTTP routes, provider managementsrc/api/index.ts,src/interfaces/plugins.ts— server bootstrap and plugin wiringdocs/develop/rest-api/database_api.md— REST API documentationdocs/develop/plugins/database_provider_plugins.md— provider plugin guideTested with
Fixes: #2407