@framers/sql-storage-adapter 0.6.16 → 0.7.0

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.
Files changed (72) hide show
  1. package/README.md +94 -3
  2. package/dist/adapters/betterSqliteAdapter.d.ts +18 -2
  3. package/dist/adapters/betterSqliteAdapter.d.ts.map +1 -1
  4. package/dist/adapters/betterSqliteAdapter.js +13 -5
  5. package/dist/adapters/betterSqliteAdapter.js.map +1 -1
  6. package/dist/adapters/capacitorSqliteAdapter.d.ts +8 -0
  7. package/dist/adapters/capacitorSqliteAdapter.d.ts.map +1 -1
  8. package/dist/adapters/capacitorSqliteAdapter.js +4 -1
  9. package/dist/adapters/capacitorSqliteAdapter.js.map +1 -1
  10. package/dist/adapters/electron/electronMainAdapter.d.ts +10 -0
  11. package/dist/adapters/electron/electronMainAdapter.d.ts.map +1 -1
  12. package/dist/adapters/electron/electronMainAdapter.js +21 -12
  13. package/dist/adapters/electron/electronMainAdapter.js.map +1 -1
  14. package/dist/adapters/indexedDbAdapter.d.ts +198 -54
  15. package/dist/adapters/indexedDbAdapter.d.ts.map +1 -1
  16. package/dist/adapters/indexedDbAdapter.js +549 -129
  17. package/dist/adapters/indexedDbAdapter.js.map +1 -1
  18. package/dist/adapters/postgresAdapter.d.ts +143 -4
  19. package/dist/adapters/postgresAdapter.d.ts.map +1 -1
  20. package/dist/adapters/postgresAdapter.js +612 -107
  21. package/dist/adapters/postgresAdapter.js.map +1 -1
  22. package/dist/adapters/sqlJsAdapter.d.ts +17 -3
  23. package/dist/adapters/sqlJsAdapter.d.ts.map +1 -1
  24. package/dist/adapters/sqlJsAdapter.js +15 -3
  25. package/dist/adapters/sqlJsAdapter.js.map +1 -1
  26. package/dist/core/database.d.ts +28 -8
  27. package/dist/core/database.d.ts.map +1 -1
  28. package/dist/core/database.js +34 -5
  29. package/dist/core/database.js.map +1 -1
  30. package/dist/core/resolver.d.ts +14 -1
  31. package/dist/core/resolver.d.ts.map +1 -1
  32. package/dist/core/resolver.js +28 -8
  33. package/dist/core/resolver.js.map +1 -1
  34. package/dist/features/kv/idb.d.ts +79 -0
  35. package/dist/features/kv/idb.d.ts.map +1 -0
  36. package/dist/features/kv/idb.js +14 -0
  37. package/dist/features/kv/idb.js.map +1 -0
  38. package/dist/features/kv/index.d.ts +14 -0
  39. package/dist/features/kv/index.d.ts.map +1 -0
  40. package/dist/features/kv/index.js +14 -0
  41. package/dist/features/kv/index.js.map +1 -0
  42. package/dist/features/kv/indexedDbKeyValueStore.d.ts +77 -0
  43. package/dist/features/kv/indexedDbKeyValueStore.d.ts.map +1 -0
  44. package/dist/features/kv/indexedDbKeyValueStore.js +127 -0
  45. package/dist/features/kv/indexedDbKeyValueStore.js.map +1 -0
  46. package/dist/features/kv/keyValueStore.d.ts +48 -0
  47. package/dist/features/kv/keyValueStore.d.ts.map +1 -0
  48. package/dist/features/kv/keyValueStore.js +54 -0
  49. package/dist/features/kv/keyValueStore.js.map +1 -0
  50. package/dist/features/kv/sqlKeyValueStore.d.ts +34 -0
  51. package/dist/features/kv/sqlKeyValueStore.d.ts.map +1 -0
  52. package/dist/features/kv/sqlKeyValueStore.js +91 -0
  53. package/dist/features/kv/sqlKeyValueStore.js.map +1 -0
  54. package/dist/features/kv/valueCodec.d.ts +35 -0
  55. package/dist/features/kv/valueCodec.d.ts.map +1 -0
  56. package/dist/features/kv/valueCodec.js +462 -0
  57. package/dist/features/kv/valueCodec.js.map +1 -0
  58. package/dist/features/migrations/dataExport.js +9 -6
  59. package/dist/features/migrations/dataExport.js.map +1 -1
  60. package/dist/index.d.ts +1 -0
  61. package/dist/index.d.ts.map +1 -1
  62. package/dist/index.js +3 -0
  63. package/dist/index.js.map +1 -1
  64. package/dist/shared/postgresTarget.d.ts +4 -3
  65. package/dist/shared/postgresTarget.d.ts.map +1 -1
  66. package/dist/shared/postgresTarget.js +6 -4
  67. package/dist/shared/postgresTarget.js.map +1 -1
  68. package/dist/shared/segment.d.ts +43 -0
  69. package/dist/shared/segment.d.ts.map +1 -0
  70. package/dist/shared/segment.js +77 -0
  71. package/dist/shared/segment.js.map +1 -0
  72. package/package.json +10 -2
@@ -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 @@
1
+ {"version":3,"file":"idb.d.ts","sourceRoot":"","sources":["../../../src/features/kv/idb.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,8CAA8C;AAC9C,MAAM,WAAW,cAAc,CAAC,CAAC;IAC/B,kDAAkD;IAClD,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC;IACnB,qEAAqE;IACrE,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC;IACxB,6CAA6C;IAC7C,SAAS,EAAE,CAAC,MAAM,IAAI,CAAC,GAAG,IAAI,CAAC;IAC/B,0CAA0C;IAC1C,OAAO,EAAE,CAAC,MAAM,IAAI,CAAC,GAAG,IAA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jD,WAAW,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,GAAG,WAAW,GAAG,kBAAkB,CAAC;IACnF,4DAA4D;IAC5D,KAAK,IAAI,IAAI,CAAC;CACf;AAED,kCAAkC;AAClC,MAAM,WAAW,kBAAmB,SAAQ,cAAc,CAAC,eAAe,CAAC;IACzE,+EAA+E;IAC/E,QAAQ,CAAC,WAAW,EAAE;QAAE,KAAK,IAAI,IAAI,CAAA;KAAE,GAAG,IAAI,CAAC;IAC/C,+FAA+F;IAC/F,eAAe,EAAE,CAAC,MAAM,IAAI,CAAC,GAAG,IAAI,CAAC;IACrC,oEAAoE;IACpE,SAAS,EAAE,CAAC,MAAM,IAAI,CAAC,GAAG,IAAI,CAAC;CAChC;AAED,oFAAoF;AACpF,wBAAgB,GAAG,IAAI;IAAE,OAAO,EAAE;QAAE,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,kBAAkB,CAAA;KAAE,CAAC;IAAC,QAAQ,EAAE;QAAE,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,eAAe,CAAA;KAAE,CAAA;CAAE,CAOnK"}
@@ -0,0 +1,14 @@
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
+ /** The page's `indexedDB` and `IDBKeyRange`, or an error when the page has none. */
8
+ export function idb() {
9
+ const scope = globalThis;
10
+ if (!scope.indexedDB || !scope.IDBKeyRange)
11
+ throw new Error('IndexedDB is not available here.');
12
+ return { factory: scope.indexedDB, keyRange: scope.IDBKeyRange };
13
+ }
14
+ //# sourceMappingURL=idb.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"idb.js","sourceRoot":"","sources":["../../../src/features/kv/idb.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAmEH,oFAAoF;AACpF,MAAM,UAAU,GAAG;IACjB,MAAM,KAAK,GAAG,UAGb,CAAC;IACF,IAAI,CAAC,KAAK,CAAC,SAAS,IAAI,CAAC,KAAK,CAAC,WAAW;QAAE,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,CAAC;IAChG,OAAO,EAAE,OAAO,EAAE,KAAK,CAAC,SAAS,EAAE,QAAQ,EAAE,KAAK,CAAC,WAAW,EAAE,CAAC;AACnE,CAAC"}
@@ -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 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/features/kv/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,mBAAmB,EAAE,UAAU,EAAE,KAAK,aAAa,EAAE,MAAM,iBAAiB,CAAC;AACtF,OAAO,EAAE,sBAAsB,EAAE,kBAAkB,EAAE,KAAK,qBAAqB,EAAE,MAAM,0BAA0B,CAAC;AAClH,OAAO,EACL,GAAG,EACH,KAAK,eAAe,EACpB,KAAK,eAAe,EACpB,KAAK,kBAAkB,EACvB,KAAK,kBAAkB,EACvB,KAAK,cAAc,EACnB,KAAK,kBAAkB,GACxB,MAAM,OAAO,CAAC;AACf,OAAO,EAAE,sBAAsB,EAAE,KAAK,uBAAuB,EAAE,MAAM,oBAAoB,CAAC"}
@@ -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 } from './keyValueStore.js';
11
+ export { IndexedDbKeyValueStore, openIndexedDbStore } from './indexedDbKeyValueStore.js';
12
+ export { idb, } from './idb.js';
13
+ export { createSqlKeyValueStore } from './sqlKeyValueStore.js';
14
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/features/kv/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,mBAAmB,EAAE,UAAU,EAAsB,MAAM,iBAAiB,CAAC;AACtF,OAAO,EAAE,sBAAsB,EAAE,kBAAkB,EAA8B,MAAM,0BAA0B,CAAC;AAClH,OAAO,EACL,GAAG,GAOJ,MAAM,OAAO,CAAC;AACf,OAAO,EAAE,sBAAsB,EAAgC,MAAM,oBAAoB,CAAC"}
@@ -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 @@
1
+ {"version":3,"file":"indexedDbKeyValueStore.d.ts","sourceRoot":"","sources":["../../../src/features/kv/indexedDbKeyValueStore.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAO,KAAK,eAAe,EAAsE,MAAM,OAAO,CAAC;AACtH,OAAO,EAAuB,KAAK,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAE1E,6BAA6B;AAC7B,MAAM,WAAW,qBAAqB;IACpC,2BAA2B;IAC3B,MAAM,EAAE,MAAM,CAAC;IACf,4GAA4G;IAC5G,SAAS,EAAE,MAAM,CAAC;IAClB;;;;;OAKG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;;;;;GAUG;AACH,qBAAa,sBAAuB,YAAW,aAAa;IAQ9C,OAAO,CAAC,QAAQ,CAAC,EAAE;IAAmB,OAAO,CAAC,QAAQ,CAAC,SAAS;IAP5E,8DAA8D;IAC9D,QAAQ,CAAC,UAAU,SAAS;IAE5B;;;OAGG;gBAC0B,EAAE,EAAE,eAAe,EAAmB,SAAS,EAAE,MAAM;IAEpF,OAAO,CAAC,GAAG;IAaX,OAAO,CAAC,MAAM,CAAC,KAAK;IAIpB,mEAAmE;IAC7D,GAAG,CAAC,CAAC,GAAG,OAAO,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,SAAS,CAAC;IAI3D,uDAAuD;IACjD,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;IAIrD,qBAAqB;IACf,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAIxC;;;OAGG;IACG,IAAI,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAK7C,4GAA4G;IACtG,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAKjD,sCAAsC;IACtC,KAAK,IAAI,IAAI;CAGd;AAED;;;;;;;;GAQG;AACH,wBAAsB,kBAAkB,CAAC,OAAO,EAAE,qBAAqB,GAAG,OAAO,CAAC,aAAa,GAAG;IAAE,KAAK,IAAI,IAAI,CAAA;CAAE,CAAC,CAoCnH"}
@@ -0,0 +1,127 @@
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 { idb } from './idb.js';
11
+ import { MemoryKeyValueStore } from './keyValueStore.js';
12
+ /**
13
+ * The store over IndexedDB. Each call is one transaction over the object store: its promise is fulfilled when that
14
+ * transaction completes, and rejected with the reason when its request fails or the transaction is aborted.
15
+ *
16
+ * `keys` and `deletePrefix` read the keys between `prefix` and `prefix` followed by U+FFFF, so they miss a key that
17
+ * holds U+FFFF right after the prefix and goes on: keep keys to characters below it.
18
+ *
19
+ * The connection stays open until `close`: when another tab opens the database at a newer version, its upgrade waits
20
+ * for this tab to close the store or go. A connection the browser closes (the database deleted, the site's storage
21
+ * cleared) is not opened again, and every call then rejects.
22
+ */
23
+ export class IndexedDbKeyValueStore {
24
+ /**
25
+ * @param db - An open database that holds the object store.
26
+ * @param storeName - The object store's name.
27
+ */
28
+ constructor(db, storeName) {
29
+ this.db = db;
30
+ this.storeName = storeName;
31
+ /** Always false: the values are in the browser's database. */
32
+ this.memoryOnly = false;
33
+ }
34
+ run(mode, work) {
35
+ return new Promise((resolve, reject) => {
36
+ const transaction = this.db.transaction(this.storeName, mode);
37
+ const request = work(transaction.objectStore(this.storeName));
38
+ transaction.oncomplete = () => resolve(request.result);
39
+ // A failed request's event reaches the transaction before the transaction is aborted for it, so the
40
+ // transaction's own error is still null then, and the request's says why. A transaction aborted for a reason
41
+ // of its own has that reason set by the time its unfinished request's event arrives.
42
+ transaction.onerror = () => reject(transaction.error ?? request.error ?? new Error('IndexedDB request failed'));
43
+ transaction.onabort = () => reject(transaction.error ?? new Error('IndexedDB transaction aborted'));
44
+ });
45
+ }
46
+ static range(prefix) {
47
+ return prefix === '' ? undefined : idb().keyRange.bound(prefix, `${prefix}\uffff`);
48
+ }
49
+ /** The value held under `key`, or undefined when there is none. */
50
+ async get(key) {
51
+ return (await this.run('readonly', (store) => store.get(key)));
52
+ }
53
+ /** Holds a structured clone of `value` under `key`. */
54
+ async set(key, value) {
55
+ await this.run('readwrite', (store) => store.put(value, key));
56
+ }
57
+ /** Removes `key`. */
58
+ async delete(key) {
59
+ await this.run('readwrite', (store) => store.delete(key));
60
+ }
61
+ /**
62
+ * Every key that begins with `prefix`, in order. With an empty prefix, every key of the object store, a key that is
63
+ * not a string (a number, a date or an array another page wrote) given as `String` writes it.
64
+ */
65
+ async keys(prefix) {
66
+ const keys = await this.run('readonly', (store) => store.getAllKeys(IndexedDbKeyValueStore.range(prefix)));
67
+ return keys.map(String).filter((key) => key.startsWith(prefix)).sort();
68
+ }
69
+ /** Removes every key that begins with `prefix`; with an empty prefix, everything the object store holds. */
70
+ async deletePrefix(prefix) {
71
+ const range = IndexedDbKeyValueStore.range(prefix);
72
+ await this.run('readwrite', (store) => (range ? store.delete(range) : store.clear()));
73
+ }
74
+ /** Closes the database connection. */
75
+ close() {
76
+ this.db.close();
77
+ }
78
+ }
79
+ /**
80
+ * Opens the store, or the memory store when the browser refuses IndexedDB: a page with no `indexedDB`, an `open`
81
+ * that throws or fails (a database above `version` among them), or an upgrade that waits on another tab. The
82
+ * answer's `memoryOnly` says which it is; the memory store's `close` does nothing.
83
+ *
84
+ * An upgrade that waited on another tab still has its turn once that tab closes the database. It is aborted then,
85
+ * so the database stays as it was and the connection it opened is closed: nothing is left open behind the memory
86
+ * store.
87
+ */
88
+ export async function openIndexedDbStore(options) {
89
+ try {
90
+ const db = await new Promise((resolve, reject) => {
91
+ const request = idb().factory.open(options.dbName, options.version ?? 1);
92
+ // True once the promise is settled. The browser does not end a blocked open: it goes on when the other tab
93
+ // closes the database, after the store has answered from memory, and what it opens then is nobody's.
94
+ let answered = false;
95
+ const refuse = (reason) => {
96
+ answered = true;
97
+ reject(reason);
98
+ };
99
+ request.onupgradeneeded = () => {
100
+ if (answered && request.transaction) {
101
+ // Aborting the upgrade fails the open, which closes its connection and leaves the database as it was.
102
+ request.transaction.abort();
103
+ return;
104
+ }
105
+ if (!request.result.objectStoreNames.contains(options.storeName))
106
+ request.result.createObjectStore(options.storeName);
107
+ };
108
+ request.onsuccess = () => {
109
+ if (answered) {
110
+ // An open that succeeds after the answer (an upgrade that could not be aborted) is closed at once.
111
+ request.result.close();
112
+ return;
113
+ }
114
+ answered = true;
115
+ resolve(request.result);
116
+ };
117
+ request.onerror = () => refuse(request.error);
118
+ request.onblocked = () => refuse(new Error('the database is held open by another tab at an older version'));
119
+ });
120
+ return new IndexedDbKeyValueStore(db, options.storeName);
121
+ }
122
+ catch {
123
+ const memory = new MemoryKeyValueStore();
124
+ return Object.assign(memory, { close: () => undefined });
125
+ }
126
+ }
127
+ //# sourceMappingURL=indexedDbKeyValueStore.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"indexedDbKeyValueStore.js","sourceRoot":"","sources":["../../../src/features/kv/indexedDbKeyValueStore.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,GAAG,EAA4F,MAAM,OAAO,CAAC;AACtH,OAAO,EAAE,mBAAmB,EAAsB,MAAM,iBAAiB,CAAC;AAiB1E;;;;;;;;;;GAUG;AACH,MAAM,OAAO,sBAAsB;IAIjC;;;OAGG;IACH,YAA6B,EAAmB,EAAmB,SAAiB;QAAvD,OAAE,GAAF,EAAE,CAAiB;QAAmB,cAAS,GAAT,SAAS,CAAQ;QAPpF,8DAA8D;QACrD,eAAU,GAAG,KAAK,CAAC;IAM2D,CAAC;IAEhF,GAAG,CAAI,IAA8B,EAAE,IAAsD;QACnG,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACrC,MAAM,WAAW,GAAG,IAAI,CAAC,EAAE,CAAC,WAAW,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;YAC9D,MAAM,OAAO,GAAG,IAAI,CAAC,WAAW,CAAC,WAAW,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC;YAC9D,WAAW,CAAC,UAAU,GAAG,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;YACvD,oGAAoG;YACpG,6GAA6G;YAC7G,qFAAqF;YACrF,WAAW,CAAC,OAAO,GAAG,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,GAAG,EAAE;gBAC7B,IAAI,QAAQ,IAAI,OAAO,CAAC,WAAW,EAAE,CAAC;oBACpC,sGAAsG;oBACtG,OAAO,CAAC,WAAW,CAAC,KAAK,EAAE,CAAC;oBAC5B,OAAO;gBACT,CAAC;gBACD,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,gBAAgB,CAAC,QAAQ,CAAC,OAAO,CAAC,SAAS,CAAC;oBAAE,OAAO,CAAC,MAAM,CAAC,iBAAiB,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;YACxH,CAAC,CAAC;YACF,OAAO,CAAC,SAAS,GAAG,GAAG,EAAE;gBACvB,IAAI,QAAQ,EAAE,CAAC;oBACb,mGAAmG;oBACnG,OAAO,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;oBACvB,OAAO;gBACT,CAAC;gBACD,QAAQ,GAAG,IAAI,CAAC;gBAChB,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;YAC1B,CAAC,CAAC;YACF,OAAO,CAAC,OAAO,GAAG,GAAG,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;YAC9C,OAAO,CAAC,SAAS,GAAG,GAAG,EAAE,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,8DAA8D,CAAC,CAAC,CAAC;QAC9G,CAAC,CAAC,CAAC;QACH,OAAO,IAAI,sBAAsB,CAAC,EAAE,EAAE,OAAO,CAAC,SAAS,CAAC,CAAC;IAC3D,CAAC;IAAC,MAAM,CAAC;QACP,MAAM,MAAM,GAAG,IAAI,mBAAmB,EAAE,CAAC;QACzC,OAAO,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,SAAS,EAAE,CAAC,CAAC;IAC3D,CAAC;AACH,CAAC"}
@@ -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
+ //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MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAI7C,mDAAmD;IAC7C,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CAGlD;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,aAAa,EAAE,MAAM,EAAE,MAAM,GAAG,aAAa,CAW9E"}
@@ -0,0 +1,54 @@
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
+ /** A store held in memory. Values are copied in and out with `structuredClone`, as IndexedDB copies them. */
9
+ export class MemoryKeyValueStore {
10
+ constructor() {
11
+ /** Always true: the values go with the page. */
12
+ this.memoryOnly = true;
13
+ this.values = new Map();
14
+ }
15
+ /** A copy of the value held under `key`, or undefined. */
16
+ async get(key) {
17
+ return this.values.has(key) ? structuredClone(this.values.get(key)) : undefined;
18
+ }
19
+ /** Holds a copy of `value` under `key`. */
20
+ async set(key, value) {
21
+ this.values.set(key, structuredClone(value));
22
+ }
23
+ /** Removes `key`. */
24
+ async delete(key) {
25
+ this.values.delete(key);
26
+ }
27
+ /** Every key that begins with `prefix`, in order. */
28
+ async keys(prefix) {
29
+ return [...this.values.keys()].filter((key) => key.startsWith(prefix)).sort();
30
+ }
31
+ /** Removes every key that begins with `prefix`. */
32
+ async deletePrefix(prefix) {
33
+ for (const key of await this.keys(prefix))
34
+ this.values.delete(key);
35
+ }
36
+ }
37
+ /**
38
+ * A store whose keys all begin with `prefix`; its own keys are answered without it. Two namespaces stay apart only
39
+ * when neither prefix begins with the other, so end each with a character no id holds: the keys under `anna` are
40
+ * among those under `ann`, while `ann/` and `anna/` never meet. Each call makes a new store object.
41
+ */
42
+ export function namespaced(store, prefix) {
43
+ return {
44
+ get memoryOnly() {
45
+ return store.memoryOnly;
46
+ },
47
+ get: (key) => store.get(prefix + key),
48
+ set: (key, value) => store.set(prefix + key, value),
49
+ delete: (key) => store.delete(prefix + key),
50
+ keys: async (inner) => (await store.keys(prefix + inner)).map((key) => key.slice(prefix.length)),
51
+ deletePrefix: (inner) => store.deletePrefix(prefix + inner),
52
+ };
53
+ }
54
+ //# sourceMappingURL=keyValueStore.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"keyValueStore.js","sourceRoot":"","sources":["../../../src/features/kv/keyValueStore.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAqBH,6GAA6G;AAC7G,MAAM,OAAO,mBAAmB;IAAhC;QACE,gDAAgD;QACvC,eAAU,GAAG,IAAI,CAAC;QACV,WAAM,GAAG,IAAI,GAAG,EAAmB,CAAC;IA0BvD,CAAC;IAxBC,0DAA0D;IAC1D,KAAK,CAAC,GAAG,CAAc,GAAW;QAChC,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAE,eAAe,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAO,CAAC,CAAC,CAAC,SAAS,CAAC;IACzF,CAAC;IAED,2CAA2C;IAC3C,KAAK,CAAC,GAAG,CAAC,GAAW,EAAE,KAAc;QACnC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,eAAe,CAAC,KAAK,CAAC,CAAC,CAAC;IAC/C,CAAC;IAED,qBAAqB;IACrB,KAAK,CAAC,MAAM,CAAC,GAAW;QACtB,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IAC1B,CAAC;IAED,qDAAqD;IACrD,KAAK,CAAC,IAAI,CAAC,MAAc;QACvB,OAAO,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;IAChF,CAAC;IAED,mDAAmD;IACnD,KAAK,CAAC,YAAY,CAAC,MAAc;QAC/B,KAAK,MAAM,GAAG,IAAI,MAAM,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC;YAAE,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IACrE,CAAC;CACF;AAED;;;;GAIG;AACH,MAAM,UAAU,UAAU,CAAC,KAAoB,EAAE,MAAc;IAC7D,OAAO;QACL,IAAI,UAAU;YACZ,OAAO,KAAK,CAAC,UAAU,CAAC;QAC1B,CAAC;QACD,GAAG,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,GAAG,GAAG,CAAC;QACrC,GAAG,EAAE,CAAC,GAAG,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,GAAG,GAAG,EAAE,KAAK,CAAC;QACnD,MAAM,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,GAAG,GAAG,CAAC;QAC3C,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC,MAAM,KAAK,CAAC,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QAChG,YAAY,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,YAAY,CAAC,MAAM,GAAG,KAAK,CAAC;KAC5D,CAAC;AACJ,CAAC"}
@@ -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 @@
1
+ {"version":3,"file":"sqlKeyValueStore.d.ts","sourceRoot":"","sources":["../../../src/features/kv/sqlKeyValueStore.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAC3D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAGrD,iDAAiD;AACjD,MAAM,WAAW,uBAAuB;IACtC;;;OAGG;IACH,KAAK,EAAE,MAAM,CAAC;CACf;AAmFD;;;;;;;;;;;;GAYG;AACH,wBAAsB,sBAAsB,CAC1C,OAAO,EAAE,cAAc,EACvB,OAAO,EAAE,uBAAuB,GAC/B,OAAO,CAAC,aAAa,GAAG;IAAE,KAAK,IAAI,IAAI,CAAA;CAAE,CAAC,CAU5C"}
@@ -0,0 +1,91 @@
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 { decodeValue, encodeValue } from './valueCodec.js';
9
+ /** A table name the store takes: an unquoted PostgreSQL identifier, which SQLite takes too. */
10
+ const TABLE_PATTERN = /^[a-z_][a-z0-9_]*$/;
11
+ /** PostgreSQL's identifier limit. */
12
+ const MAX_TABLE_LENGTH = 63;
13
+ /** The length of `text` in characters (code points), as SQLite's and PostgreSQL's `substr` count them. */
14
+ const characters = (text) => Array.from(text).length;
15
+ /**
16
+ * The store over one table. Each call is one statement on the adapter, so it is atomic, and runs in the adapter's
17
+ * transaction rules: through a transaction's handle it joins that transaction.
18
+ *
19
+ * Keys are text and are listed in the order of their UTF-16 code units, as IndexedDB orders them, whatever the
20
+ * database's collation. On PostgreSQL a key cannot hold the NUL character, which its text type refuses.
21
+ */
22
+ class SqlKeyValueStore {
23
+ constructor(adapter, table) {
24
+ this.adapter = adapter;
25
+ this.table = table;
26
+ this.memoryOnly = !adapter.capabilities.has('persistence');
27
+ }
28
+ /** The value held under `key`, or undefined when there is none. */
29
+ async get(key) {
30
+ const row = await this.adapter.get(`SELECT "value" FROM "${this.table}" WHERE "key" = ?`, [key]);
31
+ return row ? decodeValue(row.value) : undefined;
32
+ }
33
+ /**
34
+ * Holds `value` under `key`, in place of what was there. A value the structured clone refuses is refused with a
35
+ * `DataCloneError` before anything is written, and the key keeps what it held.
36
+ */
37
+ async set(key, value) {
38
+ const text = encodeValue(value);
39
+ await this.adapter.run(`INSERT INTO "${this.table}" ("key", "value") VALUES (?, ?) ON CONFLICT ("key") DO UPDATE SET "value" = excluded."value"`, [key, text]);
40
+ }
41
+ /** Removes `key`. A key that is not held is no error. */
42
+ async delete(key) {
43
+ await this.adapter.run(`DELETE FROM "${this.table}" WHERE "key" = ?`, [key]);
44
+ }
45
+ /**
46
+ * Every key that begins with `prefix`, in order. The prefix is read literally: `%`, `_` and `*` are characters,
47
+ * not patterns.
48
+ */
49
+ async keys(prefix) {
50
+ const rows = prefix === ''
51
+ ? await this.adapter.all(`SELECT "key" FROM "${this.table}"`)
52
+ : await this.adapter.all(`SELECT "key" FROM "${this.table}" WHERE substr("key", 1, ?) = ?`, [characters(prefix), prefix]);
53
+ return rows
54
+ .map((row) => row.key)
55
+ .filter((key) => key.startsWith(prefix))
56
+ .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
+ * Opens the key-value store over `options.table` on `adapter`, making the table when it is missing. The adapter must
71
+ * be open; the store never closes it.
72
+ *
73
+ * @example
74
+ * ```typescript
75
+ * const db = await createDatabase({ segment: 'agentos' });
76
+ * const kv = await createSqlKeyValueStore(db, { table: 'agentos_kv' });
77
+ * await kv.set('notes/1', { title: 'One', saved: Date.now() });
78
+ * ```
79
+ *
80
+ * @throws {Error} When `options.table` is not a table name the store takes.
81
+ */
82
+ export async function createSqlKeyValueStore(adapter, options) {
83
+ const table = options.table;
84
+ if (typeof table !== 'string' || !TABLE_PATTERN.test(table) || table.length > MAX_TABLE_LENGTH) {
85
+ throw new Error(`Invalid table name ${JSON.stringify(table)} for the key-value store: lower-case letters, digits and underscores, ` +
86
+ `starting with a letter or an underscore, at most ${MAX_TABLE_LENGTH} characters.`);
87
+ }
88
+ await adapter.exec(`CREATE TABLE IF NOT EXISTS "${table}" ("key" TEXT PRIMARY KEY NOT NULL, "value" TEXT NOT NULL)`);
89
+ return new SqlKeyValueStore(adapter, table);
90
+ }
91
+ //# sourceMappingURL=sqlKeyValueStore.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sqlKeyValueStore.js","sourceRoot":"","sources":["../../../src/features/kv/sqlKeyValueStore.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAWxD,+FAA+F;AAC/F,MAAM,aAAa,GAAG,oBAAoB,CAAC;AAE3C,qCAAqC;AACrC,MAAM,gBAAgB,GAAG,EAAE,CAAC;AAE5B,0GAA0G;AAC1G,MAAM,UAAU,GAAG,CAAC,IAAY,EAAU,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC;AAErE;;;;;;GAMG;AACH,MAAM,gBAAgB;IAIpB,YACmB,OAAuB,EACvB,KAAa;QADb,YAAO,GAAP,OAAO,CAAgB;QACvB,UAAK,GAAL,KAAK,CAAQ;QAE9B,IAAI,CAAC,UAAU,GAAG,CAAC,OAAO,CAAC,YAAY,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;IAC7D,CAAC;IAED,mEAAmE;IACnE,KAAK,CAAC,GAAG,CAAc,GAAW;QAChC,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,GAAG,CAAoB,wBAAwB,IAAI,CAAC,KAAK,mBAAmB,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;QACpH,OAAO,GAAG,CAAC,CAAC,CAAE,WAAW,CAAC,GAAG,CAAC,KAAK,CAAO,CAAC,CAAC,CAAC,SAAS,CAAC;IACzD,CAAC;IAED;;;OAGG;IACH,KAAK,CAAC,GAAG,CAAC,GAAW,EAAE,KAAc;QAC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