plugins: api.openDb

Massimo Melina committed Apr 26, 2024 at 10:13 UTC a1e35140a6c18bd7407ee1ce533889f514033896
4 files changed +15 -3
dev-plugins.md
+6 -2
@@ -183,7 +183,10 @@ The `api` object you get as parameter of the `init` contains the following:
183 then discuss it on the forum because an addition to `api` is your best option for making a future-proof plugin.
184
185 - `customApiCall: (method: string, ...params) => any[]` this will invoke other plugins if they define `method`
186 - exported inside `customApi: object`
186 + exported inside `customApi: object`
187 +
188 +- `openDb: (filename, options) => Promise<{ get, put, del, close, unlink, sublevel }>` LevelDB-like class for storage.
189 + Refer to [dedicated documentation](https://www.npmjs.com/package/@rejetto/kvstorage) for details.
190
191 ## Front-end specific
192
@@ -420,7 +423,8 @@ If you want to override a text regardless of the language, use the special langu
423
424 ## API version history
425
423 -- 8.8 (v0.53.0)
426 +- 8.81 (v0.53.0)
427 + - api.openDb
428 - new event: menuZip
429 - config.type:username
430 - 8.72 (v0.52.0)
package.json
+1
@@ -71,6 +71,7 @@
71 "dependencies": {
72 "@koa/router": "^12.0.1",
73 "@node-rs/crc32": "^1.6.0",
74 + "@rejetto/kvstorage": "^0.4.0",
75 "acme-client": "^5.2.0",
76 "basic-auth": "^2.0.1",
77 "buffer-crc32": "^0.2.13",
src/const.ts
+1 -1
@@ -7,7 +7,7 @@ import { mkdirSync } from 'fs'
7 import { basename, dirname, join } from 'path'
8 export * from './cross-const'
9
10 -export const API_VERSION = 8.8
10 +export const API_VERSION = 8.81
11 export const COMPATIBLE_API_VERSION = 1 // while changes in the api are not breaking, this number stays the same, otherwise it is made equal to API_VERSION
12 export const HFS_REPO = 'rejetto/hfs'
13
src/plugins.ts
+7
@@ -19,6 +19,7 @@ import { existsSync, mkdirSync } from 'fs'
19 import { getConnections } from './connections'
20 import { dirname, join, resolve } from 'path'
21 import { watchLoadCustomHtml } from './customHtml'
22 +import { KvStorage, KvStorageOptions } from '@rejetto/kvstorage'
23
24 export const PATH = 'plugins'
25 export const DISABLING_SUFFIX = '-disabled'
@@ -381,6 +382,12 @@ function watchPlugin(id: string, path: string) {
382 await initPlugin(pluginData, {
383 srcDir: __dirname,
384 storageDir,
385 + async openDb(filename: string, options?: KvStorageOptions){
386 + if (!filename) throw Error("missing filename")
387 + const db = new KvStorage(options)
388 + await db.open(join(storageDir, filename))
389 + return db
390 + },
391 log(...args: any[]) {
392 console.log('plugin', id+':', ...args)
393 },