@framers/sql-storage-adapter 0.7.1 → 0.7.3
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 +43 -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 +23 -0
- package/dist/cjs/core/portable.js +66 -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/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 +30 -0
- package/dist/cjs/index.js +101 -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 +23 -0
- package/dist/core/portable.d.ts.map +1 -0
- package/dist/core/portable.js +23 -0
- package/dist/core/portable.js.map +1 -0
- package/dist/features/migrations/schemaMigrations.d.ts +93 -0
- package/dist/features/migrations/schemaMigrations.d.ts.map +1 -0
- package/dist/features/migrations/schemaMigrations.js +216 -0
- package/dist/features/migrations/schemaMigrations.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 +139 -24
|
@@ -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
|