@aztec/kv-store 0.0.1-commit.8c0b8ff → 0.0.1-commit.8cb2d04d8

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 (58) hide show
  1. package/dest/bench/shared_map_bench.d.ts +19 -0
  2. package/dest/bench/shared_map_bench.d.ts.map +1 -0
  3. package/dest/bench/shared_map_bench.js +91 -0
  4. package/dest/indexeddb/index.d.ts +2 -2
  5. package/dest/indexeddb/index.d.ts.map +1 -1
  6. package/dest/lmdb/index.d.ts +2 -2
  7. package/dest/lmdb/index.d.ts.map +1 -1
  8. package/dest/lmdb/store.d.ts +3 -3
  9. package/dest/lmdb/store.d.ts.map +1 -1
  10. package/dest/lmdb/store.js +12 -8
  11. package/dest/lmdb-v2/factory.d.ts +2 -2
  12. package/dest/lmdb-v2/factory.d.ts.map +1 -1
  13. package/dest/sqlite-opfs/array.d.ts +21 -0
  14. package/dest/sqlite-opfs/array.d.ts.map +1 -0
  15. package/dest/sqlite-opfs/array.js +128 -0
  16. package/dest/sqlite-opfs/index.d.ts +7 -0
  17. package/dest/sqlite-opfs/index.d.ts.map +1 -0
  18. package/dest/sqlite-opfs/index.js +13 -0
  19. package/dest/sqlite-opfs/map.d.ts +35 -0
  20. package/dest/sqlite-opfs/map.d.ts.map +1 -0
  21. package/dest/sqlite-opfs/map.js +163 -0
  22. package/dest/sqlite-opfs/messages.d.ts +58 -0
  23. package/dest/sqlite-opfs/messages.d.ts.map +1 -0
  24. package/dest/sqlite-opfs/messages.js +5 -0
  25. package/dest/sqlite-opfs/multi_map.d.ts +16 -0
  26. package/dest/sqlite-opfs/multi_map.d.ts.map +1 -0
  27. package/dest/sqlite-opfs/multi_map.js +67 -0
  28. package/dest/sqlite-opfs/set.d.ts +13 -0
  29. package/dest/sqlite-opfs/set.d.ts.map +1 -0
  30. package/dest/sqlite-opfs/set.js +19 -0
  31. package/dest/sqlite-opfs/singleton.d.ts +13 -0
  32. package/dest/sqlite-opfs/singleton.d.ts.map +1 -0
  33. package/dest/sqlite-opfs/singleton.js +48 -0
  34. package/dest/sqlite-opfs/store.d.ts +70 -0
  35. package/dest/sqlite-opfs/store.d.ts.map +1 -0
  36. package/dest/sqlite-opfs/store.js +242 -0
  37. package/dest/sqlite-opfs/worker.d.ts +2 -0
  38. package/dest/sqlite-opfs/worker.d.ts.map +1 -0
  39. package/dest/sqlite-opfs/worker.js +189 -0
  40. package/package.json +10 -8
  41. package/src/bench/shared_map_bench.ts +111 -0
  42. package/src/indexeddb/index.ts +1 -1
  43. package/src/lmdb/index.ts +1 -1
  44. package/src/lmdb/store.ts +12 -8
  45. package/src/lmdb-v2/factory.ts +1 -1
  46. package/src/sqlite-opfs/array.ts +124 -0
  47. package/src/sqlite-opfs/index.ts +27 -0
  48. package/src/sqlite-opfs/map.ts +163 -0
  49. package/src/sqlite-opfs/messages.ts +28 -0
  50. package/src/sqlite-opfs/multi_map.ts +74 -0
  51. package/src/sqlite-opfs/set.ts +29 -0
  52. package/src/sqlite-opfs/singleton.ts +48 -0
  53. package/src/sqlite-opfs/store.ts +248 -0
  54. package/src/sqlite-opfs/worker.ts +157 -0
  55. package/dest/config.d.ts +0 -17
  56. package/dest/config.d.ts.map +0 -1
  57. package/dest/config.js +0 -26
  58. package/src/config.ts +0 -36
@@ -0,0 +1,124 @@
1
+ import { Encoder } from 'msgpackr';
2
+ import { hash } from 'ohash';
3
+ import { toBufferKey } from 'ordered-binary';
4
+
5
+ import type { AztecAsyncArray } from '../interfaces/array.js';
6
+ import type { Value } from '../interfaces/common.js';
7
+ import type { AztecSQLiteOPFSStore } from './store.js';
8
+
9
+ /**
10
+ * Persistent array backed by SQLite. Entries share a common `key` (the array name)
11
+ * and are ordered by `key_count`, which doubles as the 1-indexed slot number.
12
+ */
13
+ export class SQLiteOPFSAztecArray<T extends Value> implements AztecAsyncArray<T> {
14
+ readonly #name: string;
15
+ readonly #container: string;
16
+ readonly #encoder = new Encoder();
17
+
18
+ constructor(
19
+ private readonly store: AztecSQLiteOPFSStore,
20
+ name: string,
21
+ ) {
22
+ this.#name = name;
23
+ this.#container = `array:${name}`;
24
+ }
25
+
26
+ async lengthAsync(): Promise<number> {
27
+ const rows = await this.store.allAsync('SELECT COUNT(*) FROM data WHERE container = ? AND key = ?', [
28
+ this.#container,
29
+ this.#encodedKey(),
30
+ ]);
31
+ return Number(rows[0]?.[0] ?? 0);
32
+ }
33
+
34
+ async push(...vals: T[]): Promise<number> {
35
+ if (vals.length === 0) {
36
+ return this.lengthAsync();
37
+ }
38
+ return await this.store.transactionAsync(async () => {
39
+ let length = await this.lengthAsync();
40
+ for (const val of vals) {
41
+ await this.store.runAsync(
42
+ `INSERT INTO data (slot, container, key, key_count, hash, value)
43
+ VALUES (?, ?, ?, ?, ?, ?)`,
44
+ [this.#slot(length), this.#container, this.#encodedKey(), length + 1, hash(val), this.#encoder.pack(val)],
45
+ );
46
+ length += 1;
47
+ }
48
+ return length;
49
+ });
50
+ }
51
+
52
+ async pop(): Promise<T | undefined> {
53
+ return await this.store.transactionAsync(async () => {
54
+ const length = await this.lengthAsync();
55
+ if (length === 0) {
56
+ return undefined;
57
+ }
58
+ const slot = this.#slot(length - 1);
59
+ const rows = await this.store.allAsync('SELECT value FROM data WHERE slot = ? LIMIT 1', [slot]);
60
+ await this.store.runAsync('DELETE FROM data WHERE slot = ?', [slot]);
61
+ const raw = rows[0]?.[0];
62
+ return raw instanceof Uint8Array ? (this.#encoder.unpack(raw) as T) : undefined;
63
+ });
64
+ }
65
+
66
+ async atAsync(index: number): Promise<T | undefined> {
67
+ const length = await this.lengthAsync();
68
+ const resolved = index < 0 ? length + index : index;
69
+ if (resolved < 0 || resolved >= length) {
70
+ return undefined;
71
+ }
72
+ const rows = await this.store.allAsync('SELECT value FROM data WHERE slot = ? LIMIT 1', [this.#slot(resolved)]);
73
+ const raw = rows[0]?.[0];
74
+ return raw instanceof Uint8Array ? (this.#encoder.unpack(raw) as T) : undefined;
75
+ }
76
+
77
+ async setAt(index: number, val: T): Promise<boolean> {
78
+ return await this.store.transactionAsync(async () => {
79
+ const length = await this.lengthAsync();
80
+ const resolved = index < 0 ? length + index : index;
81
+ if (resolved < 0 || resolved >= length) {
82
+ return false;
83
+ }
84
+ await this.store.runAsync(
85
+ `INSERT OR REPLACE INTO data (slot, container, key, key_count, hash, value)
86
+ VALUES (?, ?, ?, ?, ?, ?)`,
87
+ [this.#slot(resolved), this.#container, this.#encodedKey(), resolved + 1, hash(val), this.#encoder.pack(val)],
88
+ );
89
+ return true;
90
+ });
91
+ }
92
+
93
+ async *entriesAsync(): AsyncIterableIterator<[number, T]> {
94
+ const rows = await this.store.allAsync(
95
+ 'SELECT key_count, value FROM data WHERE container = ? AND key = ? ORDER BY key_count ASC',
96
+ [this.#container, this.#encodedKey()],
97
+ );
98
+ for (const row of rows) {
99
+ const keyCount = Number(row[0]);
100
+ const raw = row[1];
101
+ if (raw instanceof Uint8Array) {
102
+ yield [keyCount - 1, this.#encoder.unpack(raw) as T];
103
+ }
104
+ }
105
+ }
106
+
107
+ async *valuesAsync(): AsyncIterableIterator<T> {
108
+ for await (const [, val] of this.entriesAsync()) {
109
+ yield val;
110
+ }
111
+ }
112
+
113
+ [Symbol.asyncIterator](): AsyncIterableIterator<T> {
114
+ return this.valuesAsync();
115
+ }
116
+
117
+ #encodedKey(): Buffer {
118
+ return toBufferKey([this.#name]);
119
+ }
120
+
121
+ #slot(index: number): string {
122
+ return `array:${this.#name}:slot:${index}`;
123
+ }
124
+ }
@@ -0,0 +1,27 @@
1
+ import { type Logger, createLogger } from '@aztec/foundation/log';
2
+ import type { DataStoreConfig } from '@aztec/stdlib/kv-store';
3
+
4
+ import { initStoreForRollupAndSchemaVersion } from '../utils.js';
5
+ import { AztecSQLiteOPFSStore } from './store.js';
6
+
7
+ export { AztecSQLiteOPFSStore } from './store.js';
8
+
9
+ export async function createStore(
10
+ name: string,
11
+ config: DataStoreConfig,
12
+ schemaVersion: number | undefined = undefined,
13
+ log: Logger = createLogger('kv-store'),
14
+ ) {
15
+ const { dataDirectory } = config;
16
+ log.info(
17
+ dataDirectory
18
+ ? `Creating ${name} SQLite-OPFS data store with map size ${config.dataStoreMapSizeKb} KB`
19
+ : `Creating ${name} ephemeral SQLite-OPFS data store with map size ${config.dataStoreMapSizeKb} KB`,
20
+ );
21
+ const store = await AztecSQLiteOPFSStore.open(createLogger('kv-store:sqlite-opfs'), name, false);
22
+ return initStoreForRollupAndSchemaVersion(store, schemaVersion, config.l1Contracts?.rollupAddress, log);
23
+ }
24
+
25
+ export function openTmpStore(ephemeral: boolean = false): Promise<AztecSQLiteOPFSStore> {
26
+ return AztecSQLiteOPFSStore.open(createLogger('kv-store:sqlite-opfs'), undefined, ephemeral);
27
+ }
@@ -0,0 +1,163 @@
1
+ import { Buffer } from 'buffer';
2
+ import { Encoder } from 'msgpackr';
3
+ import { hash } from 'ohash';
4
+ import { fromBufferKey, toBufferKey } from 'ordered-binary';
5
+
6
+ import type { Key, Range, Value } from '../interfaces/common.js';
7
+ import type { AztecAsyncMap } from '../interfaces/map.js';
8
+ import type { SqlValue } from './messages.js';
9
+ import type { AztecSQLiteOPFSStore } from './store.js';
10
+
11
+ /** A map backed by SQLite in OPFS. Mirrors `IndexedDBAztecMap`. */
12
+ export class SQLiteOPFSAztecMap<K extends Key, V extends Value> implements AztecAsyncMap<K, V> {
13
+ protected readonly name: string;
14
+ protected readonly container: string;
15
+ protected readonly encoder = new Encoder();
16
+
17
+ constructor(
18
+ protected readonly store: AztecSQLiteOPFSStore,
19
+ mapName: string,
20
+ ) {
21
+ this.name = mapName;
22
+ this.container = `map:${mapName}`;
23
+ }
24
+
25
+ async getAsync(key: K): Promise<V | undefined> {
26
+ const rows = await this.store.allAsync('SELECT value FROM data WHERE slot = ? LIMIT 1', [this.slot(key)]);
27
+ if (rows.length === 0) {
28
+ return undefined;
29
+ }
30
+ const raw = rows[0][0];
31
+ return raw == null ? undefined : this.decodeValue(raw);
32
+ }
33
+
34
+ async hasAsync(key: K): Promise<boolean> {
35
+ const rows = await this.store.allAsync('SELECT 1 FROM data WHERE slot = ? LIMIT 1', [this.slot(key)]);
36
+ return rows.length > 0;
37
+ }
38
+
39
+ async sizeAsync(): Promise<number> {
40
+ const rows = await this.store.allAsync('SELECT COUNT(*) FROM data WHERE container = ?', [this.container]);
41
+ return Number(rows[0]?.[0] ?? 0);
42
+ }
43
+
44
+ async set(key: K, val: V): Promise<void> {
45
+ await this.store.runAsync(
46
+ `INSERT OR REPLACE INTO data (slot, container, key, key_count, hash, value)
47
+ VALUES (?, ?, ?, ?, ?, ?)`,
48
+ [this.slot(key), this.container, this.encodedKey(key), 1, hash(val), this.encoder.pack(val)],
49
+ );
50
+ }
51
+
52
+ async setMany(entries: { key: K; value: V }[]): Promise<void> {
53
+ if (entries.length === 0) {
54
+ return;
55
+ }
56
+ await this.store.transactionAsync(async () => {
57
+ for (const { key, value } of entries) {
58
+ await this.set(key, value);
59
+ }
60
+ });
61
+ }
62
+
63
+ swap(_key: K, _fn: (val: V | undefined) => V): Promise<void> {
64
+ throw new Error('Not implemented');
65
+ }
66
+
67
+ async setIfNotExists(key: K, val: V): Promise<boolean> {
68
+ return await this.store.transactionAsync(async () => {
69
+ if (await this.hasAsync(key)) {
70
+ return false;
71
+ }
72
+ await this.set(key, val);
73
+ return true;
74
+ });
75
+ }
76
+
77
+ async delete(key: K): Promise<void> {
78
+ await this.store.runAsync('DELETE FROM data WHERE slot = ?', [this.slot(key)]);
79
+ }
80
+
81
+ async *entriesAsync(range: Range<K> = {}): AsyncIterableIterator<[K, V]> {
82
+ const rows = await this.rangeQuery(range);
83
+ for (const row of rows) {
84
+ const [_slot, keyBlob, value] = row;
85
+ if (keyBlob == null || value == null) {
86
+ continue;
87
+ }
88
+ yield [this.decodeKey(keyBlob), this.decodeValue(value)];
89
+ }
90
+ }
91
+
92
+ async *valuesAsync(range: Range<K> = {}): AsyncIterableIterator<V> {
93
+ for await (const [, value] of this.entriesAsync(range)) {
94
+ yield value;
95
+ }
96
+ }
97
+
98
+ async *keysAsync(range: Range<K> = {}): AsyncIterableIterator<K> {
99
+ for await (const [key] of this.entriesAsync(range)) {
100
+ yield key;
101
+ }
102
+ }
103
+
104
+ protected async rangeQuery(range: Range<K>): Promise<Array<[string, Uint8Array, Uint8Array | null]>> {
105
+ // Inclusivity flips with direction to match the IndexedDB backend:
106
+ // forward: [start, end) reverse: (start, end]
107
+ // That asymmetry is load-bearing — tests pin the exact inclusivity at boundaries.
108
+ const reverse = !!range.reverse;
109
+ const parts: string[] = ['container = ?'];
110
+ const bind: SqlValue[] = [this.container];
111
+ if (range.start !== undefined) {
112
+ parts.push(reverse ? 'key > ?' : 'key >= ?');
113
+ bind.push(this.encodedKey(range.start));
114
+ }
115
+ if (range.end !== undefined) {
116
+ parts.push(reverse ? 'key <= ?' : 'key < ?');
117
+ bind.push(this.encodedKey(range.end));
118
+ }
119
+ const order = reverse ? 'DESC' : 'ASC';
120
+ let sql = `SELECT slot, key, value FROM data WHERE ${parts.join(' AND ')} ORDER BY key ${order}, key_count ${order}`;
121
+ if (range.limit !== undefined) {
122
+ sql += ' LIMIT ?';
123
+ bind.push(range.limit);
124
+ }
125
+ const rows = await this.store.allAsync(sql, bind);
126
+ return rows.map(r => [String(r[0]), r[1] as Uint8Array, r[2] as Uint8Array | null]);
127
+ }
128
+
129
+ protected decodeValue(val: SqlValue): V {
130
+ if (!(val instanceof Uint8Array)) {
131
+ return val as V;
132
+ }
133
+ const unpacked = this.encoder.unpack(val);
134
+ // msgpackr returns plain Uint8Array in browsers for packed Buffers. Callers that
135
+ // stored Buffers (walletDB uses Buffer.from(...).toString('utf8') round-trips)
136
+ // rely on Buffer-flavored behavior — re-wrap at the storage boundary, mirroring
137
+ // IndexedDBAztecMap.restoreBuffers.
138
+ if (unpacked instanceof Uint8Array && !Buffer.isBuffer(unpacked)) {
139
+ return Buffer.from(unpacked) as V;
140
+ }
141
+ return unpacked as V;
142
+ }
143
+
144
+ protected decodeKey(raw: Uint8Array): K {
145
+ const parsed = fromBufferKey(Buffer.from(raw));
146
+ if (Array.isArray(parsed)) {
147
+ return (parsed.length > 1 ? parsed : parsed[0]) as K;
148
+ }
149
+ return parsed as K;
150
+ }
151
+
152
+ protected encodedKey(key: K): Buffer {
153
+ return toBufferKey(this.normalizeKey(key));
154
+ }
155
+
156
+ protected normalizeKey(key: K): (string | number | Uint8Array)[] {
157
+ return Array.isArray(key) ? key : [key];
158
+ }
159
+
160
+ protected slot(key: K, index: number = 0): string {
161
+ return `map:${this.name}:slot:${this.normalizeKey(key)}:${index}`;
162
+ }
163
+ }
@@ -0,0 +1,28 @@
1
+ /**
2
+ * RPC protocol between the main thread and the SQLite worker.
3
+ * All requests carry a unique `id`; responses echo the same id so the main thread
4
+ * can resolve the right pending promise.
5
+ */
6
+
7
+ /** Matches `@sqlite.org/sqlite-wasm`'s internal SqlValue type. Boolean is not a native SQLite type. */
8
+ export type SqlValue = string | number | bigint | null | Uint8Array;
9
+
10
+ /** A row returned in 'array' rowMode — one value per column, in select order. */
11
+ export type ResultRow = SqlValue[];
12
+
13
+ export type WorkerRequest =
14
+ | { type: 'init'; id: number; dbName: string; ephemeral: boolean; poolDirectory?: string }
15
+ | { type: 'close'; id: number }
16
+ | { type: 'deleteDb'; id: number; dbName: string }
17
+ | { type: 'run'; id: number; sql: string; bind?: SqlValue[] }
18
+ | { type: 'all'; id: number; sql: string; bind?: SqlValue[] }
19
+ | { type: 'export'; id: number }
20
+ | { type: 'begin'; id: number }
21
+ | { type: 'commit'; id: number }
22
+ | { type: 'rollback'; id: number };
23
+
24
+ export type WorkerResponse =
25
+ | { type: 'ok'; id: number; rows?: ResultRow[]; changes?: number; bytes?: Uint8Array }
26
+ | { type: 'err'; id: number; message: string };
27
+
28
+ export type WorkerRequestType = WorkerRequest['type'];
@@ -0,0 +1,74 @@
1
+ import { hash } from 'ohash';
2
+
3
+ import type { Key, Value } from '../interfaces/common.js';
4
+ import type { AztecAsyncMultiMap } from '../interfaces/multi_map.js';
5
+ import { SQLiteOPFSAztecMap } from './map.js';
6
+
7
+ /**
8
+ * Multi-map backed by SQLite. Extends the base map with always-incrementing
9
+ * `key_count` per-key so the same slot is never reused across deletions — this
10
+ * matches the IndexedDB backend's sparse-multi-map semantics.
11
+ */
12
+ export class SQLiteOPFSAztecMultiMap<K extends Key, V extends Value>
13
+ extends SQLiteOPFSAztecMap<K, V>
14
+ implements AztecAsyncMultiMap<K, V>
15
+ {
16
+ override async set(key: K, val: V): Promise<void> {
17
+ const valueHash = hash(val);
18
+ await this.store.transactionAsync(async () => {
19
+ const exists = await this.store.allAsync(
20
+ 'SELECT 1 FROM data WHERE container = ? AND key = ? AND hash = ? LIMIT 1',
21
+ [this.container, this.encodedKey(key), valueHash],
22
+ );
23
+ if (exists.length > 0) {
24
+ return;
25
+ }
26
+ const maxRow = await this.store.allAsync('SELECT MAX(key_count) FROM data WHERE container = ? AND key = ?', [
27
+ this.container,
28
+ this.encodedKey(key),
29
+ ]);
30
+ const count = Number(maxRow[0]?.[0] ?? 0);
31
+ await this.store.runAsync(
32
+ `INSERT INTO data (slot, container, key, key_count, hash, value)
33
+ VALUES (?, ?, ?, ?, ?, ?)`,
34
+ [this.slot(key, count), this.container, this.encodedKey(key), count + 1, valueHash, this.encoder.pack(val)],
35
+ );
36
+ });
37
+ }
38
+
39
+ async *getValuesAsync(key: K): AsyncIterableIterator<V> {
40
+ const rows = await this.store.allAsync(
41
+ 'SELECT value FROM data WHERE container = ? AND key = ? ORDER BY key_count ASC',
42
+ [this.container, this.encodedKey(key)],
43
+ );
44
+ for (const row of rows) {
45
+ const raw = row[0];
46
+ if (raw instanceof Uint8Array) {
47
+ yield this.decodeValue(raw);
48
+ }
49
+ }
50
+ }
51
+
52
+ async getValueCountAsync(key: K): Promise<number> {
53
+ const rows = await this.store.allAsync('SELECT COUNT(*) FROM data WHERE container = ? AND key = ?', [
54
+ this.container,
55
+ this.encodedKey(key),
56
+ ]);
57
+ return Number(rows[0]?.[0] ?? 0);
58
+ }
59
+
60
+ async deleteValue(key: K, val: V): Promise<void> {
61
+ await this.store.runAsync('DELETE FROM data WHERE container = ? AND key = ? AND hash = ?', [
62
+ this.container,
63
+ this.encodedKey(key),
64
+ hash(val),
65
+ ]);
66
+ }
67
+
68
+ override async delete(key: K): Promise<void> {
69
+ await this.store.runAsync('DELETE FROM data WHERE container = ? AND key = ?', [
70
+ this.container,
71
+ this.encodedKey(key),
72
+ ]);
73
+ }
74
+ }
@@ -0,0 +1,29 @@
1
+ import type { Key, Range } from '../interfaces/common.js';
2
+ import type { AztecAsyncSet } from '../interfaces/set.js';
3
+ import { SQLiteOPFSAztecMap } from './map.js';
4
+ import type { AztecSQLiteOPFSStore } from './store.js';
5
+
6
+ /** Set backed by SQLite. Composes a Map<K, true>. */
7
+ export class SQLiteOPFSAztecSet<K extends Key> implements AztecAsyncSet<K> {
8
+ readonly #map: SQLiteOPFSAztecMap<K, boolean>;
9
+
10
+ constructor(store: AztecSQLiteOPFSStore, name: string) {
11
+ this.#map = new SQLiteOPFSAztecMap<K, boolean>(store, name);
12
+ }
13
+
14
+ hasAsync(key: K): Promise<boolean> {
15
+ return this.#map.hasAsync(key);
16
+ }
17
+
18
+ add(key: K): Promise<void> {
19
+ return this.#map.set(key, true);
20
+ }
21
+
22
+ delete(key: K): Promise<void> {
23
+ return this.#map.delete(key);
24
+ }
25
+
26
+ async *entriesAsync(range: Range<K> = {}): AsyncIterableIterator<K> {
27
+ yield* this.#map.keysAsync(range);
28
+ }
29
+ }
@@ -0,0 +1,48 @@
1
+ import { Encoder } from 'msgpackr';
2
+ import { hash } from 'ohash';
3
+ import { toBufferKey } from 'ordered-binary';
4
+
5
+ import type { Value } from '../interfaces/common.js';
6
+ import type { AztecAsyncSingleton } from '../interfaces/singleton.js';
7
+ import type { AztecSQLiteOPFSStore } from './store.js';
8
+
9
+ /** Stores a single value identified by `name`. */
10
+ export class SQLiteOPFSAztecSingleton<T extends Value> implements AztecAsyncSingleton<T> {
11
+ readonly #container: string;
12
+ readonly #slot: string;
13
+ readonly #encoder = new Encoder();
14
+
15
+ constructor(
16
+ private readonly store: AztecSQLiteOPFSStore,
17
+ name: string,
18
+ ) {
19
+ this.#container = `singleton:${name}`;
20
+ this.#slot = `singleton:${name}:value`;
21
+ }
22
+
23
+ async getAsync(): Promise<T | undefined> {
24
+ const rows = await this.store.allAsync('SELECT value FROM data WHERE slot = ? LIMIT 1', [this.#slot]);
25
+ if (rows.length === 0) {
26
+ return undefined;
27
+ }
28
+ const raw = rows[0][0];
29
+ if (raw instanceof Uint8Array) {
30
+ return this.#encoder.unpack(raw) as T;
31
+ }
32
+ return undefined;
33
+ }
34
+
35
+ async set(val: T): Promise<boolean> {
36
+ const { changes } = await this.store.runAsync(
37
+ `INSERT OR REPLACE INTO data (slot, container, key, key_count, hash, value)
38
+ VALUES (?, ?, ?, ?, ?, ?)`,
39
+ [this.#slot, this.#container, toBufferKey([this.#slot]), 1, hash(val), this.#encoder.pack(val)],
40
+ );
41
+ return changes > 0;
42
+ }
43
+
44
+ async delete(): Promise<boolean> {
45
+ await this.store.runAsync('DELETE FROM data WHERE slot = ?', [this.#slot]);
46
+ return true;
47
+ }
48
+ }