polystore 0.15.0 → 0.15.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/package.json +1 -1
- package/readme.md +6 -3
- package/src/clients/file.js +2 -1
- package/src/clients/folder.js +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "polystore",
|
|
3
|
-
"version": "0.15.
|
|
3
|
+
"version": "0.15.1",
|
|
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/readme.md
CHANGED
|
@@ -700,7 +700,7 @@ await fsp.writeFile(file, serialValue);
|
|
|
700
700
|
|
|
701
701
|
### Folder
|
|
702
702
|
|
|
703
|
-
Treat a single folder in your filesystem as the
|
|
703
|
+
Treat a single folder in your filesystem as the store, where each key is a file:
|
|
704
704
|
|
|
705
705
|
```js
|
|
706
706
|
import kv from "polystore";
|
|
@@ -708,6 +708,7 @@ import kv from "polystore";
|
|
|
708
708
|
const store = kv(new URL("file:///Users/me/project/data/"));
|
|
709
709
|
|
|
710
710
|
await store.set("key1", "Hello world", { expires: "1h" });
|
|
711
|
+
// Writes "./data/key1.json"
|
|
711
712
|
console.log(await store.get("key1"));
|
|
712
713
|
// "Hello world"
|
|
713
714
|
```
|
|
@@ -717,12 +718,14 @@ console.log(await store.get("key1"));
|
|
|
717
718
|
You can also create multiple stores:
|
|
718
719
|
|
|
719
720
|
```js
|
|
720
|
-
// Paths need to be absolute, but you can use process.cwd() to make
|
|
721
|
-
// it relative to the current process
|
|
721
|
+
// Paths need to be absolute, but you can use `process.cwd()` to make
|
|
722
|
+
// it relative to the current process, or `import.meta.dirname`:
|
|
722
723
|
const store1 = kv(new URL(`file://${process.cwd()}/cache/`));
|
|
723
724
|
const store2 = kv(new URL(`file://${import.meta.dirname}/data/`));
|
|
724
725
|
```
|
|
725
726
|
|
|
727
|
+
The folder is created if it doesn't exist. When a key is deleted, the corresponding file is also deleted. The data is serialized as JSON, with a meta wrapper to store the expiration date.
|
|
728
|
+
|
|
726
729
|
<details>
|
|
727
730
|
<summary>Why use polystore with a folder?</summary>
|
|
728
731
|
<p>These benefits are for wrapping a folder with polystore:</p>
|
package/src/clients/file.js
CHANGED
|
@@ -29,7 +29,8 @@ export default class File {
|
|
|
29
29
|
// We want to make sure the file already exists, so attempt to
|
|
30
30
|
// create the folders and the file (but not OVERWRITE it, that's why the x flag)
|
|
31
31
|
// It fails if it already exists, hence the catch case
|
|
32
|
-
|
|
32
|
+
const folder = path.dirname(this.file);
|
|
33
|
+
await fsp.mkdir(folder, { recursive: true }).catch(() => {});
|
|
33
34
|
await fsp.writeFile(this.file, "{}", { flag: "wx" }).catch((err) => {
|
|
34
35
|
if (err.code !== "EEXIST") throw err;
|
|
35
36
|
});
|
package/src/clients/folder.js
CHANGED
|
@@ -32,7 +32,7 @@ export default class Folder {
|
|
|
32
32
|
|
|
33
33
|
// Make sure the folder already exists, so attempt to create it
|
|
34
34
|
// It fails if it already exists, hence the catch case
|
|
35
|
-
await fsp.mkdir(this.folder, { recursive: true }).catch((
|
|
35
|
+
await fsp.mkdir(this.folder, { recursive: true }).catch(() => {});
|
|
36
36
|
return fsp;
|
|
37
37
|
})();
|
|
38
38
|
}
|