@framers/sql-storage-adapter 0.7.2 → 0.7.4
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.
- package/README.md +41 -0
- package/dist/adapters/electron/preload.cjs +289 -0
- package/dist/adapters/electron/renderer.d.ts +9 -0
- package/dist/adapters/electron/renderer.d.ts.map +1 -0
- package/dist/adapters/electron/renderer.js +8 -0
- package/dist/adapters/electron/renderer.js.map +1 -0
- package/dist/cjs/adapters/__tests__/indexedDbAdapter.spec.d.ts +2 -0
- package/dist/cjs/adapters/__tests__/indexedDbAdapter.spec.js +133 -0
- package/dist/cjs/adapters/baseStorageAdapter.d.ts +368 -0
- package/dist/cjs/adapters/baseStorageAdapter.js +744 -0
- package/dist/cjs/adapters/betterSqliteAdapter.d.ts +124 -0
- package/dist/cjs/adapters/betterSqliteAdapter.js +334 -0
- package/dist/cjs/adapters/capacitorSqliteAdapter.d.ts +83 -0
- package/dist/cjs/adapters/capacitorSqliteAdapter.js +250 -0
- package/dist/cjs/adapters/electron/electronMainAdapter.d.ts +251 -0
- package/dist/cjs/adapters/electron/electronMainAdapter.js +392 -0
- package/dist/cjs/adapters/electron/electronRendererAdapter.d.ts +177 -0
- package/dist/cjs/adapters/electron/electronRendererAdapter.js +283 -0
- package/dist/cjs/adapters/electron/index.d.ts +74 -0
- package/dist/cjs/adapters/electron/index.js +86 -0
- package/dist/cjs/adapters/electron/ipc/channels.d.ts +196 -0
- package/dist/cjs/adapters/electron/ipc/channels.js +127 -0
- package/dist/cjs/adapters/electron/ipc/index.d.ts +11 -0
- package/dist/cjs/adapters/electron/ipc/index.js +38 -0
- package/dist/cjs/adapters/electron/ipc/protocol.d.ts +78 -0
- package/dist/cjs/adapters/electron/ipc/protocol.js +341 -0
- package/dist/cjs/adapters/electron/ipc/types.d.ts +248 -0
- package/dist/cjs/adapters/electron/ipc/types.js +15 -0
- package/dist/cjs/adapters/electron/migration/autoMigrator.d.ts +184 -0
- package/dist/cjs/adapters/electron/migration/autoMigrator.js +441 -0
- package/dist/cjs/adapters/electron/migration/index.d.ts +9 -0
- package/dist/cjs/adapters/electron/migration/index.js +29 -0
- package/dist/cjs/adapters/electron/preload.d.ts +126 -0
- package/dist/cjs/adapters/electron/preload.js +233 -0
- package/dist/cjs/adapters/electron/recovery/corruptionDetector.d.ts +214 -0
- package/dist/cjs/adapters/electron/recovery/corruptionDetector.js +359 -0
- package/dist/cjs/adapters/electron/recovery/index.d.ts +11 -0
- package/dist/cjs/adapters/electron/recovery/index.js +34 -0
- package/dist/cjs/adapters/electron/recovery/walCheckpoint.d.ts +186 -0
- package/dist/cjs/adapters/electron/recovery/walCheckpoint.js +263 -0
- package/dist/cjs/adapters/electron/renderer.d.ts +9 -0
- package/dist/cjs/adapters/electron/renderer.js +33 -0
- package/dist/cjs/adapters/electron/window/index.d.ts +9 -0
- package/dist/cjs/adapters/electron/window/index.js +29 -0
- package/dist/cjs/adapters/electron/window/windowManager.d.ts +190 -0
- package/dist/cjs/adapters/electron/window/windowManager.js +321 -0
- package/dist/cjs/adapters/indexedDbAdapter.d.ts +490 -0
- package/dist/cjs/adapters/indexedDbAdapter.js +993 -0
- package/dist/cjs/adapters/postgresAdapter.d.ts +424 -0
- package/dist/cjs/adapters/postgresAdapter.js +889 -0
- package/dist/cjs/adapters/sqlJsAdapter.d.ts +64 -0
- package/dist/cjs/adapters/sqlJsAdapter.js +268 -0
- package/dist/cjs/adapters/supabase.d.ts +58 -0
- package/dist/cjs/adapters/supabase.js +401 -0
- package/dist/cjs/codecs/BrowserBlobCodec.d.ts +14 -0
- package/dist/cjs/codecs/BrowserBlobCodec.js +57 -0
- package/dist/cjs/codecs/NodeBlobCodec.d.ts +14 -0
- package/dist/cjs/codecs/NodeBlobCodec.js +59 -0
- package/dist/cjs/core/contracts/blobCodec.d.ts +15 -0
- package/dist/cjs/core/contracts/blobCodec.js +15 -0
- package/dist/cjs/core/contracts/context.d.ts +221 -0
- package/dist/cjs/core/contracts/context.js +15 -0
- package/dist/cjs/core/contracts/dialect.d.ts +154 -0
- package/dist/cjs/core/contracts/dialect.js +15 -0
- package/dist/cjs/core/contracts/events.d.ts +225 -0
- package/dist/cjs/core/contracts/events.js +15 -0
- package/dist/cjs/core/contracts/exporter.d.ts +13 -0
- package/dist/cjs/core/contracts/exporter.js +15 -0
- package/dist/cjs/core/contracts/extensions.d.ts +73 -0
- package/dist/cjs/core/contracts/extensions.js +15 -0
- package/dist/cjs/core/contracts/features.d.ts +25 -0
- package/dist/cjs/core/contracts/features.js +61 -0
- package/dist/cjs/core/contracts/fts.d.ts +66 -0
- package/dist/cjs/core/contracts/fts.js +15 -0
- package/dist/cjs/core/contracts/hooks.d.ts +481 -0
- package/dist/cjs/core/contracts/hooks.js +140 -0
- package/dist/cjs/core/contracts/index.d.ts +260 -0
- package/dist/cjs/core/contracts/index.js +41 -0
- package/dist/cjs/core/contracts/limitations.d.ts +46 -0
- package/dist/cjs/core/contracts/limitations.js +218 -0
- package/dist/cjs/core/contracts/performance.d.ts +308 -0
- package/dist/cjs/core/contracts/performance.js +144 -0
- package/dist/cjs/core/database.d.ts +220 -0
- package/dist/cjs/core/database.js +136 -0
- package/dist/cjs/core/portable.d.ts +24 -0
- package/dist/cjs/core/portable.js +68 -0
- package/dist/cjs/core/resolver.d.ts +77 -0
- package/dist/cjs/core/resolver.js +235 -0
- package/dist/cjs/dialects/PostgresDialect.d.ts +35 -0
- package/dist/cjs/dialects/PostgresDialect.js +143 -0
- package/dist/cjs/dialects/SqliteDialect.d.ts +35 -0
- package/dist/cjs/dialects/SqliteDialect.js +142 -0
- package/dist/cjs/dialects/shared.d.ts +17 -0
- package/dist/cjs/dialects/shared.js +90 -0
- package/dist/cjs/exporters/PostgresExporter.d.ts +14 -0
- package/dist/cjs/exporters/PostgresExporter.js +61 -0
- package/dist/cjs/exporters/SqliteFileExporter.d.ts +16 -0
- package/dist/cjs/exporters/SqliteFileExporter.js +43 -0
- package/dist/cjs/features/auth/authJsAdapter.d.ts +110 -0
- package/dist/cjs/features/auth/authJsAdapter.js +251 -0
- package/dist/cjs/features/backup/cloudBackup.d.ts +219 -0
- package/dist/cjs/features/backup/cloudBackup.js +209 -0
- package/dist/cjs/features/kv/idb.d.ts +79 -0
- package/dist/cjs/features/kv/idb.js +32 -0
- package/dist/cjs/features/kv/index.d.ts +14 -0
- package/dist/cjs/features/kv/index.js +40 -0
- package/dist/cjs/features/kv/indexedDbKeyValueStore.d.ts +77 -0
- package/dist/cjs/features/kv/indexedDbKeyValueStore.js +116 -0
- package/dist/cjs/features/kv/keyValueStore.d.ts +48 -0
- package/dist/cjs/features/kv/keyValueStore.js +67 -0
- package/dist/cjs/features/kv/sqlKeyValueStore.d.ts +34 -0
- package/dist/cjs/features/kv/sqlKeyValueStore.js +81 -0
- package/dist/cjs/features/kv/valueCodec.d.ts +35 -0
- package/dist/cjs/features/kv/valueCodec.js +426 -0
- package/dist/cjs/features/migrations/dataExport.d.ts +77 -0
- package/dist/cjs/features/migrations/dataExport.js +194 -0
- package/dist/cjs/features/migrations/dataImport.d.ts +54 -0
- package/dist/cjs/features/migrations/dataImport.js +300 -0
- package/dist/cjs/features/migrations/migration.d.ts +89 -0
- package/dist/cjs/features/migrations/migration.js +187 -0
- package/dist/cjs/features/migrations/schemaMigrations.d.ts +93 -0
- package/dist/cjs/features/migrations/schemaMigrations.js +220 -0
- package/dist/cjs/features/sync/conflicts/conflictResolver.d.ts +222 -0
- package/dist/cjs/features/sync/conflicts/conflictResolver.js +366 -0
- package/dist/cjs/features/sync/conflicts/index.d.ts +9 -0
- package/dist/cjs/features/sync/conflicts/index.js +21 -0
- package/dist/cjs/features/sync/crossPlatformSync.d.ts +281 -0
- package/dist/cjs/features/sync/crossPlatformSync.js +551 -0
- package/dist/cjs/features/sync/devices/deviceManager.d.ts +243 -0
- package/dist/cjs/features/sync/devices/deviceManager.js +450 -0
- package/dist/cjs/features/sync/devices/index.d.ts +10 -0
- package/dist/cjs/features/sync/devices/index.js +21 -0
- package/dist/cjs/features/sync/index.d.ts +37 -0
- package/dist/cjs/features/sync/index.js +111 -0
- package/dist/cjs/features/sync/protocol/index.d.ts +11 -0
- package/dist/cjs/features/sync/protocol/index.js +23 -0
- package/dist/cjs/features/sync/protocol/messages.d.ts +348 -0
- package/dist/cjs/features/sync/protocol/messages.js +215 -0
- package/dist/cjs/features/sync/protocol/vectorClock.d.ts +164 -0
- package/dist/cjs/features/sync/protocol/vectorClock.js +256 -0
- package/dist/cjs/features/sync/syncManager.d.ts +348 -0
- package/dist/cjs/features/sync/syncManager.js +455 -0
- package/dist/cjs/features/sync/tables/index.d.ts +10 -0
- package/dist/cjs/features/sync/tables/index.js +21 -0
- package/dist/cjs/features/sync/tables/syncLogManager.d.ts +216 -0
- package/dist/cjs/features/sync/tables/syncLogManager.js +423 -0
- package/dist/cjs/features/sync/transport/httpTransport.d.ts +123 -0
- package/dist/cjs/features/sync/transport/httpTransport.js +346 -0
- package/dist/cjs/features/sync/transport/index.d.ts +12 -0
- package/dist/cjs/features/sync/transport/index.js +25 -0
- package/dist/cjs/features/sync/transport/transport.d.ts +259 -0
- package/dist/cjs/features/sync/transport/transport.js +163 -0
- package/dist/cjs/features/sync/transport/websocketTransport.d.ts +126 -0
- package/dist/cjs/features/sync/transport/websocketTransport.js +342 -0
- package/dist/cjs/fts/PostgresFts.d.ts +29 -0
- package/dist/cjs/fts/PostgresFts.js +77 -0
- package/dist/cjs/fts/SqliteFts5.d.ts +22 -0
- package/dist/cjs/fts/SqliteFts5.js +58 -0
- package/dist/cjs/index.d.ts +31 -0
- package/dist/cjs/index.js +103 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/shared/connectionGate.d.ts +94 -0
- package/dist/cjs/shared/connectionGate.js +77 -0
- package/dist/cjs/shared/errors.d.ts +23 -0
- package/dist/cjs/shared/errors.js +45 -0
- package/dist/cjs/shared/logger.d.ts +26 -0
- package/dist/cjs/shared/logger.js +40 -0
- package/dist/cjs/shared/parameterUtils.d.ts +16 -0
- package/dist/cjs/shared/parameterUtils.js +59 -0
- package/dist/cjs/shared/postgresTarget.d.ts +11 -0
- package/dist/cjs/shared/postgresTarget.js +30 -0
- package/dist/cjs/shared/segment.d.ts +43 -0
- package/dist/cjs/shared/segment.js +63 -0
- package/dist/cjs/shared/serializedTransaction.d.ts +14 -0
- package/dist/cjs/shared/serializedTransaction.js +74 -0
- package/dist/cjs/shared/splitSqlStatements.d.ts +27 -0
- package/dist/cjs/shared/splitSqlStatements.js +163 -0
- package/dist/cjs/shared/sqlPlaceholders.d.ts +48 -0
- package/dist/cjs/shared/sqlPlaceholders.js +263 -0
- package/dist/cjs/types/index.d.ts +29 -0
- package/dist/cjs/types/index.js +33 -0
- package/dist/core/portable.d.ts +24 -0
- package/dist/core/portable.d.ts.map +1 -0
- package/dist/core/portable.js +24 -0
- package/dist/core/portable.js.map +1 -0
- package/dist/features/auth/authJsAdapter.d.ts +110 -0
- package/dist/features/auth/authJsAdapter.d.ts.map +1 -0
- package/dist/features/auth/authJsAdapter.js +248 -0
- package/dist/features/auth/authJsAdapter.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/package.json +149 -24
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview The part of IndexedDB the key-value store calls, typed structurally: the module compiles without the DOM
|
|
3
|
+
* library, and a page's IndexedDB satisfies these shapes.
|
|
4
|
+
* Moved unchanged from @framers/agentos src/core/storage/browser/idb.ts.
|
|
5
|
+
* @module sql-storage-adapter/kv/idb
|
|
6
|
+
*/
|
|
7
|
+
/** A request's outcome and its two events. */
|
|
8
|
+
export interface IdbRequestLike<T> {
|
|
9
|
+
/** What the request answered, once it is done. */
|
|
10
|
+
readonly result: T;
|
|
11
|
+
/** Why the request failed, once it is done; null when it did not. */
|
|
12
|
+
readonly error: unknown;
|
|
13
|
+
/** Called when the request has succeeded. */
|
|
14
|
+
onsuccess: (() => void) | null;
|
|
15
|
+
/** Called when the request has failed. */
|
|
16
|
+
onerror: (() => void) | null;
|
|
17
|
+
}
|
|
18
|
+
/** A key range; only made and passed on. */
|
|
19
|
+
export type IdbKeyRangeLike = object;
|
|
20
|
+
/** The object store's calls the store makes. */
|
|
21
|
+
export interface IdbObjectStoreLike {
|
|
22
|
+
/** Reads the value under a key. */
|
|
23
|
+
get(key: string): IdbRequestLike<unknown>;
|
|
24
|
+
/** Writes a value under a key, in place of what was there. */
|
|
25
|
+
put(value: unknown, key: string): IdbRequestLike<unknown>;
|
|
26
|
+
/** Removes one key, or every key of a range. */
|
|
27
|
+
delete(key: string | IdbKeyRangeLike): IdbRequestLike<unknown>;
|
|
28
|
+
/** Removes every key. */
|
|
29
|
+
clear(): IdbRequestLike<unknown>;
|
|
30
|
+
/** Lists the keys of a range, or every key. */
|
|
31
|
+
getAllKeys(range?: IdbKeyRangeLike): IdbRequestLike<unknown[]>;
|
|
32
|
+
}
|
|
33
|
+
/** A transaction and its three events. */
|
|
34
|
+
export interface IdbTransactionLike {
|
|
35
|
+
/** Why the transaction was aborted; null until it is, and after a plain abort. */
|
|
36
|
+
readonly error: unknown;
|
|
37
|
+
/** One of the object stores the transaction was opened over. */
|
|
38
|
+
objectStore(name: string): IdbObjectStoreLike;
|
|
39
|
+
/** Called when everything the transaction did is kept. */
|
|
40
|
+
oncomplete: (() => void) | null;
|
|
41
|
+
/** Called when one of the transaction's requests has failed, before the transaction is aborted for it. */
|
|
42
|
+
onerror: (() => void) | null;
|
|
43
|
+
/** Called when the transaction is aborted and nothing it did is kept. */
|
|
44
|
+
onabort: (() => void) | null;
|
|
45
|
+
}
|
|
46
|
+
/** An open database. */
|
|
47
|
+
export interface IdbDatabaseLike {
|
|
48
|
+
/** The names of the database's object stores. */
|
|
49
|
+
readonly objectStoreNames: {
|
|
50
|
+
contains(name: string): boolean;
|
|
51
|
+
};
|
|
52
|
+
/** Makes an object store with out-of-line keys; only inside an upgrade. */
|
|
53
|
+
createObjectStore(name: string): unknown;
|
|
54
|
+
/** Opens a transaction over one object store. */
|
|
55
|
+
transaction(storeName: string, mode: 'readonly' | 'readwrite'): IdbTransactionLike;
|
|
56
|
+
/** Closes the connection once its transactions are done. */
|
|
57
|
+
close(): void;
|
|
58
|
+
}
|
|
59
|
+
/** The request `open` answers. */
|
|
60
|
+
export interface IdbOpenRequestLike extends IdbRequestLike<IdbDatabaseLike> {
|
|
61
|
+
/** The upgrade's transaction while `onupgradeneeded` runs; null outside it. */
|
|
62
|
+
readonly transaction: {
|
|
63
|
+
abort(): void;
|
|
64
|
+
} | null;
|
|
65
|
+
/** Called when the database is new or older than the version asked for, inside the upgrade. */
|
|
66
|
+
onupgradeneeded: (() => void) | null;
|
|
67
|
+
/** Called when an upgrade waits for another connection to close. */
|
|
68
|
+
onblocked: (() => void) | null;
|
|
69
|
+
}
|
|
70
|
+
/** The page's `indexedDB` and `IDBKeyRange`, or an error when the page has none. */
|
|
71
|
+
export declare function idb(): {
|
|
72
|
+
factory: {
|
|
73
|
+
open(name: string, version?: number): IdbOpenRequestLike;
|
|
74
|
+
};
|
|
75
|
+
keyRange: {
|
|
76
|
+
bound(lower: string, upper: string): IdbKeyRangeLike;
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
//# sourceMappingURL=idb.d.ts.map
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var idb_exports = {};
|
|
19
|
+
__export(idb_exports, {
|
|
20
|
+
idb: () => idb
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(idb_exports);
|
|
23
|
+
function idb() {
|
|
24
|
+
const scope = globalThis;
|
|
25
|
+
if (!scope.indexedDB || !scope.IDBKeyRange)
|
|
26
|
+
throw new Error("IndexedDB is not available here.");
|
|
27
|
+
return { factory: scope.indexedDB, keyRange: scope.IDBKeyRange };
|
|
28
|
+
}
|
|
29
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
30
|
+
0 && (module.exports = {
|
|
31
|
+
idb
|
|
32
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview The key-value module, `@framers/sql-storage-adapter/kv`: one `KeyValueStore` contract and its
|
|
3
|
+
* stores. `openIndexedDbStore` keeps values in one IndexedDB object store, moved unchanged from @framers/agentos, so a
|
|
4
|
+
* page's keys stay where they are; `createSqlKeyValueStore` keeps them in a SQL table on any adapter; the memory store
|
|
5
|
+
* keeps them for the page alone.
|
|
6
|
+
*
|
|
7
|
+
* The module imports no adapter, so a page that loads it for its key-value store loads neither sql.js nor pg.
|
|
8
|
+
* @module sql-storage-adapter/kv
|
|
9
|
+
*/
|
|
10
|
+
export { MemoryKeyValueStore, namespaced, type KeyValueStore } from './keyValueStore.js';
|
|
11
|
+
export { IndexedDbKeyValueStore, openIndexedDbStore, type IndexedDbStoreOptions } from './indexedDbKeyValueStore.js';
|
|
12
|
+
export { idb, type IdbDatabaseLike, type IdbKeyRangeLike, type IdbObjectStoreLike, type IdbOpenRequestLike, type IdbRequestLike, type IdbTransactionLike, } from './idb.js';
|
|
13
|
+
export { createSqlKeyValueStore, type SqlKeyValueStoreOptions } from './sqlKeyValueStore.js';
|
|
14
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var kv_exports = {};
|
|
19
|
+
__export(kv_exports, {
|
|
20
|
+
IndexedDbKeyValueStore: () => import_indexedDbKeyValueStore.IndexedDbKeyValueStore,
|
|
21
|
+
MemoryKeyValueStore: () => import_keyValueStore.MemoryKeyValueStore,
|
|
22
|
+
createSqlKeyValueStore: () => import_sqlKeyValueStore.createSqlKeyValueStore,
|
|
23
|
+
idb: () => import_idb.idb,
|
|
24
|
+
namespaced: () => import_keyValueStore.namespaced,
|
|
25
|
+
openIndexedDbStore: () => import_indexedDbKeyValueStore.openIndexedDbStore
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(kv_exports);
|
|
28
|
+
var import_keyValueStore = require("./keyValueStore.js");
|
|
29
|
+
var import_indexedDbKeyValueStore = require("./indexedDbKeyValueStore.js");
|
|
30
|
+
var import_idb = require("./idb.js");
|
|
31
|
+
var import_sqlKeyValueStore = require("./sqlKeyValueStore.js");
|
|
32
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
33
|
+
0 && (module.exports = {
|
|
34
|
+
IndexedDbKeyValueStore,
|
|
35
|
+
MemoryKeyValueStore,
|
|
36
|
+
createSqlKeyValueStore,
|
|
37
|
+
idb,
|
|
38
|
+
namespaced,
|
|
39
|
+
openIndexedDbStore
|
|
40
|
+
});
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview The key-value store over one IndexedDB object store with out-of-line keys. It opens a database at
|
|
3
|
+
* the version given (1 by default) and creates the object store only when the open upgrades the database (a new one,
|
|
4
|
+
* or one below that version), so a database another page made at that version is read in place. A browser that
|
|
5
|
+
* refuses IndexedDB gets the memory store.
|
|
6
|
+
* Moved unchanged from @framers/agentos src/core/storage/browser/IndexedDbKeyValueStore.ts: the same database,
|
|
7
|
+
* object store, version and keys.
|
|
8
|
+
* @module sql-storage-adapter/kv/indexedDbKeyValueStore
|
|
9
|
+
*/
|
|
10
|
+
import { type IdbDatabaseLike } from './idb.js';
|
|
11
|
+
import { type KeyValueStore } from './keyValueStore.js';
|
|
12
|
+
/** Where the store lives. */
|
|
13
|
+
export interface IndexedDbStoreOptions {
|
|
14
|
+
/** The database's name. */
|
|
15
|
+
dbName: string;
|
|
16
|
+
/** The object store's name. It is made, with out-of-line keys, only when the open upgrades the database. */
|
|
17
|
+
storeName: string;
|
|
18
|
+
/**
|
|
19
|
+
* The version the database is opened at. A database that exists at this version is opened as it is, even when it
|
|
20
|
+
* lacks the object store, and every call of the store then rejects; one below it is upgraded, and gains the object
|
|
21
|
+
* store when it lacks it; one above it is refused by the browser.
|
|
22
|
+
* @default 1
|
|
23
|
+
*/
|
|
24
|
+
version?: number;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The store over IndexedDB. Each call is one transaction over the object store: its promise is fulfilled when that
|
|
28
|
+
* transaction completes, and rejected with the reason when its request fails or the transaction is aborted.
|
|
29
|
+
*
|
|
30
|
+
* `keys` and `deletePrefix` read the keys between `prefix` and `prefix` followed by U+FFFF, so they miss a key that
|
|
31
|
+
* holds U+FFFF right after the prefix and goes on: keep keys to characters below it.
|
|
32
|
+
*
|
|
33
|
+
* The connection stays open until `close`: when another tab opens the database at a newer version, its upgrade waits
|
|
34
|
+
* for this tab to close the store or go. A connection the browser closes (the database deleted, the site's storage
|
|
35
|
+
* cleared) is not opened again, and every call then rejects.
|
|
36
|
+
*/
|
|
37
|
+
export declare class IndexedDbKeyValueStore implements KeyValueStore {
|
|
38
|
+
private readonly db;
|
|
39
|
+
private readonly storeName;
|
|
40
|
+
/** Always false: the values are in the browser's database. */
|
|
41
|
+
readonly memoryOnly = false;
|
|
42
|
+
/**
|
|
43
|
+
* @param db - An open database that holds the object store.
|
|
44
|
+
* @param storeName - The object store's name.
|
|
45
|
+
*/
|
|
46
|
+
constructor(db: IdbDatabaseLike, storeName: string);
|
|
47
|
+
private run;
|
|
48
|
+
private static range;
|
|
49
|
+
/** The value held under `key`, or undefined when there is none. */
|
|
50
|
+
get<T = unknown>(key: string): Promise<T | undefined>;
|
|
51
|
+
/** Holds a structured clone of `value` under `key`. */
|
|
52
|
+
set(key: string, value: unknown): Promise<void>;
|
|
53
|
+
/** Removes `key`. */
|
|
54
|
+
delete(key: string): Promise<void>;
|
|
55
|
+
/**
|
|
56
|
+
* Every key that begins with `prefix`, in order. With an empty prefix, every key of the object store, a key that is
|
|
57
|
+
* not a string (a number, a date or an array another page wrote) given as `String` writes it.
|
|
58
|
+
*/
|
|
59
|
+
keys(prefix: string): Promise<string[]>;
|
|
60
|
+
/** Removes every key that begins with `prefix`; with an empty prefix, everything the object store holds. */
|
|
61
|
+
deletePrefix(prefix: string): Promise<void>;
|
|
62
|
+
/** Closes the database connection. */
|
|
63
|
+
close(): void;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Opens the store, or the memory store when the browser refuses IndexedDB: a page with no `indexedDB`, an `open`
|
|
67
|
+
* that throws or fails (a database above `version` among them), or an upgrade that waits on another tab. The
|
|
68
|
+
* answer's `memoryOnly` says which it is; the memory store's `close` does nothing.
|
|
69
|
+
*
|
|
70
|
+
* An upgrade that waited on another tab still has its turn once that tab closes the database. It is aborted then,
|
|
71
|
+
* so the database stays as it was and the connection it opened is closed: nothing is left open behind the memory
|
|
72
|
+
* store.
|
|
73
|
+
*/
|
|
74
|
+
export declare function openIndexedDbStore(options: IndexedDbStoreOptions): Promise<KeyValueStore & {
|
|
75
|
+
close(): void;
|
|
76
|
+
}>;
|
|
77
|
+
//# sourceMappingURL=indexedDbKeyValueStore.d.ts.map
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var indexedDbKeyValueStore_exports = {};
|
|
19
|
+
__export(indexedDbKeyValueStore_exports, {
|
|
20
|
+
IndexedDbKeyValueStore: () => IndexedDbKeyValueStore,
|
|
21
|
+
openIndexedDbStore: () => openIndexedDbStore
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(indexedDbKeyValueStore_exports);
|
|
24
|
+
var import_idb = require("./idb.js");
|
|
25
|
+
var import_keyValueStore = require("./keyValueStore.js");
|
|
26
|
+
class IndexedDbKeyValueStore {
|
|
27
|
+
/**
|
|
28
|
+
* @param db - An open database that holds the object store.
|
|
29
|
+
* @param storeName - The object store's name.
|
|
30
|
+
*/
|
|
31
|
+
constructor(db, storeName) {
|
|
32
|
+
this.db = db;
|
|
33
|
+
this.storeName = storeName;
|
|
34
|
+
this.memoryOnly = false;
|
|
35
|
+
}
|
|
36
|
+
run(mode, work) {
|
|
37
|
+
return new Promise((resolve, reject) => {
|
|
38
|
+
const transaction = this.db.transaction(this.storeName, mode);
|
|
39
|
+
const request = work(transaction.objectStore(this.storeName));
|
|
40
|
+
transaction.oncomplete = () => resolve(request.result);
|
|
41
|
+
transaction.onerror = () => reject(transaction.error ?? request.error ?? new Error("IndexedDB request failed"));
|
|
42
|
+
transaction.onabort = () => reject(transaction.error ?? new Error("IndexedDB transaction aborted"));
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
static range(prefix) {
|
|
46
|
+
return prefix === "" ? void 0 : (0, import_idb.idb)().keyRange.bound(prefix, `${prefix}\uFFFF`);
|
|
47
|
+
}
|
|
48
|
+
/** The value held under `key`, or undefined when there is none. */
|
|
49
|
+
async get(key) {
|
|
50
|
+
return await this.run("readonly", (store) => store.get(key));
|
|
51
|
+
}
|
|
52
|
+
/** Holds a structured clone of `value` under `key`. */
|
|
53
|
+
async set(key, value) {
|
|
54
|
+
await this.run("readwrite", (store) => store.put(value, key));
|
|
55
|
+
}
|
|
56
|
+
/** Removes `key`. */
|
|
57
|
+
async delete(key) {
|
|
58
|
+
await this.run("readwrite", (store) => store.delete(key));
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Every key that begins with `prefix`, in order. With an empty prefix, every key of the object store, a key that is
|
|
62
|
+
* not a string (a number, a date or an array another page wrote) given as `String` writes it.
|
|
63
|
+
*/
|
|
64
|
+
async keys(prefix) {
|
|
65
|
+
const keys = await this.run("readonly", (store) => store.getAllKeys(IndexedDbKeyValueStore.range(prefix)));
|
|
66
|
+
return keys.map(String).filter((key) => key.startsWith(prefix)).sort();
|
|
67
|
+
}
|
|
68
|
+
/** Removes every key that begins with `prefix`; with an empty prefix, everything the object store holds. */
|
|
69
|
+
async deletePrefix(prefix) {
|
|
70
|
+
const range = IndexedDbKeyValueStore.range(prefix);
|
|
71
|
+
await this.run("readwrite", (store) => range ? store.delete(range) : store.clear());
|
|
72
|
+
}
|
|
73
|
+
/** Closes the database connection. */
|
|
74
|
+
close() {
|
|
75
|
+
this.db.close();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
async function openIndexedDbStore(options) {
|
|
79
|
+
try {
|
|
80
|
+
const db = await new Promise((resolve, reject) => {
|
|
81
|
+
const request = (0, import_idb.idb)().factory.open(options.dbName, options.version ?? 1);
|
|
82
|
+
let answered = false;
|
|
83
|
+
const refuse = (reason) => {
|
|
84
|
+
answered = true;
|
|
85
|
+
reject(reason);
|
|
86
|
+
};
|
|
87
|
+
request.onupgradeneeded = () => {
|
|
88
|
+
if (answered && request.transaction) {
|
|
89
|
+
request.transaction.abort();
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (!request.result.objectStoreNames.contains(options.storeName))
|
|
93
|
+
request.result.createObjectStore(options.storeName);
|
|
94
|
+
};
|
|
95
|
+
request.onsuccess = () => {
|
|
96
|
+
if (answered) {
|
|
97
|
+
request.result.close();
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
answered = true;
|
|
101
|
+
resolve(request.result);
|
|
102
|
+
};
|
|
103
|
+
request.onerror = () => refuse(request.error);
|
|
104
|
+
request.onblocked = () => refuse(new Error("the database is held open by another tab at an older version"));
|
|
105
|
+
});
|
|
106
|
+
return new IndexedDbKeyValueStore(db, options.storeName);
|
|
107
|
+
} catch {
|
|
108
|
+
const memory = new import_keyValueStore.MemoryKeyValueStore();
|
|
109
|
+
return Object.assign(memory, { close: () => void 0 });
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
113
|
+
0 && (module.exports = {
|
|
114
|
+
IndexedDbKeyValueStore,
|
|
115
|
+
openIndexedDbStore
|
|
116
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview A key-value store for a page: values are structured-cloneable, keys are listed and removed by
|
|
3
|
+
* prefix. `namespaced` scopes one to a prefix, which keeps two users of one browser apart when neither's prefix
|
|
4
|
+
* begins with the other's.
|
|
5
|
+
* Moved unchanged from @framers/agentos src/core/storage/browser/KeyValueStore.ts.
|
|
6
|
+
* @module sql-storage-adapter/kv/keyValueStore
|
|
7
|
+
*/
|
|
8
|
+
/** What a page's store does. */
|
|
9
|
+
export interface KeyValueStore {
|
|
10
|
+
/** True when nothing outlives the page (a browser that refused the database). */
|
|
11
|
+
readonly memoryOnly: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* The value held under `key`, or undefined when there is none. The type argument is the caller's word for what
|
|
14
|
+
* it stored: the store checks nothing of what it reads.
|
|
15
|
+
*/
|
|
16
|
+
get<T = unknown>(key: string): Promise<T | undefined>;
|
|
17
|
+
/** Holds `value` under `key`, in place of what was there. The value must be one the structured clone can copy. */
|
|
18
|
+
set(key: string, value: unknown): Promise<void>;
|
|
19
|
+
/** Removes `key`. A key that is not held is no error. */
|
|
20
|
+
delete(key: string): Promise<void>;
|
|
21
|
+
/** Every key that begins with `prefix`, in order. */
|
|
22
|
+
keys(prefix: string): Promise<string[]>;
|
|
23
|
+
/** Removes every key that begins with `prefix`. */
|
|
24
|
+
deletePrefix(prefix: string): Promise<void>;
|
|
25
|
+
}
|
|
26
|
+
/** A store held in memory. Values are copied in and out with `structuredClone`, as IndexedDB copies them. */
|
|
27
|
+
export declare class MemoryKeyValueStore implements KeyValueStore {
|
|
28
|
+
/** Always true: the values go with the page. */
|
|
29
|
+
readonly memoryOnly = true;
|
|
30
|
+
private readonly values;
|
|
31
|
+
/** A copy of the value held under `key`, or undefined. */
|
|
32
|
+
get<T = unknown>(key: string): Promise<T | undefined>;
|
|
33
|
+
/** Holds a copy of `value` under `key`. */
|
|
34
|
+
set(key: string, value: unknown): Promise<void>;
|
|
35
|
+
/** Removes `key`. */
|
|
36
|
+
delete(key: string): Promise<void>;
|
|
37
|
+
/** Every key that begins with `prefix`, in order. */
|
|
38
|
+
keys(prefix: string): Promise<string[]>;
|
|
39
|
+
/** Removes every key that begins with `prefix`. */
|
|
40
|
+
deletePrefix(prefix: string): Promise<void>;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* A store whose keys all begin with `prefix`; its own keys are answered without it. Two namespaces stay apart only
|
|
44
|
+
* when neither prefix begins with the other, so end each with a character no id holds: the keys under `anna` are
|
|
45
|
+
* among those under `ann`, while `ann/` and `anna/` never meet. Each call makes a new store object.
|
|
46
|
+
*/
|
|
47
|
+
export declare function namespaced(store: KeyValueStore, prefix: string): KeyValueStore;
|
|
48
|
+
//# sourceMappingURL=keyValueStore.d.ts.map
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var keyValueStore_exports = {};
|
|
19
|
+
__export(keyValueStore_exports, {
|
|
20
|
+
MemoryKeyValueStore: () => MemoryKeyValueStore,
|
|
21
|
+
namespaced: () => namespaced
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(keyValueStore_exports);
|
|
24
|
+
class MemoryKeyValueStore {
|
|
25
|
+
constructor() {
|
|
26
|
+
this.memoryOnly = true;
|
|
27
|
+
this.values = /* @__PURE__ */ new Map();
|
|
28
|
+
}
|
|
29
|
+
/** A copy of the value held under `key`, or undefined. */
|
|
30
|
+
async get(key) {
|
|
31
|
+
return this.values.has(key) ? structuredClone(this.values.get(key)) : void 0;
|
|
32
|
+
}
|
|
33
|
+
/** Holds a copy of `value` under `key`. */
|
|
34
|
+
async set(key, value) {
|
|
35
|
+
this.values.set(key, structuredClone(value));
|
|
36
|
+
}
|
|
37
|
+
/** Removes `key`. */
|
|
38
|
+
async delete(key) {
|
|
39
|
+
this.values.delete(key);
|
|
40
|
+
}
|
|
41
|
+
/** Every key that begins with `prefix`, in order. */
|
|
42
|
+
async keys(prefix) {
|
|
43
|
+
return [...this.values.keys()].filter((key) => key.startsWith(prefix)).sort();
|
|
44
|
+
}
|
|
45
|
+
/** Removes every key that begins with `prefix`. */
|
|
46
|
+
async deletePrefix(prefix) {
|
|
47
|
+
for (const key of await this.keys(prefix))
|
|
48
|
+
this.values.delete(key);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function namespaced(store, prefix) {
|
|
52
|
+
return {
|
|
53
|
+
get memoryOnly() {
|
|
54
|
+
return store.memoryOnly;
|
|
55
|
+
},
|
|
56
|
+
get: (key) => store.get(prefix + key),
|
|
57
|
+
set: (key, value) => store.set(prefix + key, value),
|
|
58
|
+
delete: (key) => store.delete(prefix + key),
|
|
59
|
+
keys: async (inner) => (await store.keys(prefix + inner)).map((key) => key.slice(prefix.length)),
|
|
60
|
+
deletePrefix: (inner) => store.deletePrefix(prefix + inner)
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
64
|
+
0 && (module.exports = {
|
|
65
|
+
MemoryKeyValueStore,
|
|
66
|
+
namespaced
|
|
67
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview The key-value store over a SQL table, on any adapter: better-sqlite3 and sql.js files, the IndexedDB
|
|
3
|
+
* adapter in a browser, Capacitor on a phone, PostgreSQL on a server. It answers the calls the IndexedDB store answers,
|
|
4
|
+
* and gives back each value as the structured clone does (see `valueCodec`), so a host keeps one key-value contract on
|
|
5
|
+
* every platform.
|
|
6
|
+
* @module sql-storage-adapter/kv/sqlKeyValueStore
|
|
7
|
+
*/
|
|
8
|
+
import type { StorageAdapter } from '../../core/contracts/index.js';
|
|
9
|
+
import type { KeyValueStore } from './keyValueStore.js';
|
|
10
|
+
/** Where the store keeps its keys and values. */
|
|
11
|
+
export interface SqlKeyValueStoreOptions {
|
|
12
|
+
/**
|
|
13
|
+
* The table: lower-case letters, digits and underscores, starting with a letter or an underscore, at most 63
|
|
14
|
+
* characters. It is made, with a `key` and a `value` column, when it is missing.
|
|
15
|
+
*/
|
|
16
|
+
table: string;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Opens the key-value store over `options.table` on `adapter`, making the table when it is missing. The adapter must
|
|
20
|
+
* be open; the store never closes it.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* const db = await createDatabase({ segment: 'agentos' });
|
|
25
|
+
* const kv = await createSqlKeyValueStore(db, { table: 'agentos_kv' });
|
|
26
|
+
* await kv.set('notes/1', { title: 'One', saved: Date.now() });
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @throws {Error} When `options.table` is not a table name the store takes.
|
|
30
|
+
*/
|
|
31
|
+
export declare function createSqlKeyValueStore(adapter: StorageAdapter, options: SqlKeyValueStoreOptions): Promise<KeyValueStore & {
|
|
32
|
+
close(): void;
|
|
33
|
+
}>;
|
|
34
|
+
//# sourceMappingURL=sqlKeyValueStore.d.ts.map
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var sqlKeyValueStore_exports = {};
|
|
19
|
+
__export(sqlKeyValueStore_exports, {
|
|
20
|
+
createSqlKeyValueStore: () => createSqlKeyValueStore
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(sqlKeyValueStore_exports);
|
|
23
|
+
var import_valueCodec = require("./valueCodec.js");
|
|
24
|
+
const TABLE_PATTERN = /^[a-z_][a-z0-9_]*$/;
|
|
25
|
+
const MAX_TABLE_LENGTH = 63;
|
|
26
|
+
const characters = (text) => Array.from(text).length;
|
|
27
|
+
class SqlKeyValueStore {
|
|
28
|
+
constructor(adapter, table) {
|
|
29
|
+
this.adapter = adapter;
|
|
30
|
+
this.table = table;
|
|
31
|
+
this.memoryOnly = !adapter.capabilities.has("persistence");
|
|
32
|
+
}
|
|
33
|
+
/** The value held under `key`, or undefined when there is none. */
|
|
34
|
+
async get(key) {
|
|
35
|
+
const row = await this.adapter.get(`SELECT "value" FROM "${this.table}" WHERE "key" = ?`, [key]);
|
|
36
|
+
return row ? (0, import_valueCodec.decodeValue)(row.value) : void 0;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Holds `value` under `key`, in place of what was there. A value the structured clone refuses is refused with a
|
|
40
|
+
* `DataCloneError` before anything is written, and the key keeps what it held.
|
|
41
|
+
*/
|
|
42
|
+
async set(key, value) {
|
|
43
|
+
const text = (0, import_valueCodec.encodeValue)(value);
|
|
44
|
+
await this.adapter.run(`INSERT INTO "${this.table}" ("key", "value") VALUES (?, ?) ON CONFLICT ("key") DO UPDATE SET "value" = excluded."value"`, [key, text]);
|
|
45
|
+
}
|
|
46
|
+
/** Removes `key`. A key that is not held is no error. */
|
|
47
|
+
async delete(key) {
|
|
48
|
+
await this.adapter.run(`DELETE FROM "${this.table}" WHERE "key" = ?`, [key]);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Every key that begins with `prefix`, in order. The prefix is read literally: `%`, `_` and `*` are characters,
|
|
52
|
+
* not patterns.
|
|
53
|
+
*/
|
|
54
|
+
async keys(prefix) {
|
|
55
|
+
const rows = prefix === "" ? await this.adapter.all(`SELECT "key" FROM "${this.table}"`) : await this.adapter.all(`SELECT "key" FROM "${this.table}" WHERE substr("key", 1, ?) = ?`, [characters(prefix), prefix]);
|
|
56
|
+
return rows.map((row) => row.key).filter((key) => key.startsWith(prefix)).sort();
|
|
57
|
+
}
|
|
58
|
+
/** Removes every key that begins with `prefix`; with an empty prefix, every key of the table. */
|
|
59
|
+
async deletePrefix(prefix) {
|
|
60
|
+
if (prefix === "") {
|
|
61
|
+
await this.adapter.run(`DELETE FROM "${this.table}"`);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
await this.adapter.run(`DELETE FROM "${this.table}" WHERE substr("key", 1, ?) = ?`, [characters(prefix), prefix]);
|
|
65
|
+
}
|
|
66
|
+
/** Does nothing: the adapter is its caller's, who closes it. */
|
|
67
|
+
close() {
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
async function createSqlKeyValueStore(adapter, options) {
|
|
71
|
+
const table = options.table;
|
|
72
|
+
if (typeof table !== "string" || !TABLE_PATTERN.test(table) || table.length > MAX_TABLE_LENGTH) {
|
|
73
|
+
throw new Error(`Invalid table name ${JSON.stringify(table)} for the key-value store: lower-case letters, digits and underscores, starting with a letter or an underscore, at most ${MAX_TABLE_LENGTH} characters.`);
|
|
74
|
+
}
|
|
75
|
+
await adapter.exec(`CREATE TABLE IF NOT EXISTS "${table}" ("key" TEXT PRIMARY KEY NOT NULL, "value" TEXT NOT NULL)`);
|
|
76
|
+
return new SqlKeyValueStore(adapter, table);
|
|
77
|
+
}
|
|
78
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
79
|
+
0 && (module.exports = {
|
|
80
|
+
createSqlKeyValueStore
|
|
81
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview The text the SQL key-value store keeps each value as. A value comes back as the structured clone,
|
|
3
|
+
* which IndexedDB applies to every value it stores, gives it back: the SQL store and the IndexedDB store then answer
|
|
4
|
+
* alike, and a copy from one into the other can be checked value by value.
|
|
5
|
+
*
|
|
6
|
+
* What comes back, as the structured clone gives it (V8's serializer, which Chrome and Node.js run, is the model):
|
|
7
|
+
* strings (lone surrogates too), numbers (`-0`, `NaN` and the infinities too), booleans, `null`, `undefined`,
|
|
8
|
+
* bigints; plain objects and class instances as plain objects with their own enumerable string-keyed properties, in
|
|
9
|
+
* order; arrays with their holes and their own properties; `Date`, `RegExp` (source and flags), `Map`, `Set`,
|
|
10
|
+
* `ArrayBuffer`, the typed arrays and `DataView` (a view keeps its buffer, its offset and its length, and two views of
|
|
11
|
+
* one buffer share it); `Number`, `String`, `Boolean` and `BigInt` objects; errors, as `Error`, `EvalError`,
|
|
12
|
+
* `RangeError`, `ReferenceError`, `SyntaxError`, `TypeError` or `URIError` by their name, with their message, stack
|
|
13
|
+
* and cause. An object reached twice comes back as one object, and a cycle as a cycle.
|
|
14
|
+
*
|
|
15
|
+
* Refused with a `DataCloneError`, as the structured clone refuses them: functions, symbols, `WeakMap`, `WeakSet`,
|
|
16
|
+
* `WeakRef`, promises, iterators, generators, `SharedArrayBuffer`. Refused too, though IndexedDB keeps them: `Blob`,
|
|
17
|
+
* `File`, `ImageData`, `CryptoKey` and the other host objects, which text cannot hold.
|
|
18
|
+
*
|
|
19
|
+
* The text is JSON: it holds no NUL character, which PostgreSQL's text type refuses, and no lone surrogate, which
|
|
20
|
+
* cannot be encoded as UTF-8.
|
|
21
|
+
* @module sql-storage-adapter/kv/valueCodec
|
|
22
|
+
*/
|
|
23
|
+
/**
|
|
24
|
+
* Encodes `value` as text the SQL store keeps.
|
|
25
|
+
*
|
|
26
|
+
* @throws {Error} A `DataCloneError` for a value the structured clone refuses, or one text cannot hold.
|
|
27
|
+
*/
|
|
28
|
+
export declare function encodeValue(value: unknown): string;
|
|
29
|
+
/**
|
|
30
|
+
* Decodes text `encodeValue` wrote.
|
|
31
|
+
*
|
|
32
|
+
* @throws {Error} When the text is not one `encodeValue` writes.
|
|
33
|
+
*/
|
|
34
|
+
export declare function decodeValue(text: string): unknown;
|
|
35
|
+
//# sourceMappingURL=valueCodec.d.ts.map
|