polystore 0.7.0 → 0.8.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.
- package/package.json +1 -1
- package/readme.md +9 -2
- package/src/index.js +33 -0
- package/src/index.test.js +56 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "polystore",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "A small compatibility layer for many popular KV stores like localStorage, Redis, FileSystem, etc.",
|
|
5
5
|
"homepage": "https://github.com/franciscop/polystore",
|
|
6
6
|
"repository": "https://github.com/franciscop/polystore.git",
|
package/readme.md
CHANGED
|
@@ -22,6 +22,7 @@ This is the [API](#api) with all of the methods (they are all `async`):
|
|
|
22
22
|
- `.entries(prefix?): [string, any][]`: get a list of all the key-value pairs in the store.
|
|
23
23
|
- `.clear()`: delete ALL of the data in the store, effectively resetting it.
|
|
24
24
|
- `.close()`: (only _some_ stores) ends the connection to the store.
|
|
25
|
+
- `.prefix(prefix): store`: create a new sub-instance of the store where all the keys have this prefix.
|
|
25
26
|
|
|
26
27
|
Available stores:
|
|
27
28
|
|
|
@@ -224,9 +225,9 @@ Remove all of the data from the store:
|
|
|
224
225
|
await store.clear();
|
|
225
226
|
```
|
|
226
227
|
|
|
227
|
-
### .prefix() (
|
|
228
|
+
### .prefix() (unstable)
|
|
228
229
|
|
|
229
|
-
Create a sub-store where all the operations use the given prefix:
|
|
230
|
+
Create a sub-store where all the operations use the given prefix. This is **the only method** of the store that is sync and you don't need to await:
|
|
230
231
|
|
|
231
232
|
```js
|
|
232
233
|
const store = kv(new Map());
|
|
@@ -248,8 +249,13 @@ await session.clear(); // delete only keys with the prefix
|
|
|
248
249
|
This will probably never be stable given the nature of some engines, so as an alternative please consider using two stores instead of prefixes:
|
|
249
250
|
|
|
250
251
|
```js
|
|
252
|
+
// Two in-memory stores
|
|
251
253
|
const store = kv(new Map());
|
|
252
254
|
const session = kv(new Map());
|
|
255
|
+
|
|
256
|
+
// Two file-stores
|
|
257
|
+
const users = kv(new URL(`file://${import.meta.dirname}/users.json`));
|
|
258
|
+
const books = kv(new URL(`file://${import.meta.dirname}/books.json`));
|
|
253
259
|
```
|
|
254
260
|
|
|
255
261
|
The main reason this is not stable is because [_some_ store engines don't allow for atomic deletion of keys given a prefix](https://stackoverflow.com/q/4006324/938236). While we do still clear them internally in those cases, that is a non-atomic operation and it could have some trouble if some other thread is reading/writing the data _at the same time_.
|
|
@@ -359,6 +365,7 @@ const store = kv(new URL("file:///Users/me/project/cache.json"));
|
|
|
359
365
|
// Paths need to be absolute, but you can use process.cwd() to make
|
|
360
366
|
// it relative to the current process:
|
|
361
367
|
const store = kv(new URL(`file://${process.cwd()}/cache.json`));
|
|
368
|
+
const store2 = kv(new URL(`file://${import.meta.dirname}/data.json`));
|
|
362
369
|
```
|
|
363
370
|
|
|
364
371
|
### Cloudflare KV
|
package/src/index.js
CHANGED
|
@@ -385,6 +385,39 @@ export default function compat(storeClient = new Map()) {
|
|
|
385
385
|
{},
|
|
386
386
|
{
|
|
387
387
|
get: (instance, key) => {
|
|
388
|
+
if (key === "prefix") {
|
|
389
|
+
const store = compat(storeClient);
|
|
390
|
+
return (prefix) => ({
|
|
391
|
+
get: (key) => store.get(prefix + key),
|
|
392
|
+
set: async (key, value, opts) => {
|
|
393
|
+
const id = await store.set(prefix + key, value, opts);
|
|
394
|
+
return id.slice(prefix.length);
|
|
395
|
+
},
|
|
396
|
+
add: async (value, opts) => {
|
|
397
|
+
const id = await store.set(prefix + generateId(), value, opts);
|
|
398
|
+
return id.slice(prefix.length);
|
|
399
|
+
},
|
|
400
|
+
has: (key) => store.has(prefix + key),
|
|
401
|
+
del: (key) => store.del(prefix + key),
|
|
402
|
+
|
|
403
|
+
keys: async (next = "") => {
|
|
404
|
+
const keys = await store.keys(prefix + next);
|
|
405
|
+
return keys.map((k) => k.slice(prefix.length));
|
|
406
|
+
},
|
|
407
|
+
values: (next = "") => store.values(prefix + next),
|
|
408
|
+
entries: async (next = "") => {
|
|
409
|
+
const entries = await store.entries(prefix + next);
|
|
410
|
+
return entries.map(([k, v]) => [k.slice(prefix.length), v]);
|
|
411
|
+
},
|
|
412
|
+
|
|
413
|
+
clear: async () => {
|
|
414
|
+
const keys = await store.keys(prefix);
|
|
415
|
+
await Promise.all(keys.map((key) => store.del(key)));
|
|
416
|
+
},
|
|
417
|
+
close: () => store.close(),
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
|
|
388
421
|
return async (...args) => {
|
|
389
422
|
// Only once, even if called twice in succession, since the
|
|
390
423
|
// second time will go straight to the await
|
package/src/index.test.js
CHANGED
|
@@ -292,5 +292,61 @@ for (let [name, store] of stores) {
|
|
|
292
292
|
});
|
|
293
293
|
}
|
|
294
294
|
});
|
|
295
|
+
|
|
296
|
+
describe(".prefix()", () => {
|
|
297
|
+
const session = store.prefix("session:");
|
|
298
|
+
|
|
299
|
+
it("has all the methods", () => {
|
|
300
|
+
expect(Object.keys(session)).toEqual([
|
|
301
|
+
"get",
|
|
302
|
+
"set",
|
|
303
|
+
"add",
|
|
304
|
+
"has",
|
|
305
|
+
"del",
|
|
306
|
+
"keys",
|
|
307
|
+
"values",
|
|
308
|
+
"entries",
|
|
309
|
+
"clear",
|
|
310
|
+
"close",
|
|
311
|
+
]);
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
it("can write/read one", async () => {
|
|
315
|
+
const id = await session.set("a", "b");
|
|
316
|
+
expect(id).toBe("a");
|
|
317
|
+
expect(await session.get("a")).toBe("b");
|
|
318
|
+
expect(await store.get("session:a")).toBe("b");
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
it("can add with the prefix", async () => {
|
|
322
|
+
const id = await session.add("b");
|
|
323
|
+
expect(id.length).toBe(24);
|
|
324
|
+
expect(id).not.toMatch(/^session\:/);
|
|
325
|
+
|
|
326
|
+
const keys = await store.keys();
|
|
327
|
+
expect(keys[0]).toMatch(/^session\:/);
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
it("the group operations return the proper values", async () => {
|
|
331
|
+
await session.set("a", "b");
|
|
332
|
+
|
|
333
|
+
expect(await session.keys()).toEqual(["a"]);
|
|
334
|
+
expect(await session.values()).toEqual(["b"]);
|
|
335
|
+
expect(await session.entries()).toEqual([["a", "b"]]);
|
|
336
|
+
|
|
337
|
+
expect(await store.keys()).toEqual(["session:a"]);
|
|
338
|
+
expect(await store.values()).toEqual(["b"]);
|
|
339
|
+
expect(await store.entries()).toEqual([["session:a", "b"]]);
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
it("clears only the substore", async () => {
|
|
343
|
+
await store.set("a", "b");
|
|
344
|
+
await session.set("c", "d");
|
|
345
|
+
|
|
346
|
+
expect((await store.keys()).sort()).toEqual(["a", "session:c"]);
|
|
347
|
+
await session.clear();
|
|
348
|
+
expect(await store.keys()).toEqual(["a"]);
|
|
349
|
+
});
|
|
350
|
+
});
|
|
295
351
|
});
|
|
296
352
|
}
|