@aztec/kv-store 0.0.0-test.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 (166) hide show
  1. package/README.md +10 -0
  2. package/dest/config.d.ts +17 -0
  3. package/dest/config.d.ts.map +1 -0
  4. package/dest/config.js +24 -0
  5. package/dest/indexeddb/array.d.ts +21 -0
  6. package/dest/indexeddb/array.d.ts.map +1 -0
  7. package/dest/indexeddb/array.js +104 -0
  8. package/dest/indexeddb/index.d.ts +7 -0
  9. package/dest/indexeddb/index.d.ts.map +1 -0
  10. package/dest/indexeddb/index.js +19 -0
  11. package/dest/indexeddb/map.d.ts +26 -0
  12. package/dest/indexeddb/map.d.ts.map +1 -0
  13. package/dest/indexeddb/map.js +133 -0
  14. package/dest/indexeddb/set.d.ts +17 -0
  15. package/dest/indexeddb/set.d.ts.map +1 -0
  16. package/dest/indexeddb/set.js +24 -0
  17. package/dest/indexeddb/singleton.d.ts +16 -0
  18. package/dest/indexeddb/singleton.d.ts.map +1 -0
  19. package/dest/indexeddb/singleton.js +37 -0
  20. package/dest/indexeddb/store.d.ts +97 -0
  21. package/dest/indexeddb/store.d.ts.map +1 -0
  22. package/dest/indexeddb/store.js +164 -0
  23. package/dest/interfaces/array.d.ts +80 -0
  24. package/dest/interfaces/array.d.ts.map +1 -0
  25. package/dest/interfaces/array.js +3 -0
  26. package/dest/interfaces/array_test_suite.d.ts +3 -0
  27. package/dest/interfaces/array_test_suite.d.ts.map +1 -0
  28. package/dest/interfaces/array_test_suite.js +104 -0
  29. package/dest/interfaces/common.d.ts +23 -0
  30. package/dest/interfaces/common.d.ts.map +1 -0
  31. package/dest/interfaces/common.js +3 -0
  32. package/dest/interfaces/counter.d.ts +59 -0
  33. package/dest/interfaces/counter.d.ts.map +1 -0
  34. package/dest/interfaces/counter.js +1 -0
  35. package/dest/interfaces/index.d.ts +8 -0
  36. package/dest/interfaces/index.d.ts.map +1 -0
  37. package/dest/interfaces/index.js +6 -0
  38. package/dest/interfaces/map.d.ts +134 -0
  39. package/dest/interfaces/map.d.ts.map +1 -0
  40. package/dest/interfaces/map.js +3 -0
  41. package/dest/interfaces/map_test_suite.d.ts +3 -0
  42. package/dest/interfaces/map_test_suite.d.ts.map +1 -0
  43. package/dest/interfaces/map_test_suite.js +151 -0
  44. package/dest/interfaces/set.d.ts +44 -0
  45. package/dest/interfaces/set.d.ts.map +1 -0
  46. package/dest/interfaces/set.js +1 -0
  47. package/dest/interfaces/set_test_suite.d.ts +3 -0
  48. package/dest/interfaces/set_test_suite.d.ts.map +1 -0
  49. package/dest/interfaces/set_test_suite.js +96 -0
  50. package/dest/interfaces/singleton.d.ts +29 -0
  51. package/dest/interfaces/singleton.d.ts.map +1 -0
  52. package/dest/interfaces/singleton.js +4 -0
  53. package/dest/interfaces/singleton_test_suite.d.ts +3 -0
  54. package/dest/interfaces/singleton_test_suite.d.ts.map +1 -0
  55. package/dest/interfaces/singleton_test_suite.js +30 -0
  56. package/dest/interfaces/store.d.ts +145 -0
  57. package/dest/interfaces/store.d.ts.map +1 -0
  58. package/dest/interfaces/store.js +1 -0
  59. package/dest/interfaces/store_test_suite.d.ts +3 -0
  60. package/dest/interfaces/store_test_suite.d.ts.map +1 -0
  61. package/dest/interfaces/store_test_suite.js +37 -0
  62. package/dest/interfaces/utils.d.ts +16 -0
  63. package/dest/interfaces/utils.d.ts.map +1 -0
  64. package/dest/interfaces/utils.js +16 -0
  65. package/dest/lmdb/array.d.ts +23 -0
  66. package/dest/lmdb/array.d.ts.map +1 -0
  67. package/dest/lmdb/array.js +117 -0
  68. package/dest/lmdb/counter.d.ts +19 -0
  69. package/dest/lmdb/counter.d.ts.map +1 -0
  70. package/dest/lmdb/counter.js +50 -0
  71. package/dest/lmdb/index.d.ts +12 -0
  72. package/dest/lmdb/index.d.ts.map +1 -0
  73. package/dest/lmdb/index.js +25 -0
  74. package/dest/lmdb/map.d.ts +52 -0
  75. package/dest/lmdb/map.d.ts.map +1 -0
  76. package/dest/lmdb/map.js +222 -0
  77. package/dest/lmdb/set.d.ts +18 -0
  78. package/dest/lmdb/set.d.ts.map +1 -0
  79. package/dest/lmdb/set.js +32 -0
  80. package/dest/lmdb/singleton.d.ts +14 -0
  81. package/dest/lmdb/singleton.d.ts.map +1 -0
  82. package/dest/lmdb/singleton.js +26 -0
  83. package/dest/lmdb/store.d.ts +110 -0
  84. package/dest/lmdb/store.d.ts.map +1 -0
  85. package/dest/lmdb/store.js +216 -0
  86. package/dest/lmdb-v2/array.d.ts +18 -0
  87. package/dest/lmdb-v2/array.d.ts.map +1 -0
  88. package/dest/lmdb-v2/array.js +101 -0
  89. package/dest/lmdb-v2/factory.d.ts +12 -0
  90. package/dest/lmdb-v2/factory.d.ts.map +1 -0
  91. package/dest/lmdb-v2/factory.js +60 -0
  92. package/dest/lmdb-v2/index.d.ts +3 -0
  93. package/dest/lmdb-v2/index.d.ts.map +1 -0
  94. package/dest/lmdb-v2/index.js +2 -0
  95. package/dest/lmdb-v2/map.d.ts +86 -0
  96. package/dest/lmdb-v2/map.d.ts.map +1 -0
  97. package/dest/lmdb-v2/map.js +193 -0
  98. package/dest/lmdb-v2/message.d.ts +112 -0
  99. package/dest/lmdb-v2/message.d.ts.map +1 -0
  100. package/dest/lmdb-v2/message.js +18 -0
  101. package/dest/lmdb-v2/read_transaction.d.ts +14 -0
  102. package/dest/lmdb-v2/read_transaction.d.ts.map +1 -0
  103. package/dest/lmdb-v2/read_transaction.js +101 -0
  104. package/dest/lmdb-v2/singleton.d.ts +12 -0
  105. package/dest/lmdb-v2/singleton.d.ts.map +1 -0
  106. package/dest/lmdb-v2/singleton.js +31 -0
  107. package/dest/lmdb-v2/store.d.ts +43 -0
  108. package/dest/lmdb-v2/store.d.ts.map +1 -0
  109. package/dest/lmdb-v2/store.js +181 -0
  110. package/dest/lmdb-v2/utils.d.ts +19 -0
  111. package/dest/lmdb-v2/utils.d.ts.map +1 -0
  112. package/dest/lmdb-v2/utils.js +126 -0
  113. package/dest/lmdb-v2/write_transaction.d.ts +19 -0
  114. package/dest/lmdb-v2/write_transaction.d.ts.map +1 -0
  115. package/dest/lmdb-v2/write_transaction.js +254 -0
  116. package/dest/stores/index.d.ts +2 -0
  117. package/dest/stores/index.d.ts.map +1 -0
  118. package/dest/stores/index.js +1 -0
  119. package/dest/stores/l2_tips_store.d.ts +13 -0
  120. package/dest/stores/l2_tips_store.d.ts.map +1 -0
  121. package/dest/stores/l2_tips_store.js +65 -0
  122. package/dest/utils.d.ts +12 -0
  123. package/dest/utils.d.ts.map +1 -0
  124. package/dest/utils.js +24 -0
  125. package/package.json +99 -0
  126. package/src/config.ts +34 -0
  127. package/src/indexeddb/array.ts +118 -0
  128. package/src/indexeddb/index.ts +29 -0
  129. package/src/indexeddb/map.ts +142 -0
  130. package/src/indexeddb/set.ts +37 -0
  131. package/src/indexeddb/singleton.ts +49 -0
  132. package/src/indexeddb/store.ts +197 -0
  133. package/src/interfaces/array.ts +90 -0
  134. package/src/interfaces/array_test_suite.ts +130 -0
  135. package/src/interfaces/common.ts +20 -0
  136. package/src/interfaces/counter.ts +65 -0
  137. package/src/interfaces/index.ts +7 -0
  138. package/src/interfaces/map.ts +151 -0
  139. package/src/interfaces/map_test_suite.ts +143 -0
  140. package/src/interfaces/set.ts +48 -0
  141. package/src/interfaces/set_test_suite.ts +81 -0
  142. package/src/interfaces/singleton.ts +29 -0
  143. package/src/interfaces/singleton_test_suite.ts +46 -0
  144. package/src/interfaces/store.ts +177 -0
  145. package/src/interfaces/store_test_suite.ts +56 -0
  146. package/src/interfaces/utils.ts +21 -0
  147. package/src/lmdb/array.ts +139 -0
  148. package/src/lmdb/counter.ts +67 -0
  149. package/src/lmdb/index.ts +37 -0
  150. package/src/lmdb/map.ts +248 -0
  151. package/src/lmdb/set.ts +45 -0
  152. package/src/lmdb/singleton.ts +35 -0
  153. package/src/lmdb/store.ts +263 -0
  154. package/src/lmdb-v2/array.ts +115 -0
  155. package/src/lmdb-v2/factory.ts +98 -0
  156. package/src/lmdb-v2/index.ts +2 -0
  157. package/src/lmdb-v2/map.ts +233 -0
  158. package/src/lmdb-v2/message.ts +146 -0
  159. package/src/lmdb-v2/read_transaction.ts +116 -0
  160. package/src/lmdb-v2/singleton.ts +34 -0
  161. package/src/lmdb-v2/store.ts +237 -0
  162. package/src/lmdb-v2/utils.ts +150 -0
  163. package/src/lmdb-v2/write_transaction.ts +314 -0
  164. package/src/stores/index.ts +1 -0
  165. package/src/stores/l2_tips_store.ts +70 -0
  166. package/src/utils.ts +40 -0
@@ -0,0 +1,143 @@
1
+ import { toArray } from '@aztec/foundation/iterable';
2
+
3
+ import { expect } from 'chai';
4
+
5
+ import type { Key, Range } from './common.js';
6
+ import type { AztecAsyncMap, AztecAsyncMultiMap, AztecMap, AztecMultiMap } from './map.js';
7
+ import type { AztecAsyncKVStore, AztecKVStore } from './store.js';
8
+ import { isSyncStore } from './utils.js';
9
+
10
+ export function describeAztecMap(
11
+ testName: string,
12
+ getStore: () => AztecKVStore | Promise<AztecAsyncKVStore>,
13
+ forceAsync: boolean = false,
14
+ ) {
15
+ describe(testName, () => {
16
+ let store: AztecKVStore | AztecAsyncKVStore;
17
+ let map: AztecMultiMap<Key, string> | AztecAsyncMultiMap<Key, string>;
18
+
19
+ beforeEach(async () => {
20
+ store = await getStore();
21
+ map = store.openMultiMap<string, string>('test');
22
+ });
23
+
24
+ afterEach(async () => {
25
+ await store.delete();
26
+ });
27
+
28
+ async function get(key: Key, sut: AztecAsyncMap<any, any> | AztecMap<any, any> = map) {
29
+ return isSyncStore(store) && !forceAsync
30
+ ? (sut as AztecMultiMap<any, any>).get(key)
31
+ : await (sut as AztecAsyncMultiMap<any, any>).getAsync(key);
32
+ }
33
+
34
+ async function entries() {
35
+ return isSyncStore(store) && !forceAsync
36
+ ? await toArray((map as AztecMultiMap<any, any>).entries())
37
+ : await toArray((map as AztecAsyncMultiMap<any, any>).entriesAsync());
38
+ }
39
+
40
+ async function values() {
41
+ return isSyncStore(store) && !forceAsync
42
+ ? await toArray((map as AztecMultiMap<any, any>).values())
43
+ : await toArray((map as AztecAsyncMultiMap<any, any>).valuesAsync());
44
+ }
45
+
46
+ async function keys(range?: Range<Key>, sut: AztecAsyncMap<any, any> | AztecMap<any, any> = map) {
47
+ return isSyncStore(store) && !forceAsync
48
+ ? await toArray((sut as AztecMultiMap<any, any>).keys(range))
49
+ : await toArray((sut as AztecAsyncMultiMap<any, any>).keysAsync(range));
50
+ }
51
+
52
+ async function getValues(key: Key) {
53
+ return isSyncStore(store) && !forceAsync
54
+ ? await toArray((map as AztecMultiMap<any, any>).getValues(key))
55
+ : await toArray((map as AztecAsyncMultiMap<any, any>).getValuesAsync(key));
56
+ }
57
+
58
+ it('should be able to set and get values', async () => {
59
+ await map.set('foo', 'bar');
60
+ await map.set('baz', 'qux');
61
+
62
+ expect(await get('foo')).to.equal('bar');
63
+ expect(await get('baz')).to.equal('qux');
64
+ expect(await get('quux')).to.equal(undefined);
65
+ });
66
+
67
+ it('should be able to set values if they do not exist', async () => {
68
+ expect(await map.setIfNotExists('foo', 'bar')).to.equal(true);
69
+ expect(await map.setIfNotExists('foo', 'baz')).to.equal(false);
70
+
71
+ expect(await get('foo')).to.equal('bar');
72
+ });
73
+
74
+ it('should be able to delete values', async () => {
75
+ await map.set('foo', 'bar');
76
+ await map.set('baz', 'qux');
77
+
78
+ await map.delete('foo');
79
+
80
+ expect(await get('foo')).to.equal(undefined);
81
+ expect(await get('baz')).to.equal('qux');
82
+ });
83
+
84
+ it('should be able to iterate over entries when there are no keys', async () => {
85
+ expect(await entries()).to.deep.equal([]);
86
+ });
87
+
88
+ it('should be able to iterate over entries', async () => {
89
+ await map.set('foo', 'bar');
90
+ await map.set('baz', 'qux');
91
+
92
+ expect(await entries()).to.deep.equal([
93
+ ['baz', 'qux'],
94
+ ['foo', 'bar'],
95
+ ]);
96
+ });
97
+
98
+ it('should be able to iterate over values', async () => {
99
+ await map.set('foo', 'bar');
100
+ await map.set('baz', 'quux');
101
+
102
+ expect(await values()).to.deep.equal(['quux', 'bar']);
103
+ });
104
+
105
+ it('should be able to iterate over keys', async () => {
106
+ await map.set('foo', 'bar');
107
+ await map.set('baz', 'qux');
108
+
109
+ expect(await keys()).to.deep.equal(['baz', 'foo']);
110
+ });
111
+
112
+ it('should be able to get multiple values for a single key', async () => {
113
+ await map.set('foo', 'bar');
114
+ await map.set('foo', 'baz');
115
+
116
+ expect(await getValues('foo')).to.deep.equal(['bar', 'baz']);
117
+ });
118
+
119
+ it('should be able to delete individual values for a single key', async () => {
120
+ await map.set('foo', 'bar');
121
+ await map.set('foo', 'baz');
122
+
123
+ await map.deleteValue('foo', 'bar');
124
+
125
+ expect(await getValues('foo')).to.deep.equal(['baz']);
126
+ });
127
+
128
+ it('supports range queries', async () => {
129
+ await map.set('a', 'a');
130
+ await map.set('b', 'b');
131
+ await map.set('c', 'c');
132
+ await map.set('d', 'd');
133
+
134
+ expect(await keys({ start: 'b', end: 'c' })).to.deep.equal(['b']);
135
+ expect(await keys({ start: 'b' })).to.deep.equal(['b', 'c', 'd']);
136
+ expect(await keys({ end: 'c' })).to.deep.equal(['a', 'b']);
137
+ expect(await keys({ start: 'b', end: 'c', reverse: true })).to.deep.equal(['c']);
138
+ expect(await keys({ start: 'b', limit: 1 })).to.deep.equal(['b']);
139
+ expect(await keys({ start: 'b', reverse: true })).to.deep.equal(['d', 'c']);
140
+ expect(await keys({ end: 'b', reverse: true })).to.deep.equal(['b', 'a']);
141
+ });
142
+ });
143
+ }
@@ -0,0 +1,48 @@
1
+ import type { Key, Range } from './common.js';
2
+
3
+ /**
4
+ * A set backed by a persistent store.
5
+ */
6
+ interface AztecBaseSet<K extends Key> {
7
+ /**
8
+ * Adds the given value.
9
+ * @param key - The key to add.
10
+ */
11
+ add(key: K): Promise<void>;
12
+
13
+ /**
14
+ * Deletes the given key.
15
+ * @param key - The key to delete.
16
+ */
17
+ delete(key: K): Promise<void>;
18
+ }
19
+
20
+ export interface AztecSet<K extends Key> extends AztecBaseSet<K> {
21
+ /**
22
+ * Checks if a key exists in the set.
23
+ * @param key - The key to check
24
+ * @returns True if the key exists, false otherwise
25
+ */
26
+ has(key: K): boolean;
27
+
28
+ /**
29
+ * Iterates over the sets's keys entries in the key's natural order
30
+ * @param range - The range of keys to iterate over
31
+ */
32
+ entries(range?: Range<K>): IterableIterator<K>;
33
+ }
34
+
35
+ export interface AztecAsyncSet<K extends Key> extends AztecBaseSet<K> {
36
+ /**
37
+ * Checks if a key exists in the set.
38
+ * @param key - The key to check
39
+ * @returns True if the key exists, false otherwise
40
+ */
41
+ hasAsync(key: K): Promise<boolean>;
42
+
43
+ /**
44
+ * Iterates over the sets's keys entries in the key's natural order
45
+ * @param range - The range of keys to iterate over
46
+ */
47
+ entriesAsync(range?: Range<K>): AsyncIterableIterator<K>;
48
+ }
@@ -0,0 +1,81 @@
1
+ import { toArray } from '@aztec/foundation/iterable';
2
+
3
+ import { expect } from 'chai';
4
+
5
+ import type { Range } from './common.js';
6
+ import type { AztecAsyncSet, AztecSet } from './set.js';
7
+ import type { AztecAsyncKVStore, AztecKVStore } from './store.js';
8
+ import { isSyncStore } from './utils.js';
9
+
10
+ export function describeAztecSet(
11
+ testName: string,
12
+ getStore: () => AztecKVStore | Promise<AztecAsyncKVStore>,
13
+ forceAsync: boolean = false,
14
+ ) {
15
+ describe(testName, () => {
16
+ let store: AztecKVStore | AztecAsyncKVStore;
17
+ let set: AztecSet<string> | AztecAsyncSet<string>;
18
+
19
+ beforeEach(async () => {
20
+ store = await getStore();
21
+ set = store.openSet<string>('test');
22
+ });
23
+
24
+ afterEach(async () => {
25
+ await store.delete();
26
+ });
27
+
28
+ async function has(key: string) {
29
+ return isSyncStore(store) && !forceAsync
30
+ ? (set as AztecSet<string>).has(key)
31
+ : await (set as AztecAsyncSet<string>).hasAsync(key);
32
+ }
33
+
34
+ async function entries(range?: Range<any>) {
35
+ return isSyncStore(store) && !forceAsync
36
+ ? await toArray((set as AztecSet<string>).entries(range))
37
+ : await toArray((set as AztecAsyncSet<string>).entriesAsync(range));
38
+ }
39
+
40
+ it('should be able to set and get values', async () => {
41
+ await set.add('foo');
42
+ await set.add('baz');
43
+
44
+ expect(await has('foo')).to.equal(true);
45
+ expect(await has('baz')).to.equal(true);
46
+ expect(await has('bar')).to.equal(false);
47
+ });
48
+
49
+ it('should be able to delete values', async () => {
50
+ await set.add('foo');
51
+ await set.add('baz');
52
+
53
+ await set.delete('foo');
54
+
55
+ expect(await has('foo')).to.equal(false);
56
+ expect(await has('baz')).to.equal(true);
57
+ });
58
+
59
+ it('should be able to iterate over entries', async () => {
60
+ await set.add('baz');
61
+ await set.add('foo');
62
+
63
+ expect(await entries()).to.deep.equal(['baz', 'foo']);
64
+ });
65
+
66
+ it('supports range queries', async () => {
67
+ await set.add('a');
68
+ await set.add('b');
69
+ await set.add('c');
70
+ await set.add('d');
71
+
72
+ expect(await entries({ start: 'b', end: 'c' })).to.deep.equal(['b']);
73
+ expect(await entries({ start: 'b' })).to.deep.equal(['b', 'c', 'd']);
74
+ expect(await entries({ end: 'c' })).to.deep.equal(['a', 'b']);
75
+ expect(await entries({ start: 'b', end: 'c', reverse: true })).to.deep.equal(['c']);
76
+ expect(await entries({ start: 'b', limit: 1 })).to.deep.equal(['b']);
77
+ expect(await entries({ start: 'b', reverse: true })).to.deep.equal(['d', 'c']);
78
+ expect(await entries({ end: 'b', reverse: true })).to.deep.equal(['b', 'a']);
79
+ });
80
+ });
81
+ }
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Represents a singleton value in the database.
3
+ * Note: The singleton loses type info so it's recommended to serialize to buffer when storing it.
4
+ */
5
+ interface AztecBaseSingleton<T> {
6
+ /**
7
+ * Sets the value.
8
+ * @param val - The new value
9
+ */
10
+ set(val: T): Promise<boolean>;
11
+
12
+ /**
13
+ * Deletes the value.
14
+ */
15
+ delete(): Promise<boolean>;
16
+ }
17
+ export interface AztecSingleton<T> extends AztecBaseSingleton<T> {
18
+ /**
19
+ * Gets the value.
20
+ */
21
+ get(): T | undefined;
22
+ }
23
+
24
+ export interface AztecAsyncSingleton<T> extends AztecBaseSingleton<T> {
25
+ /**
26
+ * Gets the value.
27
+ */
28
+ getAsync(): Promise<T | undefined>;
29
+ }
@@ -0,0 +1,46 @@
1
+ import { expect } from 'chai';
2
+
3
+ import type { AztecAsyncSingleton, AztecSingleton } from './singleton.js';
4
+ import type { AztecAsyncKVStore, AztecKVStore } from './store.js';
5
+ import { isSyncStore } from './utils.js';
6
+
7
+ export function describeAztecSingleton(
8
+ testName: string,
9
+ getStore: () => AztecKVStore | Promise<AztecAsyncKVStore>,
10
+ forceAsync: boolean = false,
11
+ ) {
12
+ describe(testName, () => {
13
+ let store: AztecKVStore | AztecAsyncKVStore;
14
+ let singleton: AztecSingleton<string> | AztecAsyncSingleton<string>;
15
+
16
+ beforeEach(async () => {
17
+ store = await getStore();
18
+ singleton = store.openSingleton<string>('test');
19
+ });
20
+
21
+ afterEach(async () => {
22
+ await store.delete();
23
+ });
24
+
25
+ async function get() {
26
+ return isSyncStore(store) && !forceAsync
27
+ ? (singleton as AztecSingleton<string>).get()
28
+ : await (singleton as AztecAsyncSingleton<string>).getAsync();
29
+ }
30
+
31
+ it('returns undefined if the value is not set', async () => {
32
+ expect(await get()).to.equal(undefined);
33
+ });
34
+
35
+ it('should be able to set and get values', async () => {
36
+ expect(await singleton.set('foo')).to.equal(true);
37
+ expect(await get()).to.equal('foo');
38
+ });
39
+
40
+ it('overwrites the value if it is set again', async () => {
41
+ expect(await singleton.set('foo')).to.equal(true);
42
+ expect(await singleton.set('bar')).to.equal(true);
43
+ expect(await get()).to.equal('bar');
44
+ });
45
+ });
46
+ }
@@ -0,0 +1,177 @@
1
+ import type { AztecArray, AztecAsyncArray } from './array.js';
2
+ import type { Key, StoreSize } from './common.js';
3
+ import type { AztecAsyncCounter, AztecCounter } from './counter.js';
4
+ import type {
5
+ AztecAsyncMap,
6
+ AztecAsyncMultiMap,
7
+ AztecMap,
8
+ AztecMapWithSize,
9
+ AztecMultiMap,
10
+ AztecMultiMapWithSize,
11
+ } from './map.js';
12
+ import type { AztecAsyncSet, AztecSet } from './set.js';
13
+ import type { AztecAsyncSingleton, AztecSingleton } from './singleton.js';
14
+
15
+ /** A key-value store */
16
+ export interface AztecKVStore {
17
+ syncGetters: true;
18
+ /**
19
+ * Creates a new map.
20
+ * @param name - The name of the map
21
+ * @returns The map
22
+ */
23
+ openMap<K extends Key, V>(name: string): AztecMap<K, V>;
24
+
25
+ /**
26
+ * Creates a new set.
27
+ * @param name - The name of the set
28
+ * @returns The set
29
+ */
30
+ openSet<K extends Key>(name: string): AztecSet<K>;
31
+
32
+ /**
33
+ * Creates a new multi-map.
34
+ * @param name - The name of the multi-map
35
+ * @returns The multi-map
36
+ */
37
+ openMultiMap<K extends Key, V>(name: string): AztecMultiMap<K, V>;
38
+
39
+ /**
40
+ * Creates a new multi-map with size.
41
+ * @param name - The name of the multi-map
42
+ * @returns The multi-map
43
+ */
44
+ openMultiMapWithSize<K extends Key, V>(name: string): AztecMultiMapWithSize<K, V>;
45
+
46
+ /**
47
+ * Creates a new map with size.
48
+ * @param name - The name of the map
49
+ * @returns The map
50
+ */
51
+ openMapWithSize<K extends Key, V>(name: string): AztecMapWithSize<K, V>;
52
+
53
+ /**
54
+ * Creates a new array.
55
+ * @param name - The name of the array
56
+ * @returns The array
57
+ */
58
+ openArray<T>(name: string): AztecArray<T>;
59
+
60
+ /**
61
+ * Creates a new singleton.
62
+ * @param name - The name of the singleton
63
+ * @returns The singleton
64
+ */
65
+ openSingleton<T>(name: string): AztecSingleton<T>;
66
+
67
+ /**
68
+ * Creates a new count map.
69
+ * @param name - name of the counter
70
+ */
71
+ openCounter<K extends Key>(name: string): AztecCounter<K>;
72
+
73
+ /**
74
+ * Starts a transaction. All calls to read/write data while in a transaction are queued and executed atomically.
75
+ * @param callback - The callback to execute in a transaction
76
+ */
77
+ transaction<T extends Exclude<any, Promise<any>>>(callback: () => T): Promise<T>;
78
+
79
+ /**
80
+ * Clears all entries in the store
81
+ */
82
+ clear(): Promise<void>;
83
+
84
+ /**
85
+ * Forks the store.
86
+ */
87
+ fork(): Promise<AztecKVStore>;
88
+
89
+ /**
90
+ * Deletes the store
91
+ */
92
+ delete(): Promise<void>;
93
+
94
+ /**
95
+ * Estimates the size of the store in bytes.
96
+ */
97
+ estimateSize(): Promise<StoreSize>;
98
+
99
+ /**
100
+ * Closes the store
101
+ */
102
+ close(): Promise<void>;
103
+ }
104
+
105
+ export interface AztecAsyncKVStore {
106
+ /**
107
+ * Creates a new map.
108
+ * @param name - The name of the map
109
+ * @returns The map
110
+ */
111
+ openMap<K extends Key, V>(name: string): AztecAsyncMap<K, V>;
112
+
113
+ /**
114
+ * Creates a new set.
115
+ * @param name - The name of the set
116
+ * @returns The set
117
+ */
118
+ openSet<K extends Key>(name: string): AztecAsyncSet<K>;
119
+
120
+ /**
121
+ * Creates a new multi-map.
122
+ * @param name - The name of the multi-map
123
+ * @returns The multi-map
124
+ */
125
+ openMultiMap<K extends Key, V>(name: string): AztecAsyncMultiMap<K, V>;
126
+
127
+ /**
128
+ * Creates a new array.
129
+ * @param name - The name of the array
130
+ * @returns The array
131
+ */
132
+ openArray<T>(name: string): AztecAsyncArray<T>;
133
+
134
+ /**
135
+ * Creates a new singleton.
136
+ * @param name - The name of the singleton
137
+ * @returns The singleton
138
+ */
139
+ openSingleton<T>(name: string): AztecAsyncSingleton<T>;
140
+
141
+ /**
142
+ * Creates a new count map.
143
+ * @param name - name of the counter
144
+ */
145
+ openCounter<K extends Key>(name: string): AztecAsyncCounter<K>;
146
+
147
+ /**
148
+ * Starts a transaction. All calls to read/write data while in a transaction are queued and executed atomically.
149
+ * @param callback - The callback to execute in a transaction
150
+ */
151
+ transactionAsync<T extends Exclude<any, Promise<any>>>(callback: () => Promise<T>): Promise<T>;
152
+
153
+ /**
154
+ * Clears all entries in the store
155
+ */
156
+ clear(): Promise<void>;
157
+
158
+ /**
159
+ * Forks the store.
160
+ */
161
+ fork(): Promise<AztecAsyncKVStore>;
162
+
163
+ /**
164
+ * Deletes the store
165
+ */
166
+ delete(): Promise<void>;
167
+
168
+ /**
169
+ * Estimates the size of the store in bytes.
170
+ */
171
+ estimateSize(): Promise<StoreSize>;
172
+
173
+ /**
174
+ * Closes the store
175
+ */
176
+ close(): Promise<void>;
177
+ }
@@ -0,0 +1,56 @@
1
+ import { expect } from 'chai';
2
+
3
+ import type { AztecAsyncSingleton, AztecSingleton } from './singleton.js';
4
+ import type { AztecAsyncKVStore, AztecKVStore } from './store.js';
5
+ import { isSyncStore } from './utils.js';
6
+
7
+ export function describeAztecStore(
8
+ testName: string,
9
+ getPersistentStore: () => Promise<AztecKVStore | AztecAsyncKVStore>,
10
+ getPersistentNoPathStore: () => Promise<AztecKVStore | AztecAsyncKVStore>,
11
+ getEphemeralStore: () => Promise<AztecKVStore | AztecAsyncKVStore>,
12
+ ) {
13
+ describe(testName, () => {
14
+ async function get(
15
+ store: AztecKVStore | AztecAsyncKVStore,
16
+ singleton: AztecSingleton<string> | AztecAsyncSingleton<string>,
17
+ ) {
18
+ return isSyncStore(store)
19
+ ? (singleton as AztecSingleton<string>).get()
20
+ : await (singleton as AztecAsyncSingleton<string>).getAsync();
21
+ }
22
+
23
+ const itForks = async (store: AztecKVStore | AztecAsyncKVStore) => {
24
+ const singleton = store.openSingleton<string>('singleton');
25
+ await singleton.set('foo');
26
+
27
+ const forkedStore = await store.fork();
28
+ const forkedSingleton = forkedStore.openSingleton<string>('singleton');
29
+ expect(await get(store, singleton)).to.equal('foo');
30
+ await forkedSingleton.set('bar');
31
+ expect(await get(store, singleton)).to.equal('foo');
32
+ expect(await get(forkedStore, forkedSingleton)).to.equal('bar');
33
+ await forkedSingleton.delete();
34
+ expect(await get(store, singleton)).to.equal('foo');
35
+ await forkedStore.delete();
36
+ };
37
+
38
+ it('forks a persistent store', async () => {
39
+ const store = await getPersistentStore();
40
+ await itForks(store);
41
+ await store.delete();
42
+ });
43
+
44
+ it('forks a persistent store with no path', async () => {
45
+ const store = await getPersistentNoPathStore();
46
+ await itForks(store);
47
+ await store.delete();
48
+ });
49
+
50
+ it('forks an ephemeral store', async () => {
51
+ const store = await getEphemeralStore();
52
+ await itForks(store);
53
+ await store.delete();
54
+ });
55
+ });
56
+ }
@@ -0,0 +1,21 @@
1
+ import type { AztecAsyncKVStore, AztecKVStore } from './store.js';
2
+
3
+ /* eslint-disable no-console */
4
+ export const mockLogger = {
5
+ debug: (msg: string, data: any) => console.log(msg, data),
6
+ info: (msg: string, data: any) => console.log(msg, data),
7
+ warn: (msg: string, data: any) => console.log(msg, data),
8
+ error: (msg: string, data: any) => console.error(msg, data),
9
+ fatal: (msg: string, data: any) => console.error(msg, data),
10
+ silent: (_msg: string, _data: any) => {},
11
+ verbose: (msg: string, data: any) => console.log(msg, data),
12
+ trace: (msg: string, data: any) => console.log(msg, data),
13
+ level: 'trace' as const,
14
+ isLevelEnabled: (_level: string) => true,
15
+ module: 'kv-store:mock-logger',
16
+ };
17
+ /* eslint-enable no-console */
18
+
19
+ export function isSyncStore(store: AztecKVStore | AztecAsyncKVStore): store is AztecAsyncKVStore {
20
+ return (store as AztecKVStore).syncGetters === true;
21
+ }