|
| 1 | +import { |
| 2 | + Pair as StorePair, |
| 3 | + Batch as StoreBatch, |
| 4 | + QueryFilter as StoreQueryFilter, |
| 5 | + QueryOrder as StoreQueryOrder, |
| 6 | + Query as StoreQuery, |
| 7 | + KeyQueryFilter as StoreKeyQueryFilter, |
| 8 | + KeyQueryOrder as StoreKeyQueryOrder, |
| 9 | + KeyQuery as StoreKeyQuery, |
| 10 | + Options as StoreOptions, |
| 11 | + Store |
| 12 | +} from 'interface-store' |
1 | 13 | import type Key from './key'
|
2 | 14 |
|
3 |
| -export type AwaitIterable<T> = Iterable<T> | AsyncIterable<T> |
4 |
| -export type Await<T> = Promise<T> | T |
5 |
| -export interface Pair { |
6 |
| - key: Key |
7 |
| - value: Uint8Array |
| 15 | +export interface Options extends StoreOptions{ |
| 16 | + |
| 17 | +} |
| 18 | + |
| 19 | +export interface Pair extends StorePair<Key, Uint8Array> { |
| 20 | + |
| 21 | +} |
| 22 | + |
| 23 | +export interface Batch extends StoreBatch<Key, Uint8Array> { |
| 24 | + |
| 25 | +} |
| 26 | + |
| 27 | +export interface Datastore extends Store<Key, Uint8Array> { |
| 28 | + |
8 | 29 | }
|
9 |
| -/** |
10 |
| - * Options for async operations. |
11 |
| - */ |
12 |
| -export interface Options { |
13 |
| - signal?: AbortSignal |
| 30 | + |
| 31 | +export interface QueryFilter extends StoreQueryFilter<Key, Uint8Array> { |
| 32 | + |
14 | 33 | }
|
15 | 34 |
|
16 |
| -export interface Batch { |
17 |
| - put: (key: Key, value: Uint8Array) => void |
18 |
| - delete: (key: Key) => void |
19 |
| - commit: (options?: Options) => Promise<void> |
| 35 | +export interface QueryOrder extends StoreQueryOrder<Key, Uint8Array> { |
| 36 | + |
20 | 37 | }
|
21 |
| -export interface Datastore { |
22 |
| - open: () => Promise<void> |
23 |
| - close: () => Promise<void> |
24 |
| - /** |
25 |
| - * Store the passed value under the passed key |
26 |
| - * |
27 |
| - * @example |
28 |
| - * |
29 |
| - * ```js |
30 |
| - * await store.put([{ key: new Key('awesome'), value: new Uint8Array([0, 1, 2, 3]) }]) |
31 |
| - * ``` |
32 |
| - */ |
33 |
| - put: (key: Key, val: Uint8Array, options?: Options) => Promise<void> |
34 |
| - /** |
35 |
| - * Retrieve the value stored under the given key |
36 |
| - * |
37 |
| - * @example |
38 |
| - * ```js |
39 |
| - * const value = await store.get(new Key('awesome')) |
40 |
| - * console.log('got content: %s', value.toString('utf8')) |
41 |
| - * // => got content: datastore |
42 |
| - * ``` |
43 |
| - */ |
44 |
| - get: (key: Key, options?: Options) => Promise<Uint8Array> |
45 |
| - /** |
46 |
| - * Check for the existence of a value for the passed key |
47 |
| - * |
48 |
| - * @example |
49 |
| - * ```js |
50 |
| - *const exists = await store.has(new Key('awesome')) |
51 |
| - * |
52 |
| - *if (exists) { |
53 |
| - * console.log('it is there') |
54 |
| - *} else { |
55 |
| - * console.log('it is not there') |
56 |
| - *} |
57 |
| - *``` |
58 |
| - */ |
59 |
| - has: (key: Key, options?: Options) => Promise<boolean> |
60 |
| - /** |
61 |
| - * Remove the record for the passed key |
62 |
| - * |
63 |
| - * @example |
64 |
| - * |
65 |
| - * ```js |
66 |
| - * await store.delete(new Key('awesome')) |
67 |
| - * console.log('deleted awesome content :(') |
68 |
| - * ``` |
69 |
| - */ |
70 |
| - delete: (key: Key, options?: Options) => Promise<void> |
71 |
| - /** |
72 |
| - * Store the given key/value pairs |
73 |
| - * |
74 |
| - * @example |
75 |
| - * ```js |
76 |
| - * const source = [{ key: new Key('awesome'), value: new Uint8Array([0, 1, 2, 3]) }] |
77 |
| - * |
78 |
| - * for await (const { key, value } of store.putMany(source)) { |
79 |
| - * console.info(`put content for key ${key}`) |
80 |
| - * } |
81 |
| - * ``` |
82 |
| - */ |
83 |
| - putMany: ( |
84 |
| - source: AwaitIterable<Pair>, |
85 |
| - options?: Options |
86 |
| - ) => AsyncIterable<Pair> |
87 |
| - /** |
88 |
| - * Retrieve values for the passed keys |
89 |
| - * |
90 |
| - * @example |
91 |
| - * ```js |
92 |
| - * for await (const value of store.getMany([new Key('awesome')])) { |
93 |
| - * console.log('got content:', new TextDecoder('utf8').decode(value)) |
94 |
| - * // => got content: datastore |
95 |
| - * } |
96 |
| - * ``` |
97 |
| - */ |
98 |
| - getMany: ( |
99 |
| - source: AwaitIterable<Key>, |
100 |
| - options?: Options |
101 |
| - ) => AsyncIterable<Uint8Array> |
102 |
| - /** |
103 |
| - * Remove values for the passed keys |
104 |
| - * |
105 |
| - * @example |
106 |
| - * |
107 |
| - * ```js |
108 |
| - * const source = [new Key('awesome')] |
109 |
| - * |
110 |
| - * for await (const key of store.deleteMany(source)) { |
111 |
| - * console.log(`deleted content with key ${key}`) |
112 |
| - * } |
113 |
| - * ``` |
114 |
| - */ |
115 |
| - deleteMany: ( |
116 |
| - source: AwaitIterable<Key>, |
117 |
| - options?: Options |
118 |
| - ) => AsyncIterable<Key> |
119 |
| - /** |
120 |
| - * This will return an object with which you can chain multiple operations together, with them only being executed on calling `commit`. |
121 |
| - * |
122 |
| - * @example |
123 |
| - * ```js |
124 |
| - * const b = store.batch() |
125 |
| - * |
126 |
| - * for (let i = 0; i < 100; i++) { |
127 |
| - * b.put(new Key(`hello${i}`), new TextEncoder('utf8').encode(`hello world ${i}`)) |
128 |
| - * } |
129 |
| - * |
130 |
| - * await b.commit() |
131 |
| - * console.log('put 100 values') |
132 |
| - * ``` |
133 |
| - */ |
134 |
| - batch: () => Batch |
135 |
| - /** |
136 |
| - * Query the store. |
137 |
| - * |
138 |
| - * @example |
139 |
| - * ```js |
140 |
| - * // retrieve __all__ key/value pairs from the store |
141 |
| - * let list = [] |
142 |
| - * for await (const { key, value } of store.query({})) { |
143 |
| - * list.push(value) |
144 |
| - * } |
145 |
| - * console.log('ALL THE VALUES', list) |
146 |
| - * ``` |
147 |
| - */ |
148 |
| - query: (query: Query, options?: Options) => AsyncIterable<Pair> |
149 |
| - /** |
150 |
| - * Query the store. |
151 |
| - * |
152 |
| - * @example |
153 |
| - * ```js |
154 |
| - * // retrieve __all__ keys from the store |
155 |
| - * let list = [] |
156 |
| - * for await (const key of store.queryKeys({})) { |
157 |
| - * list.push(key) |
158 |
| - * } |
159 |
| - * console.log('ALL THE KEYS', key) |
160 |
| - * ``` |
161 |
| - */ |
162 |
| - queryKeys: (query: KeyQuery, options?: Options) => AsyncIterable<Key> |
| 38 | + |
| 39 | +export interface Query extends StoreQuery<Key, Uint8Array> { |
| 40 | + |
| 41 | +} |
| 42 | + |
| 43 | +export interface KeyQueryFilter extends StoreKeyQueryFilter<Key> { |
| 44 | + |
163 | 45 | }
|
164 | 46 |
|
165 |
| -export type QueryFilter = (item: Pair) => boolean |
166 |
| -export type QueryOrder = (a: Pair, b: Pair) => -1 | 0 | 1 |
| 47 | +export interface KeyQueryOrder extends StoreKeyQueryOrder<Key> { |
167 | 48 |
|
168 |
| -export interface Query { |
169 |
| - prefix?: string |
170 |
| - filters?: QueryFilter[] |
171 |
| - orders?: QueryOrder[] |
172 |
| - limit?: number |
173 |
| - offset?: number |
174 | 49 | }
|
175 | 50 |
|
176 |
| -export type KeyQueryFilter = (item: Key) => boolean |
177 |
| -export type KeyQueryOrder = (a: Key, b: Key) => -1 | 0 | 1 |
| 51 | +export interface KeyQuery extends StoreKeyQuery<Key> { |
178 | 52 |
|
179 |
| -export interface KeyQuery { |
180 |
| - prefix?: string |
181 |
| - filters?: KeyQueryFilter[] |
182 |
| - orders?: KeyQueryOrder[] |
183 |
| - limit?: number |
184 |
| - offset?: number |
185 | 53 | }
|
0 commit comments