polystore 0.11.2 → 0.11.3

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +6 -196
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "polystore",
3
- "version": "0.11.2",
3
+ "version": "0.11.3",
4
4
  "description": "A small compatibility layer for many popular KV stores like localStorage, Redis, FileSystem, etc.",
5
5
  "homepage": "https://polystore.dev/",
6
6
  "repository": "https://github.com/franciscop/polystore.git",
package/src/index.js CHANGED
@@ -1,12 +1,6 @@
1
- /**
2
- * A number, or a string containing a number.
3
- * @typedef {(number|string|object|array)} Value
4
- */
5
-
6
1
  import clients from "./clients/index.js";
7
2
  import { createId, isClass, parse } from "./utils.js";
8
3
 
9
- // #region Store
10
4
  class Store {
11
5
  PREFIX = "";
12
6
 
@@ -21,7 +15,6 @@ class Store {
21
15
  });
22
16
  }
23
17
 
24
- // #region #client()
25
18
  #find(store) {
26
19
  // Already a fully compliant KV store
27
20
  if (store instanceof Store) return store.client;
@@ -41,7 +34,6 @@ class Store {
41
34
  return store;
42
35
  }
43
36
 
44
- // #region #validate()
45
37
  #validate(client) {
46
38
  if (!client.set || !client.get || !client.iterate) {
47
39
  throw new Error(
@@ -93,21 +85,6 @@ class Store {
93
85
  return false;
94
86
  }
95
87
 
96
- // #region .add()
97
- /**
98
- * Save the data on an autogenerated key, can add expiration as well:
99
- *
100
- * ```js
101
- * const key1 = await store.add("value1");
102
- * const key2 = await store.add({ hello: "world" });
103
- * const key3 = await store.add("value3", { expires: "1h" });
104
- * ```
105
- *
106
- * **[→ Full .add() Docs](https://polystore.dev/documentation#add)**
107
- * @param {Value} value
108
- * @param {{ expires: string }} options
109
- * @returns {Promise<string>}
110
- */
111
88
  async add(value, options = {}) {
112
89
  await this.promise;
113
90
  const expires = parse(options.expire ?? options.expires);
@@ -130,22 +107,6 @@ class Store {
130
107
  return id; // The plain one without the prefix
131
108
  }
132
109
 
133
- // #region .set()
134
- /**
135
- * Save the data on the given key, can add expiration as well:
136
- *
137
- * ```js
138
- * const key = await store.set("key1", "value1");
139
- * await store.set("key2", { hello: "world" });
140
- * await store.set("key3", "value3", { expires: "1h" });
141
- * ```
142
- *
143
- * **[→ Full .set() Docs](https://polystore.dev/documentation#set)**
144
- * @param {string} key
145
- * @param {Value} value
146
- * @param {{ expires: string }} options
147
- * @returns {Promise<string>}
148
- */
149
110
  async set(key, value, options = {}) {
150
111
  await this.promise;
151
112
  const id = this.PREFIX + key;
@@ -168,23 +129,6 @@ class Store {
168
129
  return key;
169
130
  }
170
131
 
171
- // #region .get()
172
- /**
173
- * Read a single value from the KV store:
174
- *
175
- * ```js
176
- * const value1 = await store.get("key1");
177
- * // null (doesn't exist or has expired)
178
- * const value2 = await store.get("key2");
179
- * // "value2"
180
- * const value3 = await store.get("key3");
181
- * // { hello: "world" }
182
- * ```
183
- *
184
- * **[→ Full .get() Docs](https://polystore.dev/documentation#get)**
185
- * @param {string} key
186
- * @returns {Promise<Value>}
187
- */
188
132
  async get(key) {
189
133
  await this.promise;
190
134
  const id = this.PREFIX + key;
@@ -202,26 +146,6 @@ class Store {
202
146
  return data.value;
203
147
  }
204
148
 
205
- // #region .has()
206
- /**
207
- * Check whether a key exists or not:
208
- *
209
- * ```js
210
- * if (await store.has("key1")) { ... }
211
- * ```
212
- *
213
- * If you are going to use the value, it's better to just read it:
214
- *
215
- * ```js
216
- * const val = await store.get("key1");
217
- * if (val) { ... }
218
- * ```
219
- *
220
- *
221
- * **[→ Full .has() Docs](https://polystore.dev/documentation#has)**
222
- * @param {string} key
223
- * @returns {Promise<boolean>}
224
- */
225
149
  async has(key) {
226
150
  await this.promise;
227
151
  const id = this.PREFIX + key;
@@ -234,18 +158,6 @@ class Store {
234
158
  return value !== null;
235
159
  }
236
160
 
237
- // #region .del()
238
- /**
239
- * Remove a single key and its value from the store:
240
- *
241
- * ```js
242
- * const key = await store.del("key1");
243
- * ```
244
- *
245
- * **[→ Full .del() Docs](https://polystore.dev/documentation#del)**
246
- * @param {string} key
247
- * @returns {Promise<string>}
248
- */
249
161
  async del(key) {
250
162
  await this.promise;
251
163
  const id = this.PREFIX + key;
@@ -272,33 +184,19 @@ class Store {
272
184
  }
273
185
  }
274
186
 
275
- // #region .entries()
276
- /**
277
- * Return an array of the entries, in the [key, value] format:
278
- *
279
- * ```js
280
- * const entries = await store.entries();
281
- * // [["key1", "value1"], ["key2", { hello: "world" }], ...]
282
- *
283
- * // To limit it to a given prefix, use `.prefix()`:
284
- * const sessions = await store.prefix("session:").entries();
285
- * ```
286
- *
287
- * **[→ Full .entries() Docs](https://polystore.dev/documentation#entries)**
288
- * @returns {Promise<[string, Value][]>}
289
- */
290
187
  async entries() {
291
188
  await this.promise;
292
189
 
190
+ // Cut the key to size
191
+ const trim = (key) => key.slice(this.PREFIX.length);
192
+
293
193
  let list = [];
294
194
  if (this.client.entries) {
295
- list = (await this.client.entries(this.PREFIX)).map(([key, value]) => [
296
- key.slice(this.PREFIX.length),
297
- value,
298
- ]);
195
+ const entries = await this.client.entries(this.PREFIX);
196
+ list = entries.map(([key, value]) => [trim(key), value]);
299
197
  } else {
300
198
  for await (const [key, value] of this.client.iterate(this.PREFIX)) {
301
- list.push([key.slice(this.PREFIX.length), value]);
199
+ list.push([trim(key), value]);
302
200
  }
303
201
  }
304
202
 
@@ -312,21 +210,6 @@ class Store {
312
210
  .map(([key, data]) => [key, data.value]);
313
211
  }
314
212
 
315
- // #region .keys()
316
- /**
317
- * Return an array of the keys in the store:
318
- *
319
- * ```js
320
- * const keys = await store.keys();
321
- * // ["key1", "key2", ...]
322
- *
323
- * // To limit it to a given prefix, use `.prefix()`:
324
- * const sessions = await store.prefix("session:").keys();
325
- * ```
326
- *
327
- * **[→ Full .keys() Docs](https://polystore.dev/documentation#keys)**
328
- * @returns {Promise<string[]>}
329
- */
330
213
  async keys() {
331
214
  await this.promise;
332
215
 
@@ -340,21 +223,6 @@ class Store {
340
223
  return entries.map((e) => e[0]);
341
224
  }
342
225
 
343
- // #region .values()
344
- /**
345
- * Return an array of the values in the store:
346
- *
347
- * ```js
348
- * const values = await store.values();
349
- * // ["value1", { hello: "world" }, ...]
350
- *
351
- * // To limit it to a given prefix, use `.prefix()`:
352
- * const sessions = await store.prefix("session:").values();
353
- * ```
354
- *
355
- * **[→ Full .values() Docs](https://polystore.dev/documentation#values)**
356
- * @returns {Promise<Value[]>}
357
- */
358
226
  async values() {
359
227
  await this.promise;
360
228
 
@@ -370,21 +238,6 @@ class Store {
370
238
  return entries.map((e) => e[1]);
371
239
  }
372
240
 
373
- // #region .all()
374
- /**
375
- * Return an object with the keys:values in the store:
376
- *
377
- * ```js
378
- * const obj = await store.all();
379
- * // { key1: "value1", key2: { hello: "world" }, ... }
380
- *
381
- * // To limit it to a given prefix, use `.prefix()`:
382
- * const sessions = await store.prefix("session:").all();
383
- * ```
384
- *
385
- * **[→ Full .all() Docs](https://polystore.dev/documentation#all)**
386
- * @returns {Promise<{ [key:string]: Value }>}
387
- */
388
241
  async all() {
389
242
  await this.promise;
390
243
 
@@ -402,19 +255,6 @@ class Store {
402
255
  return Object.fromEntries(entries);
403
256
  }
404
257
 
405
- // #region .clear()
406
- /**
407
- * Delete all of the records of the store:
408
- *
409
- * ```js
410
- * await store.clear();
411
- * ```
412
- *
413
- * It's useful for cache invalidation, clearing the data, and testing.
414
- *
415
- * **[→ Full .clear() Docs](https://polystore.dev/documentation#clear)**
416
- * @returns {Promise<null>}
417
- */
418
258
  async clear() {
419
259
  await this.promise;
420
260
 
@@ -427,23 +267,6 @@ class Store {
427
267
  await Promise.all(keys.map((key) => this.del(key)));
428
268
  }
429
269
 
430
- // #region .prefix()
431
- /**
432
- * Create a substore where all the keys are stored with
433
- * the given prefix:
434
- *
435
- * ```js
436
- * const session = store.prefix("session:");
437
- * await session.set("key1", "value1");
438
- * console.log(await session.entries()); // session.
439
- * // [["key1", "value1"]]
440
- * console.log(await store.entries()); // store.
441
- * // [["session:key1", "value1"]]
442
- * ```
443
- *
444
- * **[→ Full .prefix() Docs](https://polystore.dev/documentation#prefix)**
445
- * @returns {Store}
446
- */
447
270
  prefix(prefix = "") {
448
271
  const store = new Store(
449
272
  Promise.resolve(this.promise).then((client) => client || this.client)
@@ -452,19 +275,6 @@ class Store {
452
275
  return store;
453
276
  }
454
277
 
455
- // #region .close()
456
- /**
457
- * Stop the connection to the store, if any:
458
- *
459
- * ```js
460
- * await session.set("key1", "value1");
461
- * await store.close();
462
- * await session.set("key2", "value2"); // error
463
- * ```
464
- *
465
- * **[→ Full .close() Docs](https://polystore.dev/documentation#close)**
466
- * @returns {Store}
467
- */
468
278
  async close() {
469
279
  await this.promise;
470
280