polystore 0.12.1 → 0.13.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "polystore",
3
- "version": "0.12.1",
3
+ "version": "0.13.0",
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",
@@ -17,7 +17,8 @@
17
17
  "size": "echo $(gzip -c src/index.js | wc -c) bytes",
18
18
  "lint": "check-dts test/index.types.ts",
19
19
  "start": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watch --coverage --detectOpenHandles",
20
- "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage --ci --watchAll=false --detectOpenHandles"
20
+ "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage --ci --watchAll=false --detectOpenHandles",
21
+ "db": "etcd"
21
22
  },
22
23
  "keywords": [
23
24
  "kv",
package/readme.md CHANGED
@@ -1,4 +1,4 @@
1
- # Polystore [![npm install polystore](https://img.shields.io/badge/npm%20install-polystore-blue.svg)](https://www.npmjs.com/package/polystore) [![test badge](https://github.com/franciscop/polystore/workflows/tests/badge.svg "test badge")](https://github.com/franciscop/polystore/blob/master/.github/workflows/tests.yml) [![gzip size](https://badgen.net/bundlephobia/minzip/polystore?label=gzip&color=green)](https://github.com/franciscop/polystore/blob/master/src/index.js)
1
+ # Polystore [![npm install polystore](https://img.shields.io/badge/npm%20install-polystore-blue.svg)](https://www.npmjs.com/package/polystore) [![test badge](https://github.com/franciscop/polystore/workflows/tests/badge.svg "test badge")](https://github.com/franciscop/polystore/blob/master/.github/workflows/tests.yml) [![gzip size](https://badgen.net/bundlephobia/minzip/polystore?label=gzip&color=green)](https://bundlephobia.com/package/polystore)
2
2
 
3
3
  A key-value library to unify the API of [many clients](#clients), like localStorage, Redis, FileSystem, etc:
4
4
 
@@ -354,7 +354,7 @@ const sessions = Object.fromEntries(sessionEntries);
354
354
 
355
355
  ### .clear()
356
356
 
357
- Remove all of the data from the store:
357
+ Remove all of the data from the store and resets it to the original state:
358
358
 
359
359
  ```js
360
360
  await store.clear();
package/src/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import clients from "./clients/index.js";
2
- import { createId, isClass, parse } from "./utils.js";
2
+ import { createId, isClass, parse, unix } from "./utils.js";
3
3
 
4
4
  class Store {
5
5
  PREFIX = "";
@@ -42,18 +42,13 @@ class Store {
42
42
  for (let method of ["has", "keys", "values"]) {
43
43
  if (client[method]) {
44
44
  throw new Error(
45
- `You can only define client.${method}() when the client manages the expiration; otherwise please do NOT define .${method}() and let us manage it`
45
+ `You can only define client.${method}() when the client manages the expiration; otherwise please do NOT define .${method}() and let us manage it`,
46
46
  );
47
47
  }
48
48
  }
49
49
  }
50
50
  }
51
51
 
52
- #unix(expires) {
53
- const now = new Date().getTime();
54
- return expires === null ? null : now + expires * 1000;
55
- }
56
-
57
52
  // Check if the given data is fresh or not; if
58
53
  #isFresh(data, key) {
59
54
  // Should never happen, but COULD happen; schedule it for
@@ -76,30 +71,29 @@ class Store {
76
71
 
77
72
  async add(value, options = {}) {
78
73
  await this.promise;
79
- const expires = parse(options.expire ?? options.expires);
74
+ let expires = parse(options.expire ?? options.expires);
80
75
 
81
76
  // Use the underlying one from the client if found
82
77
  if (this.client.add) {
83
78
  if (this.client.EXPIRES) {
84
- return this.client.add(this.PREFIX, value, { expires });
79
+ return await this.client.add(this.PREFIX, value, { expires });
85
80
  }
86
81
 
87
82
  // In the data we need the timestamp since we need it "absolute":
88
- return this.client.add(this.PREFIX, {
89
- expires: this.#unix(expires),
90
- value,
91
- });
83
+ expires = unix(expires);
84
+ const key = await this.client.add(this.PREFIX, { expires, value });
85
+ return key;
92
86
  }
93
87
 
94
- const id = createId();
95
- await this.set(id, value, { expires });
96
- return id; // The plain one without the prefix
88
+ const key = createId();
89
+ await this.set(key, value, { expires });
90
+ return key; // The plain one without the prefix
97
91
  }
98
92
 
99
93
  async set(key, value, options = {}) {
100
94
  await this.promise;
101
95
  const id = this.PREFIX + key;
102
- const expires = parse(options.expire ?? options.expires);
96
+ let expires = parse(options.expire ?? options.expires);
103
97
 
104
98
  // Quick delete
105
99
  if (value === null || (typeof expires === "number" && expires <= 0)) {
@@ -114,7 +108,8 @@ class Store {
114
108
  }
115
109
 
116
110
  // In the data we need the timestamp since we need it "absolute":
117
- await this.client.set(id, { expires: this.#unix(expires), value });
111
+ expires = unix(expires);
112
+ await this.client.set(id, { expires, value });
118
113
  return key;
119
114
  }
120
115
 
@@ -258,7 +253,7 @@ class Store {
258
253
 
259
254
  prefix(prefix = "") {
260
255
  const store = new Store(
261
- Promise.resolve(this.promise).then((client) => client || this.client)
256
+ Promise.resolve(this.promise).then((client) => client || this.client),
262
257
  );
263
258
  store.PREFIX = this.PREFIX + prefix;
264
259
  return store;
package/src/utils.js CHANGED
@@ -47,3 +47,8 @@ export function isClass(func) {
47
47
  /^class\s/.test(Function.prototype.toString.call(func))
48
48
  );
49
49
  }
50
+
51
+ export function unix(expires) {
52
+ const now = new Date().getTime();
53
+ return expires === null ? null : now + expires * 1000;
54
+ }