local-lite 2.0.5 → 2.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.KVStore = exports.DEFAULT_KV_STORE_TABLE = void 0;
4
+ exports.mapKVStoreRow = mapKVStoreRow;
5
+ exports.DEFAULT_KV_STORE_TABLE = '_local_kv_store';
6
+ function mapKVStoreRow(r) {
7
+ let val = r.value;
8
+ const type = r.type;
9
+ if (type === 'number') {
10
+ val = Number(val);
11
+ }
12
+ else if (type === 'boolean') {
13
+ val = val === 'true' || val === '1' || val === 1;
14
+ }
15
+ else if (type === 'json') {
16
+ try {
17
+ val = JSON.parse(val);
18
+ }
19
+ catch {
20
+ // fallback
21
+ }
22
+ }
23
+ return {
24
+ key: r.key,
25
+ value: val,
26
+ type: r.type,
27
+ createdAt: Number(r.created_at || 0),
28
+ updatedAt: Number(r.updated_at || 0),
29
+ expiresAt: r.expires_at != null ? Number(r.expires_at) : null,
30
+ };
31
+ }
32
+ class KVStore {
33
+ static async ensureSchema(db, tableName = exports.DEFAULT_KV_STORE_TABLE) {
34
+ await db.execute(`CREATE TABLE IF NOT EXISTS ${tableName} (
35
+ key TEXT PRIMARY KEY,
36
+ value TEXT NOT NULL,
37
+ type TEXT NOT NULL,
38
+ created_at INTEGER NOT NULL,
39
+ updated_at INTEGER NOT NULL,
40
+ expires_at INTEGER
41
+ );`);
42
+ await db.execute(`CREATE INDEX IF NOT EXISTS idx_${tableName}_expires ON ${tableName} (expires_at);`);
43
+ }
44
+ static async set(db, tableName = exports.DEFAULT_KV_STORE_TABLE, key, valueStr, type, now, expiresAt) {
45
+ await db.execute(`INSERT INTO ${tableName} (key, value, type, created_at, updated_at, expires_at)
46
+ VALUES (?, ?, ?, ?, ?, ?)
47
+ ON CONFLICT(key) DO UPDATE SET
48
+ value = excluded.value,
49
+ type = excluded.type,
50
+ updated_at = excluded.updated_at,
51
+ expires_at = excluded.expires_at;`, [key, valueStr, type, now, now, expiresAt]);
52
+ }
53
+ static async get(db, tableName = exports.DEFAULT_KV_STORE_TABLE, key) {
54
+ const rows = await db.execute(`SELECT * FROM ${tableName} WHERE key = ? LIMIT 1;`, [key]);
55
+ if (!rows || rows.length === 0)
56
+ return null;
57
+ return mapKVStoreRow(rows[0]);
58
+ }
59
+ static async delete(db, tableName = exports.DEFAULT_KV_STORE_TABLE, key) {
60
+ await db.execute(`DELETE FROM ${tableName} WHERE key = ?;`, [key]);
61
+ return true;
62
+ }
63
+ static async listKeys(db, tableName = exports.DEFAULT_KV_STORE_TABLE, prefix) {
64
+ let rows;
65
+ if (prefix) {
66
+ rows = await db.execute(`SELECT key, expires_at FROM ${tableName} WHERE key LIKE ? ORDER BY key ASC;`, [`${prefix}%`]);
67
+ }
68
+ else {
69
+ rows = await db.execute(`SELECT key, expires_at FROM ${tableName} ORDER BY key ASC;`);
70
+ }
71
+ const now = Date.now();
72
+ return (rows || [])
73
+ .filter((r) => r.expires_at == null || Number(r.expires_at) > now)
74
+ .map((r) => r.key);
75
+ }
76
+ static async clear(db, tableName = exports.DEFAULT_KV_STORE_TABLE) {
77
+ await db.execute(`DELETE FROM ${tableName};`);
78
+ }
79
+ static async pruneExpired(db, tableName = exports.DEFAULT_KV_STORE_TABLE) {
80
+ const now = Date.now();
81
+ await db.execute(`DELETE FROM ${tableName} WHERE expires_at IS NOT NULL AND expires_at <= ?;`, [now]);
82
+ return 0;
83
+ }
84
+ }
85
+ exports.KVStore = KVStore;
@@ -0,0 +1,27 @@
1
+ export interface KVStoreSetOptions {
2
+ /**
3
+ * Time-to-live in milliseconds from current time.
4
+ */
5
+ ttlMs?: number;
6
+ /**
7
+ * Explicit Unix timestamp in milliseconds when this key expires.
8
+ */
9
+ expiresAt?: number;
10
+ }
11
+ export interface KVStoreSetSecretOptions extends KVStoreSetOptions {
12
+ /**
13
+ * Passphrase used to encrypt the value using AES-256-GCM.
14
+ */
15
+ passphrase: string;
16
+ }
17
+ export interface KVStoreRecord<T = any> {
18
+ key: string;
19
+ value: T;
20
+ type: 'string' | 'number' | 'boolean' | 'json' | 'encrypted';
21
+ createdAt: number;
22
+ updatedAt: number;
23
+ expiresAt?: number | null;
24
+ }
25
+ export interface KVStoreOptions {
26
+ tableName?: string;
27
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -128,6 +128,6 @@ export * from './status.js';
128
128
  export * from './theme.js';
129
129
  export * from './auth.js';
130
130
  export * from './backups.js';
131
- export * from './kv.js';
131
+ export * from './kv-store.js';
132
132
  export * from './search.js';
133
133
  export * from './session.js';
@@ -699,6 +699,6 @@ __exportStar(require("./status.js"), exports);
699
699
  __exportStar(require("./theme.js"), exports);
700
700
  __exportStar(require("./auth.js"), exports);
701
701
  __exportStar(require("./backups.js"), exports);
702
- __exportStar(require("./kv.js"), exports);
702
+ __exportStar(require("./kv-store.js"), exports);
703
703
  __exportStar(require("./search.js"), exports);
704
704
  __exportStar(require("./session.js"), exports);
@@ -0,0 +1,9 @@
1
+ import { LocalFirstClient } from '../core/client.js';
2
+ /**
3
+ * Reactive React hook for client.kvStore key-value preferences.
4
+ * Automatically re-renders whenever the key is updated or deleted.
5
+ *
6
+ * Example:
7
+ * const [theme, setTheme, loading] = useKVStore(client, 'theme', 'light');
8
+ */
9
+ export declare function useKVStore<T = any>(client: LocalFirstClient | null | undefined, key: string, defaultValue?: T): [T | undefined, (value: T) => Promise<void>, boolean];
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useKVStore = useKVStore;
4
+ const react_1 = require("react");
5
+ /**
6
+ * Reactive React hook for client.kvStore key-value preferences.
7
+ * Automatically re-renders whenever the key is updated or deleted.
8
+ *
9
+ * Example:
10
+ * const [theme, setTheme, loading] = useKVStore(client, 'theme', 'light');
11
+ */
12
+ function useKVStore(client, key, defaultValue) {
13
+ const [value, setValueState] = (0, react_1.useState)(defaultValue);
14
+ const [loading, setLoading] = (0, react_1.useState)(true);
15
+ (0, react_1.useEffect)(() => {
16
+ if (!client) {
17
+ setValueState(defaultValue);
18
+ setLoading(false);
19
+ return;
20
+ }
21
+ let isMounted = true;
22
+ const load = async () => {
23
+ try {
24
+ const val = await client.kvStore.get(key, defaultValue);
25
+ if (isMounted) {
26
+ setValueState(val);
27
+ setLoading(false);
28
+ }
29
+ }
30
+ catch {
31
+ if (isMounted)
32
+ setLoading(false);
33
+ }
34
+ };
35
+ load();
36
+ const unsubscribe = client.events.on('_local_kv_store', (e) => {
37
+ if (!e || e.key === key || !e.key) {
38
+ load();
39
+ }
40
+ });
41
+ return () => {
42
+ isMounted = false;
43
+ unsubscribe();
44
+ };
45
+ }, [client, key, defaultValue]);
46
+ const setKVStoreValue = (0, react_1.useCallback)(async (newValue) => {
47
+ if (!client)
48
+ return;
49
+ await client.kvStore.set(key, newValue);
50
+ setValueState(newValue);
51
+ }, [client, key]);
52
+ return [value, setKVStoreValue, loading];
53
+ }
@@ -1,2 +1 @@
1
1
  export * from './generator.js';
2
- export * from './sqlMigrations.js';
@@ -15,4 +15,3 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./generator.js"), exports);
18
- __exportStar(require("./sqlMigrations.js"), exports);
@@ -0,0 +1,16 @@
1
+ import type { LocalDBAdapter } from '../adapters/local/index.js';
2
+ export interface DashboardHandlerOptions {
3
+ /** Display title in the dashboard header. */
4
+ title?: string;
5
+ /** Disable mutations and mutating SQL queries. Defaults to false. */
6
+ readOnly?: boolean;
7
+ /** Optional bearer/query token required to access the dashboard and its APIs. */
8
+ authToken?: string;
9
+ /** Base path if mounted under a sub-path (e.g. '/_dashboard'). Defaults to ''. */
10
+ basePath?: string;
11
+ }
12
+ /**
13
+ * Creates a Web Standard Request/Response handler for the local-lite dashboard.
14
+ * Can be mounted on any Node, Bun, Deno, or Edge server, or launched via `local-lite dashboard`.
15
+ */
16
+ export declare function createDashboardHandler(db: LocalDBAdapter, options?: DashboardHandlerOptions): (request: Request) => Promise<Response>;