polystore 0.15.13 → 0.16.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/index.d.ts +82 -0
- package/index.js +809 -0
- package/package.json +20 -24
- package/readme.md +197 -39
- package/src/clients/Client.js +0 -7
- package/src/clients/api.js +0 -34
- package/src/clients/cloudflare.js +0 -56
- package/src/clients/cookie.js +0 -51
- package/src/clients/etcd.js +0 -29
- package/src/clients/file.js +0 -70
- package/src/clients/folder.js +0 -49
- package/src/clients/forage.js +0 -29
- package/src/clients/index.js +0 -25
- package/src/clients/level.js +0 -40
- package/src/clients/memory.js +0 -19
- package/src/clients/redis.js +0 -51
- package/src/clients/storage.js +0 -25
- package/src/index.d.ts +0 -209
- package/src/index.js +0 -278
- package/src/server.js +0 -81
- package/src/utils.js +0 -47
package/src/clients/cookie.js
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import Client from "./Client.js";
|
|
2
|
-
|
|
3
|
-
// A client that uses a single file (JSON) as a store
|
|
4
|
-
export default class Cookie extends Client {
|
|
5
|
-
// Indicate if this client handles expirations (true = it does)
|
|
6
|
-
EXPIRES = true;
|
|
7
|
-
|
|
8
|
-
// Check if this is the right class for the given client
|
|
9
|
-
static test = (client) => client === "cookie" || client === "cookies";
|
|
10
|
-
|
|
11
|
-
// Group methods
|
|
12
|
-
#read = () => {
|
|
13
|
-
const all = {};
|
|
14
|
-
for (let entry of document.cookie.split(";")) {
|
|
15
|
-
try {
|
|
16
|
-
const [rawKey, rawValue] = entry.split("=");
|
|
17
|
-
const key = decodeURIComponent(rawKey.trim());
|
|
18
|
-
const value = JSON.parse(decodeURIComponent(rawValue.trim()));
|
|
19
|
-
all[key] = value;
|
|
20
|
-
} catch (error) {
|
|
21
|
-
// no-op (some 3rd party can set cookies independently)
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
return all;
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
// For cookies, an empty value is the same as null, even `""`
|
|
28
|
-
get = (key) => this.#read()[key] || null;
|
|
29
|
-
|
|
30
|
-
set = (key, data = null, { expires } = {}) => {
|
|
31
|
-
// Setting it to null deletes it
|
|
32
|
-
let expireStr = "";
|
|
33
|
-
// NOTE: 0 is already considered here!
|
|
34
|
-
if (expires !== null) {
|
|
35
|
-
const time = new Date(Date.now() + expires * 1000).toUTCString();
|
|
36
|
-
expireStr = `; expires=${time}`;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const value = encodeURIComponent(this.encode(data || ""));
|
|
40
|
-
document.cookie = encodeURIComponent(key) + "=" + value + expireStr;
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
del = (key) => this.set(key, "", { expires: -100 });
|
|
44
|
-
|
|
45
|
-
async *iterate(prefix = "") {
|
|
46
|
-
for (let [key, value] of Object.entries(this.#read())) {
|
|
47
|
-
if (!key.startsWith(prefix)) continue;
|
|
48
|
-
yield [key, value];
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
}
|
package/src/clients/etcd.js
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import Client from "./Client.js";
|
|
2
|
-
|
|
3
|
-
// Use a redis client to back up the store
|
|
4
|
-
export default class Etcd extends Client {
|
|
5
|
-
// Check if this is the right class for the given client
|
|
6
|
-
static test = (client) => client?.constructor?.name === "Etcd3";
|
|
7
|
-
|
|
8
|
-
get = (key) => this.client.get(key).json();
|
|
9
|
-
set = (key, value) => this.client.put(key).value(this.encode(value));
|
|
10
|
-
del = (key) => this.client.delete().key(key).exec();
|
|
11
|
-
|
|
12
|
-
async *iterate(prefix = "") {
|
|
13
|
-
const keys = await this.client.getAll().prefix(prefix).keys();
|
|
14
|
-
for (const key of keys) {
|
|
15
|
-
yield [key, await this.get(key)];
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
keys = (prefix = "") => this.client.getAll().prefix(prefix).keys();
|
|
20
|
-
entries = async (prefix = "") => {
|
|
21
|
-
const keys = await this.keys(prefix);
|
|
22
|
-
const values = await Promise.all(keys.map((k) => this.get(k)));
|
|
23
|
-
return keys.map((k, i) => [k, values[i]]);
|
|
24
|
-
};
|
|
25
|
-
clear = async (prefix = "") => {
|
|
26
|
-
if (!prefix) return this.client.delete().all();
|
|
27
|
-
return this.client.delete().prefix(prefix);
|
|
28
|
-
};
|
|
29
|
-
}
|
package/src/clients/file.js
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import Client from "./Client.js";
|
|
2
|
-
|
|
3
|
-
// A client that uses a single file (JSON) as a store
|
|
4
|
-
export default class File extends Client {
|
|
5
|
-
// Check if this is the right class for the given client
|
|
6
|
-
static test = (client) => {
|
|
7
|
-
if (client instanceof URL) client = client.href;
|
|
8
|
-
return (
|
|
9
|
-
typeof client === "string" &&
|
|
10
|
-
client.startsWith("file://") &&
|
|
11
|
-
client.includes(".")
|
|
12
|
-
);
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
// We want to make sure the file already exists, so attempt to
|
|
16
|
-
// create the folders and the file (but not OVERWRITE it, that's why the x flag)
|
|
17
|
-
// It fails if it already exists, hence the catch case
|
|
18
|
-
#promise = (async () => {
|
|
19
|
-
this.fsp = await import("node:fs/promises");
|
|
20
|
-
this.file = (this.client?.href || this.client).replace(/^file:\/\//, "");
|
|
21
|
-
const folder = this.file.split("/").slice(0, -1).join("/");
|
|
22
|
-
await this.fsp.mkdir(folder, { recursive: true }).catch(() => {});
|
|
23
|
-
await this.fsp.writeFile(this.file, "{}", { flag: "wx" }).catch(() => {});
|
|
24
|
-
})();
|
|
25
|
-
|
|
26
|
-
// Internal
|
|
27
|
-
#read = async () => {
|
|
28
|
-
const text = await this.fsp.readFile(this.file, "utf8");
|
|
29
|
-
return text ? JSON.parse(text) : {};
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
#write = async (data) => {
|
|
33
|
-
return this.fsp.writeFile(this.file, this.encode(data));
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
get = async (key) => {
|
|
37
|
-
const data = await this.#read();
|
|
38
|
-
return data[key] ?? null;
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
set = async (key, value) => {
|
|
42
|
-
const data = await this.#read();
|
|
43
|
-
if (value === null) {
|
|
44
|
-
delete data[key];
|
|
45
|
-
} else {
|
|
46
|
-
data[key] = value;
|
|
47
|
-
}
|
|
48
|
-
await this.#write(data);
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
async *iterate(prefix = "") {
|
|
52
|
-
const data = await this.#read();
|
|
53
|
-
const entries = Object.entries(data).filter((p) => p[0].startsWith(prefix));
|
|
54
|
-
for (const entry of entries) {
|
|
55
|
-
yield entry;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// Bulk updates are worth creating a custom method here
|
|
60
|
-
clearAll = () => this.#write({});
|
|
61
|
-
clear = async (prefix = "") => {
|
|
62
|
-
const data = await this.#read();
|
|
63
|
-
for (let key in data) {
|
|
64
|
-
if (key.startsWith(prefix)) {
|
|
65
|
-
delete data[key];
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
await this.#write(data);
|
|
69
|
-
};
|
|
70
|
-
}
|
package/src/clients/folder.js
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import Client from "./Client.js";
|
|
2
|
-
|
|
3
|
-
const noFileOk = (error) => {
|
|
4
|
-
if (error.code === "ENOENT") return null;
|
|
5
|
-
throw error;
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
// A client that uses a single file (JSON) as a store
|
|
9
|
-
export default class Folder extends Client {
|
|
10
|
-
// Check if this is the right class for the given client
|
|
11
|
-
static test = (client) => {
|
|
12
|
-
if (client instanceof URL) client = client.href;
|
|
13
|
-
return (
|
|
14
|
-
typeof client === "string" &&
|
|
15
|
-
client.startsWith("file://") &&
|
|
16
|
-
client.endsWith("/")
|
|
17
|
-
);
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
// Make sure the folder already exists, so attempt to create it
|
|
21
|
-
// It fails if it already exists, hence the catch case
|
|
22
|
-
#promise = (async () => {
|
|
23
|
-
this.fsp = await import("node:fs/promises");
|
|
24
|
-
this.folder = (this.client?.href || this.client).replace(/^file:\/\//, "");
|
|
25
|
-
await this.fsp.mkdir(this.folder, { recursive: true }).catch(() => {});
|
|
26
|
-
})();
|
|
27
|
-
|
|
28
|
-
file = (key) => this.folder + key + ".json";
|
|
29
|
-
|
|
30
|
-
get = (key) => {
|
|
31
|
-
return this.fsp
|
|
32
|
-
.readFile(this.file(key), "utf8")
|
|
33
|
-
.then(this.decode, noFileOk);
|
|
34
|
-
};
|
|
35
|
-
set = (key, value) => {
|
|
36
|
-
return this.fsp.writeFile(this.file(key), this.encode(value), "utf8");
|
|
37
|
-
};
|
|
38
|
-
del = (key) => this.fsp.unlink(this.file(key)).catch(noFileOk);
|
|
39
|
-
|
|
40
|
-
async *iterate(prefix = "") {
|
|
41
|
-
const all = await this.fsp.readdir(this.folder);
|
|
42
|
-
const keys = all.filter((f) => f.startsWith(prefix) && f.endsWith(".json"));
|
|
43
|
-
for (const name of keys) {
|
|
44
|
-
const key = name.slice(0, -".json".length);
|
|
45
|
-
const data = await this.get(key);
|
|
46
|
-
yield [key, data];
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
package/src/clients/forage.js
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import Client from "./Client.js";
|
|
2
|
-
|
|
3
|
-
// Use localForage for managing the KV
|
|
4
|
-
export default class Forage extends Client {
|
|
5
|
-
// Check if this is the right class for the given client
|
|
6
|
-
static test = (client) =>
|
|
7
|
-
client?.defineDriver && client?.dropInstance && client?.INDEXEDDB;
|
|
8
|
-
|
|
9
|
-
get = (key) => this.client.getItem(key);
|
|
10
|
-
set = (key, value) => this.client.setItem(key, value);
|
|
11
|
-
del = (key) => this.client.removeItem(key);
|
|
12
|
-
|
|
13
|
-
async *iterate(prefix = "") {
|
|
14
|
-
const keys = await this.client.keys();
|
|
15
|
-
const list = keys.filter((k) => k.startsWith(prefix));
|
|
16
|
-
for (const key of list) {
|
|
17
|
-
yield [key, await this.get(key)];
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
entries = async (prefix = "") => {
|
|
22
|
-
const all = await this.client.keys();
|
|
23
|
-
const keys = all.filter((k) => k.startsWith(prefix));
|
|
24
|
-
const values = await Promise.all(keys.map((key) => this.get(key)));
|
|
25
|
-
return keys.map((key, i) => [key, values[i]]);
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
clearAll = () => this.client.clear();
|
|
29
|
-
}
|
package/src/clients/index.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import api from "./api.js";
|
|
2
|
-
import cloudflare from "./cloudflare.js";
|
|
3
|
-
import cookie from "./cookie.js";
|
|
4
|
-
import etcd from "./etcd.js";
|
|
5
|
-
import file from "./file.js";
|
|
6
|
-
import folder from "./folder.js";
|
|
7
|
-
import forage from "./forage.js";
|
|
8
|
-
import level from "./level.js";
|
|
9
|
-
import memory from "./memory.js";
|
|
10
|
-
import redis from "./redis.js";
|
|
11
|
-
import storage from "./storage.js";
|
|
12
|
-
|
|
13
|
-
export default {
|
|
14
|
-
api,
|
|
15
|
-
cloudflare,
|
|
16
|
-
cookie,
|
|
17
|
-
etcd,
|
|
18
|
-
file,
|
|
19
|
-
folder,
|
|
20
|
-
forage,
|
|
21
|
-
level,
|
|
22
|
-
memory,
|
|
23
|
-
redis,
|
|
24
|
-
storage,
|
|
25
|
-
};
|
package/src/clients/level.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import Client from "./Client.js";
|
|
2
|
-
|
|
3
|
-
const valueEncoding = "json";
|
|
4
|
-
const notFound = (error) => {
|
|
5
|
-
if (error?.code === "LEVEL_NOT_FOUND") return null;
|
|
6
|
-
throw error;
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
// Level KV DB - https://github.com/Level/level
|
|
10
|
-
export default class Level extends Client {
|
|
11
|
-
// Check if this is the right class for the given client
|
|
12
|
-
static test = (client) => client?.constructor?.name === "ClassicLevel";
|
|
13
|
-
|
|
14
|
-
get = (key) => this.client.get(key, { valueEncoding }).catch(notFound);
|
|
15
|
-
set = (key, value) => this.client.put(key, value, { valueEncoding });
|
|
16
|
-
del = (key) => this.client.del(key);
|
|
17
|
-
|
|
18
|
-
async *iterate(prefix = "") {
|
|
19
|
-
const keys = await this.client.keys().all();
|
|
20
|
-
const list = keys.filter((k) => k.startsWith(prefix));
|
|
21
|
-
for (const key of list) {
|
|
22
|
-
yield [key, await this.get(key)];
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
entries = async (prefix = "") => {
|
|
27
|
-
const keys = await this.client.keys().all();
|
|
28
|
-
const list = keys.filter((k) => k.startsWith(prefix));
|
|
29
|
-
return Promise.all(list.map(async (k) => [k, await this.get(k)]));
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
clearAll = () => this.client.clear();
|
|
33
|
-
clear = async (prefix = "") => {
|
|
34
|
-
const keys = await this.client.keys().all();
|
|
35
|
-
const list = keys.filter((k) => k.startsWith(prefix));
|
|
36
|
-
return this.client.batch(list.map((key) => ({ type: "del", key })));
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
close = () => this.client.close();
|
|
40
|
-
}
|
package/src/clients/memory.js
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import Client from "./Client.js";
|
|
2
|
-
|
|
3
|
-
// Use a Map() as an in-memory client
|
|
4
|
-
export default class Memory extends Client {
|
|
5
|
-
// Check if this is the right class for the given client
|
|
6
|
-
static test = (client) => client instanceof Map;
|
|
7
|
-
|
|
8
|
-
get = (key) => this.client.get(key) ?? null;
|
|
9
|
-
set = (key, data) => this.client.set(key, data);
|
|
10
|
-
del = (key) => this.client.delete(key);
|
|
11
|
-
|
|
12
|
-
*iterate(prefix = "") {
|
|
13
|
-
for (const entry of this.client.entries()) {
|
|
14
|
-
if (entry[0].startsWith(prefix)) yield entry;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
clearAll = () => this.client.clear();
|
|
19
|
-
}
|
package/src/clients/redis.js
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import Client from "./Client.js";
|
|
2
|
-
|
|
3
|
-
// Use a redis client to back up the store
|
|
4
|
-
export default class Redis extends Client {
|
|
5
|
-
// Indicate if this client handles expirations (true = it does)
|
|
6
|
-
EXPIRES = true;
|
|
7
|
-
|
|
8
|
-
// Check if this is the right class for the given client
|
|
9
|
-
static test = (client) => client && client.pSubscribe && client.sSubscribe;
|
|
10
|
-
|
|
11
|
-
get = async (key) => this.decode(await this.client.get(key));
|
|
12
|
-
set = async (key, value, { expires } = {}) => {
|
|
13
|
-
const EX = expires ? Math.round(expires) : undefined;
|
|
14
|
-
return this.client.set(key, this.encode(value), { EX });
|
|
15
|
-
};
|
|
16
|
-
del = (key) => this.client.del(key);
|
|
17
|
-
|
|
18
|
-
has = async (key) => Boolean(await this.client.exists(key));
|
|
19
|
-
|
|
20
|
-
// Go through each of the [key, value] in the set
|
|
21
|
-
async *iterate(prefix = "") {
|
|
22
|
-
const MATCH = prefix + "*";
|
|
23
|
-
for await (const key of this.client.scanIterator({ MATCH })) {
|
|
24
|
-
const value = await this.get(key);
|
|
25
|
-
// By the time this specific value is read, it could be gone!
|
|
26
|
-
if (!value) continue;
|
|
27
|
-
yield [key, value];
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// Optimizing the retrieval of them by not getting their values
|
|
32
|
-
keys = async (prefix = "") => {
|
|
33
|
-
const MATCH = prefix + "*";
|
|
34
|
-
const keys = [];
|
|
35
|
-
for await (const key of this.client.scanIterator({ MATCH })) {
|
|
36
|
-
keys.push(key);
|
|
37
|
-
}
|
|
38
|
-
return keys;
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
// Optimizing the retrieval of them all in bulk by loading the values
|
|
42
|
-
// in parallel
|
|
43
|
-
entries = async (prefix = "") => {
|
|
44
|
-
const keys = await this.keys(prefix);
|
|
45
|
-
const values = await Promise.all(keys.map((k) => this.get(k)));
|
|
46
|
-
return keys.map((k, i) => [k, values[i]]);
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
clearAll = () => this.client.flushAll();
|
|
50
|
-
close = () => this.client.quit();
|
|
51
|
-
}
|
package/src/clients/storage.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import Client from "./Client.js";
|
|
2
|
-
|
|
3
|
-
// A client that uses a single file (JSON) as a store
|
|
4
|
-
export default class WebStorage extends Client {
|
|
5
|
-
// Check if this is the right class for the given client
|
|
6
|
-
static test(client) {
|
|
7
|
-
if (typeof Storage === "undefined") return false;
|
|
8
|
-
return client instanceof Storage;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
// Item methods
|
|
12
|
-
get = (key) => this.decode(this.client[key]);
|
|
13
|
-
set = (key, data) => this.client.setItem(key, this.encode(data));
|
|
14
|
-
del = (key) => this.client.removeItem(key);
|
|
15
|
-
|
|
16
|
-
*iterate(prefix = "") {
|
|
17
|
-
for (const key of Object.keys(this.client)) {
|
|
18
|
-
if (!key.startsWith(prefix)) continue;
|
|
19
|
-
const value = this.get(key);
|
|
20
|
-
if (value) yield [key, value];
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
clearAll = () => this.client.clear();
|
|
25
|
-
}
|
package/src/index.d.ts
DELETED
|
@@ -1,209 +0,0 @@
|
|
|
1
|
-
export type Options = { expires?: number | string | null };
|
|
2
|
-
export type Value = string | { [key: string]: Value } | Value[];
|
|
3
|
-
|
|
4
|
-
export interface Store {
|
|
5
|
-
/**
|
|
6
|
-
* Save the data on an autogenerated key, can add expiration as well:
|
|
7
|
-
*
|
|
8
|
-
* ```js
|
|
9
|
-
* const key1 = await store.add("value1");
|
|
10
|
-
* const key2 = await store.add({ hello: "world" });
|
|
11
|
-
* const key3 = await store.add("value3", { expires: "1h" });
|
|
12
|
-
* ```
|
|
13
|
-
*
|
|
14
|
-
* **[→ Full .add() Docs](https://polystore.dev/documentation#add)**
|
|
15
|
-
*/
|
|
16
|
-
add: <T = Value>(value: T, options?: Options) => Promise<string>;
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Save the data on the given key, can add expiration as well:
|
|
20
|
-
*
|
|
21
|
-
* ```js
|
|
22
|
-
* const key = await store.set("key1", "value1");
|
|
23
|
-
* await store.set("key2", { hello: "world" });
|
|
24
|
-
* await store.set("key3", "value3", { expires: "1h" });
|
|
25
|
-
* ```
|
|
26
|
-
*
|
|
27
|
-
* **[→ Full .set() Docs](https://polystore.dev/documentation#set)**
|
|
28
|
-
*/
|
|
29
|
-
set: <T = Value>(key: string, value: T, options?: Options) => Promise<string>;
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Read a single value from the KV store:
|
|
33
|
-
*
|
|
34
|
-
* ```js
|
|
35
|
-
* const value1 = await store.get("key1");
|
|
36
|
-
* // null (doesn't exist or has expired)
|
|
37
|
-
* const value2 = await store.get("key2");
|
|
38
|
-
* // "value2"
|
|
39
|
-
* const value3 = await store.get("key3");
|
|
40
|
-
* // { hello: "world" }
|
|
41
|
-
* ```
|
|
42
|
-
*
|
|
43
|
-
* **[→ Full .get() Docs](https://polystore.dev/documentation#get)**
|
|
44
|
-
*/
|
|
45
|
-
get: <T = Value>(key: string) => Promise<T | null>;
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Check whether a key exists or not:
|
|
49
|
-
*
|
|
50
|
-
* ```js
|
|
51
|
-
* if (await store.has("key1")) { ... }
|
|
52
|
-
* ```
|
|
53
|
-
*
|
|
54
|
-
* If you are going to use the value, it's better to just read it:
|
|
55
|
-
*
|
|
56
|
-
* ```js
|
|
57
|
-
* const val = await store.get("key1");
|
|
58
|
-
* if (val) { ... }
|
|
59
|
-
* ```
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
* **[→ Full .has() Docs](https://polystore.dev/documentation#has)**
|
|
63
|
-
*/
|
|
64
|
-
has: (key: string) => Promise<boolean>;
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Remove a single key and its value from the store:
|
|
68
|
-
*
|
|
69
|
-
* ```js
|
|
70
|
-
* const key = await store.del("key1");
|
|
71
|
-
* ```
|
|
72
|
-
*
|
|
73
|
-
* **[→ Full .del() Docs](https://polystore.dev/documentation#del)**
|
|
74
|
-
*/
|
|
75
|
-
del: (key: string) => Promise<string>;
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Return an array of the entries, in the [key, value] format:
|
|
79
|
-
*
|
|
80
|
-
* ```js
|
|
81
|
-
* const entries = await store.entries();
|
|
82
|
-
* // [["key1", "value1"], ["key2", { hello: "world" }], ...]
|
|
83
|
-
*
|
|
84
|
-
* // To limit it to a given prefix, use `.prefix()`:
|
|
85
|
-
* const sessions = await store.prefix("session:").entries();
|
|
86
|
-
* ```
|
|
87
|
-
*
|
|
88
|
-
* **[→ Full .entries() Docs](https://polystore.dev/documentation#entries)**
|
|
89
|
-
*/
|
|
90
|
-
entries: <T = Value>() => Promise<[key: string, value: T][]>;
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Return an array of the keys in the store:
|
|
94
|
-
*
|
|
95
|
-
* ```js
|
|
96
|
-
* const keys = await store.keys();
|
|
97
|
-
* // ["key1", "key2", ...]
|
|
98
|
-
*
|
|
99
|
-
* // To limit it to a given prefix, use `.prefix()`:
|
|
100
|
-
* const sessions = await store.prefix("session:").keys();
|
|
101
|
-
* ```
|
|
102
|
-
*
|
|
103
|
-
* **[→ Full .keys() Docs](https://polystore.dev/documentation#keys)**
|
|
104
|
-
*/
|
|
105
|
-
keys: () => Promise<string[]>;
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Return an array of the values in the store:
|
|
109
|
-
*
|
|
110
|
-
* ```js
|
|
111
|
-
* const values = await store.values();
|
|
112
|
-
* // ["value1", { hello: "world" }, ...]
|
|
113
|
-
*
|
|
114
|
-
* // To limit it to a given prefix, use `.prefix()`:
|
|
115
|
-
* const sessions = await store.prefix("session:").values();
|
|
116
|
-
* ```
|
|
117
|
-
*
|
|
118
|
-
* **[→ Full .values() Docs](https://polystore.dev/documentation#values)**
|
|
119
|
-
*/
|
|
120
|
-
values: <T = Value>() => Promise<T[]>;
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Return an object with the keys:values in the store:
|
|
124
|
-
*
|
|
125
|
-
* ```js
|
|
126
|
-
* const obj = await store.all();
|
|
127
|
-
* // { key1: "value1", key2: { hello: "world" }, ... }
|
|
128
|
-
*
|
|
129
|
-
* // To limit it to a given prefix, use `.prefix()`:
|
|
130
|
-
* const sessions = await store.prefix("session:").all();
|
|
131
|
-
* ```
|
|
132
|
-
*
|
|
133
|
-
* **[→ Full .all() Docs](https://polystore.dev/documentation#all)**
|
|
134
|
-
*/
|
|
135
|
-
all: <T = Value>() => Promise<{ [key: string]: T }>;
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Delete all of the records of the store:
|
|
139
|
-
*
|
|
140
|
-
* ```js
|
|
141
|
-
* await store.clear();
|
|
142
|
-
* ```
|
|
143
|
-
*
|
|
144
|
-
* It's useful for cache invalidation, clearing the data, and testing.
|
|
145
|
-
*
|
|
146
|
-
* **[→ Full .clear() Docs](https://polystore.dev/documentation#clear)**
|
|
147
|
-
*/
|
|
148
|
-
clear: () => Promise<null>;
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* Create a substore where all the keys are stored with
|
|
152
|
-
* the given prefix:
|
|
153
|
-
*
|
|
154
|
-
* ```js
|
|
155
|
-
* const session = store.prefix("session:");
|
|
156
|
-
* await session.set("key1", "value1");
|
|
157
|
-
* console.log(await session.entries()); // session.
|
|
158
|
-
* // [["key1", "value1"]]
|
|
159
|
-
* console.log(await store.entries()); // store.
|
|
160
|
-
* // [["session:key1", "value1"]]
|
|
161
|
-
* ```
|
|
162
|
-
*
|
|
163
|
-
* **[→ Full .prefix() Docs](https://polystore.dev/documentation#prefix)**
|
|
164
|
-
*/
|
|
165
|
-
prefix: (prefix: string) => Store;
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Stop the connection to the store, if any:
|
|
169
|
-
*
|
|
170
|
-
* ```js
|
|
171
|
-
* await session.set("key1", "value1");
|
|
172
|
-
* await store.close();
|
|
173
|
-
* await session.set("key2", "value2"); // error
|
|
174
|
-
* ```
|
|
175
|
-
*
|
|
176
|
-
* **[→ Full .close() Docs](https://polystore.dev/documentation#close)**
|
|
177
|
-
*/
|
|
178
|
-
close?: () => Promise<null>;
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
* An iterator that goes through all of the key:value pairs in the client
|
|
182
|
-
*
|
|
183
|
-
* ```js
|
|
184
|
-
* for await (const [key, value] of store) {
|
|
185
|
-
* console.log(key, value);
|
|
186
|
-
* }
|
|
187
|
-
* ```
|
|
188
|
-
*
|
|
189
|
-
* **[→ Full Iterator Docs](https://polystore.dev/documentation#iterator)**
|
|
190
|
-
*/
|
|
191
|
-
[Symbol.asyncIterator]: () => {
|
|
192
|
-
next: () => Promise<{ value: [string, Value] }>;
|
|
193
|
-
};
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* Create a Store instance with the given client:
|
|
198
|
-
*
|
|
199
|
-
* ```js
|
|
200
|
-
* import kv from "polystore";
|
|
201
|
-
* const store1 = kv(new Map());
|
|
202
|
-
* const store2 = kv(localStorage);
|
|
203
|
-
* const store3 = kv(redisClient);
|
|
204
|
-
* const store4 = kv(yourOwnStore);
|
|
205
|
-
* ```
|
|
206
|
-
*
|
|
207
|
-
* **[→ Full kv() Docs](https://polystore.dev/documentation)**
|
|
208
|
-
*/
|
|
209
|
-
export default function (client?: any): Store;
|