polystore 0.15.13 → 0.16.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts ADDED
@@ -0,0 +1,243 @@
1
+ type Options = {
2
+ expires?: number | null | string;
3
+ };
4
+ type StoreData<T extends Serializable = Serializable> = {
5
+ value: T;
6
+ expires: number | null;
7
+ };
8
+ type Serializable = string | number | boolean | null | (Serializable | null)[] | {
9
+ [key: string]: Serializable | null;
10
+ };
11
+ interface ClientExpires {
12
+ EXPIRES: true;
13
+ promise?: Promise<any>;
14
+ test?: (client: any) => boolean;
15
+ get<T extends Serializable>(key: string): Promise<T | null> | T | null;
16
+ set<T extends Serializable>(key: string, value: T, options?: Options): Promise<any> | any;
17
+ iterate<T extends Serializable>(prefix: string): AsyncGenerator<[string, T], void, unknown> | Generator<[string, T], void, unknown>;
18
+ add?<T extends Serializable>(prefix: string, value: T, options?: Options): Promise<string>;
19
+ has?(key: string): Promise<boolean> | boolean;
20
+ del?(key: string): Promise<any> | any;
21
+ keys?(prefix: string): Promise<string[]> | string[];
22
+ values?<T extends Serializable>(prefix: string): Promise<T[]> | T[];
23
+ entries?<T extends Serializable>(prefix: string): Promise<[string, T][]> | [string, T][];
24
+ all?<T extends Serializable>(prefix: string): Promise<Record<string, T>> | Record<string, T>;
25
+ clear?(prefix: string): Promise<any> | any;
26
+ clearAll?(): Promise<any> | any;
27
+ close?(): Promise<any> | any;
28
+ }
29
+ interface ClientNonExpires {
30
+ EXPIRES: false;
31
+ promise?: Promise<any>;
32
+ test?: (client: any) => boolean;
33
+ get<T extends Serializable>(key: string): Promise<StoreData<T> | null> | StoreData<T> | null;
34
+ set<T extends Serializable>(key: string, value: StoreData<T> | null, options?: Options): Promise<any> | any;
35
+ iterate<T extends Serializable>(prefix: string): AsyncGenerator<[string, StoreData<T>], void, unknown> | Generator<[string, StoreData<T>], void, unknown>;
36
+ add?<T extends Serializable>(prefix: string, value: StoreData<T>, options?: Options): Promise<string>;
37
+ has?(key: string): Promise<boolean> | boolean;
38
+ del?(key: string): Promise<any> | any;
39
+ keys?(prefix: string): Promise<string[]> | string[];
40
+ values?<T extends Serializable>(prefix: string): Promise<StoreData<T>[]> | StoreData<T>[];
41
+ entries?<T extends Serializable>(prefix: string): Promise<[string, StoreData<T>][]> | [string, StoreData<T>][];
42
+ all?<T extends Serializable>(prefix: string): Promise<Record<string, StoreData<T>>> | Record<string, StoreData<T>>;
43
+ clear?(prefix: string): Promise<any> | any;
44
+ clearAll?(): Promise<any> | any;
45
+ close?(): Promise<any> | any;
46
+ }
47
+ type Client = ClientExpires | ClientNonExpires;
48
+
49
+ declare class Store<TDefault extends Serializable = Serializable> {
50
+ #private;
51
+ PREFIX: string;
52
+ promise: Promise<Client> | null;
53
+ client: Client;
54
+ constructor(clientPromise?: any);
55
+ /**
56
+ * Save the data on an autogenerated key, can add expiration as well:
57
+ *
58
+ * ```js
59
+ * const key1 = await store.add("value1");
60
+ * const key2 = await store.add({ hello: "world" });
61
+ * const key3 = await store.add("value3", { expires: "1h" });
62
+ * ```
63
+ *
64
+ * **[→ Full .add() Docs](https://polystore.dev/documentation#add)**
65
+ */
66
+ add(value: TDefault, options?: Options): Promise<string>;
67
+ add<T extends TDefault>(value: T, options?: Options): Promise<string>;
68
+ /**
69
+ * Save the data on the given key, can add expiration as well:
70
+ *
71
+ * ```js
72
+ * const key = await store.set("key1", "value1");
73
+ * await store.set("key2", { hello: "world" });
74
+ * await store.set("key3", "value3", { expires: "1h" });
75
+ * ```
76
+ *
77
+ * **[→ Full .set() Docs](https://polystore.dev/documentation#set)**
78
+ */
79
+ set(key: string, value: TDefault, options?: Options): Promise<string>;
80
+ set<T extends TDefault>(key: string, value: T, options?: Options): Promise<string>;
81
+ /**
82
+ * Read a single value from the KV store:
83
+ *
84
+ * ```js
85
+ * const value1 = await store.get("key1");
86
+ * // null (doesn't exist or has expired)
87
+ * const value2 = await store.get("key2");
88
+ * // "value2"
89
+ * const value3 = await store.get("key3");
90
+ * // { hello: "world" }
91
+ * ```
92
+ *
93
+ * **[→ Full .get() Docs](https://polystore.dev/documentation#get)**
94
+ */
95
+ get(key: string): Promise<TDefault | null>;
96
+ get<T extends TDefault>(key: string): Promise<T | null>;
97
+ /**
98
+ * Check whether a key exists or not:
99
+ *
100
+ * ```js
101
+ * if (await store.has("key1")) { ... }
102
+ * ```
103
+ *
104
+ * If you are going to use the value, it's better to just read it:
105
+ *
106
+ * ```js
107
+ * const val = await store.get("key1");
108
+ * if (val) { ... }
109
+ * ```
110
+ *
111
+ * **[→ Full .has() Docs](https://polystore.dev/documentation#has)**
112
+ */
113
+ has(key: string): Promise<boolean>;
114
+ /**
115
+ * Remove a single key and its value from the store:
116
+ *
117
+ * ```js
118
+ * const key = await store.del("key1");
119
+ * ```
120
+ *
121
+ * **[→ Full .del() Docs](https://polystore.dev/documentation#del)**
122
+ */
123
+ del(key: string): Promise<string>;
124
+ /**
125
+ * An iterator that goes through all of the key:value pairs in the client
126
+ *
127
+ * ```js
128
+ * for await (const [key, value] of store) {
129
+ * console.log(key, value);
130
+ * }
131
+ * ```
132
+ *
133
+ * **[→ Full Iterator Docs](https://polystore.dev/documentation#iterator)**
134
+ */
135
+ [Symbol.asyncIterator](): AsyncGenerator<[string, TDefault], void, unknown>;
136
+ [Symbol.asyncIterator]<T extends TDefault>(): AsyncGenerator<[
137
+ string,
138
+ T
139
+ ], void, unknown>;
140
+ /**
141
+ * Return an array of the entries, in the [key, value] format:
142
+ *
143
+ * ```js
144
+ * const entries = await store.entries();
145
+ * // [["key1", "value1"], ["key2", { hello: "world" }], ...]
146
+ *
147
+ * // To limit it to a given prefix, use `.prefix()`:
148
+ * const sessions = await store.prefix("session:").entries();
149
+ * ```
150
+ *
151
+ * **[→ Full .entries() Docs](https://polystore.dev/documentation#entries)**
152
+ */
153
+ entries(): Promise<[string, TDefault][]>;
154
+ entries<T extends TDefault>(): Promise<[string, T][]>;
155
+ /**
156
+ * Return an array of the keys in the store:
157
+ *
158
+ * ```js
159
+ * const keys = await store.keys();
160
+ * // ["key1", "key2", ...]
161
+ *
162
+ * // To limit it to a given prefix, use `.prefix()`:
163
+ * const sessions = await store.prefix("session:").keys();
164
+ * ```
165
+ *
166
+ * **[→ Full .keys() Docs](https://polystore.dev/documentation#keys)**
167
+ */
168
+ keys(): Promise<string[]>;
169
+ /**
170
+ * Return an array of the values in the store:
171
+ *
172
+ * ```js
173
+ * const values = await store.values();
174
+ * // ["value1", { hello: "world" }, ...]
175
+ *
176
+ * // To limit it to a given prefix, use `.prefix()`:
177
+ * const sessions = await store.prefix("session:").values();
178
+ * ```
179
+ *
180
+ * **[→ Full .values() Docs](https://polystore.dev/documentation#values)**
181
+ */
182
+ values(): Promise<TDefault[]>;
183
+ values<T extends TDefault>(): Promise<T[]>;
184
+ /**
185
+ * Return an object with the keys:values in the store:
186
+ *
187
+ * ```js
188
+ * const obj = await store.all();
189
+ * // { key1: "value1", key2: { hello: "world" }, ... }
190
+ *
191
+ * // To limit it to a given prefix, use `.prefix()`:
192
+ * const sessions = await store.prefix("session:").all();
193
+ * ```
194
+ *
195
+ * **[→ Full .all() Docs](https://polystore.dev/documentation#all)**
196
+ */
197
+ all(): Promise<Record<string, TDefault>>;
198
+ all<T extends TDefault>(): Promise<Record<string, T>>;
199
+ /**
200
+ * Delete all of the records of the store:
201
+ *
202
+ * ```js
203
+ * await store.clear();
204
+ * ```
205
+ *
206
+ * It's useful for cache invalidation, clearing the data, and testing.
207
+ *
208
+ * **[→ Full .clear() Docs](https://polystore.dev/documentation#clear)**
209
+ */
210
+ clear(): Promise<void>;
211
+ /**
212
+ * Create a substore where all the keys are stored with
213
+ * the given prefix:
214
+ *
215
+ * ```js
216
+ * const session = store.prefix("session:");
217
+ * await session.set("key1", "value1");
218
+ * console.log(await session.entries()); // session.
219
+ * // [["key1", "value1"]]
220
+ * console.log(await store.entries()); // store.
221
+ * // [["session:key1", "value1"]]
222
+ * ```
223
+ *
224
+ * **[→ Full .prefix() Docs](https://polystore.dev/documentation#prefix)**
225
+ */
226
+ prefix(prefix?: string): Store<TDefault>;
227
+ /**
228
+ * Stop the connection to the store, if any:
229
+ *
230
+ * ```js
231
+ * await session.set("key1", "value1");
232
+ * await store.close();
233
+ * await session.set("key2", "value2"); // error
234
+ * ```
235
+ *
236
+ * **[→ Full .close() Docs](https://polystore.dev/documentation#close)**
237
+ */
238
+ close(): Promise<void>;
239
+ }
240
+ declare function createStore(): Store<Serializable>;
241
+ declare function createStore<T extends Serializable = Serializable>(client?: any): Store<T>;
242
+
243
+ export { type Client, type Serializable, Store, createStore as default };