Back to packages list

Vals using @keyvhq/core

Description from the NPM package:
Simple key-value storage with support for multiple backends
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { extractValInfo } from "https://esm.town/v/pomdtr/extractValInfo?v=26";
import { openKv } from "https://esm.town/v/pomdtr/kv?v=3";
import { sqlite } from "https://esm.town/v/std/sqlite?v=6";
import { DenoSyntheticKV } from "https://esm.town/v/stevekrouse/DenoSyntheticKV?v=10";
import { createStore, sqliteStore } from "https://esm.town/v/vladimyr/keyvhqSqlite";
import Keyv from "npm:@keyvhq/core";
type TestObj = { test: number };
// setup openKv
const kv1 = openKv<TestObj>();
// setup Keyv
const { slug: namespace } = extractValInfo(import.meta.url);
// const store = sqliteStore; /* same as: const store = createStore({ table: "keyv" }); */
const store = createStore({ table: "keyv" });
const kv2 = new Keyv<TestObj>({ namespace, store });
// setup DenoSyntheticKV
const kv3 = new DenoSyntheticKV("keyv");
await kv1.set("foo", { test: 42 });
console.log("@std/sqlite:", (await sqlite.execute("select * from keyv")).rows);
console.log("openKv:", await kv1.get("foo"));
console.log("Keyv:", await kv2.get("foo"));
// doesn't work because DenoSyntheticKV serializes key to {"json":"vladimyr/kv_example:foo"}
console.log("DenoSyntheticKV:", await kv3.get(`${namespace}:foo`));
let data = await kv2.get("foo");
data.test += 1;
await kv2.set("foo", data);
console.log("\n@std/sqlite:", (await sqlite.execute("select * from keyv")).rows);
console.log("openKv:", await kv1.get("foo"));
console.log("Keyv:", await kv2.get("foo"));
// doesn't work because DenoSyntheticKV serializes key to {"json":"vladimyr/kv_example:foo"}
console.log("DenoSyntheticKV:", await kv3.get(`${namespace}:foo`));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// SPDX-License-Identifier: 0BSD
import { type Store } from "npm:@keyvhq/core";
import { type Adapter, Low } from "npm:lowdb";
export type Keyv<T> = Map<string, T>;
export type KeyvLowOptions<TValue> = {
adapter: Adapter<Record<string, TValue>>;
};
class MapAdapter<TValue> implements Adapter<Keyv<TValue>> {
constructor(
private adapter: Adapter<Record<string, TValue>>,
) {}
read = async () => {
const obj = await this.adapter.read() ?? {};
const entries = Object.entries(obj);
return new Map(entries);
};
write = async (data: Keyv<TValue>) => {
const entries = data.entries();
const obj = Object.fromEntries(entries);
return this.adapter.write(obj);
};
}
export class KeyvLow<TValue> implements Store<TValue> {
private adapter: MapAdapter<TValue>;
private db: Low<Keyv<TValue>>;
constructor(options: KeyvLowOptions<TValue>) {
this.adapter = new MapAdapter(options.adapter);
this.db = new Low(this.adapter, new Map());
}
clear = async (namespace = "") => {
await this.db.read();
const keysToDelete = [];
for (const key of this.db.data.keys()) {
if (key.startsWith(namespace)) {
keysToDelete.push(key);
}
}
keysToDelete.forEach(key => this.db.data.delete(key));
return this.db.write();
};
delete = async (key: string) => {
await this.db.read();
const result = this.db.data.delete(key);
await this.db.write();
return result;
};
has = async (key: string) => {
await this.db.read();
return this.db.data.has(key);
};
get = async (key: string) => {
await this.db.read();
return this.db.data.get(key);
};
set = async (key: string, value: TValue) => {
await this.db.read();
this.db.data.set(key, value);
return this.db.write();
};
async *iterator(namespace = "") {
await this.db.read();
for (const key of this.db.data.keys()) {
if (key.startsWith(namespace)) {
const value = this.db.data.get(key);
yield [key, value];
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { blob } from "https://esm.town/v/std/blob?v=11";
import { blobStore } from "https://esm.town/v/vladimyr/keyvhqBlob";
import Keyv from "npm:@keyvhq/core";
const keyv = new Keyv({ store: blobStore, namespace: "main" });
const keyv2 = new Keyv({ store: blobStore, namespace: "other" });
await keyv.set("foo", "bar");
console.log(await keyv.get("foo"));
await keyv.set("baz", { num: 42, str: "hello" });
console.log(await keyv.get("baz"));
await keyv.set("temp", "test");
await keyv.delete("temp");
console.log();
for await (const [key, val] of keyv.iterator()) {
console.log("%s=%o", key, val);
}
await keyv2.set("keyv", "is cool");
console.log();
const result = await blob.getJSON("keyv");
console.log(result);
console.log();
{
await keyv.clear();
const result = await blob.getJSON("keyv");
console.log(result);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import { extractValInfo } from "https://esm.town/v/pomdtr/extractValInfo?v=26";
import { blobStore } from "https://esm.town/v/vladimyr/keyvhqBlob";
import Keyv from "npm:@keyvhq/core";
const { slug } = extractValInfo(import.meta.url);
const keyv = new Keyv({ store: blobStore, namespace: slug });
await keyv.set("example", "hello keyv!");
console.log(await keyv.has("thisKeyDoesNotExist"));
console.log(await keyv.has("example"));
console.log(await keyv.get("example"));
{
const { blob } = await import("https://esm.town/v/std/blob?v=11");
const result = await blob.getJSON("keyv");
console.log(result);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// SPDX-License-Identifier: 0BSD
import { Blob } from "https://esm.town/v/pomdtr/lowdb?v=17";
import { KeyvLow } from "https://esm.town/v/vladimyr/keyvhqLow";
import { type Store } from "npm:@keyvhq/core";
export type KeyvBlobOptions = {
key?: string;
};
export const blobStore = createStore();
export function createStore<T>(options?: KeyvBlobOptions): Store<T> {
const key = options?.key ?? "keyv";
return new KeyvLow<T>({
adapter: new Blob(key),
});
}

Usage

Create valimport { openKv } from "https://esm.town/v/pomdtr/kv" const kv = openKv() await kv.set("test", true) console.log(await kv.get("test"))
1
2
3
4
5
6
7
8
import { callerRef } from "https://esm.town/v/pomdtr/refs";
import { sqliteStore } from "https://esm.town/v/vladimyr/keyvhqSqlite";
import Keyv from "npm:@keyvhq/core";
export function openKv<T = any>() {
const { slug } = callerRef();
return new Keyv<T>({ store: sqliteStore, namespace: slug });
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import { extractValInfo } from "https://esm.town/v/pomdtr/extractValInfo?v=26";
import { sqliteStore } from "https://esm.town/v/vladimyr/keyvhqSqlite";
import Keyv from "npm:@keyvhq/core";
const { slug } = extractValInfo(import.meta.url);
const keyv = new Keyv({ store: sqliteStore, namespace: slug });
await keyv.set("example", "hello keyv!");
console.log(await keyv.has("thisKeyDoesNotExist"));
console.log(await keyv.has("example"));
console.log(await keyv.get("example"));
{
const { sqlite } = await import("https://esm.town/v/std/sqlite?v=5");
const { zip } = await import("https://esm.town/v/pomdtr/sql");
const result = await sqlite.execute("select * from keyv");
console.table(zip(result));
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import { extractValInfo } from "https://esm.town/v/pomdtr/extractValInfo?v=26";
import { sqliteStore } from "https://esm.town/v/vladimyr/keyvhqSqlite";
import Keyv from "npm:@keyvhq/core";
const { slug } = extractValInfo(import.meta.url);
const keyv = new Keyv({ store: sqliteStore, namespace: slug });
await keyv.set("example", "hello keyv!");
console.log(await keyv.has("thisKeyDoesNotExist"));
console.log(await keyv.has("example"));
console.log(await keyv.get("example"));
{
const { sqlite } = await import("https://esm.town/v/std/sqlite?v=5");
const { zip } = await import("https://esm.town/v/pomdtr/sql");
const result = await sqlite.execute("select * from keyv");
console.table(zip(result));
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { sqlite } from "https://esm.town/v/std/sqlite?v=5";
import { sqliteStore } from "https://esm.town/v/vladimyr/keyvhqSqlite";
import Keyv from "npm:@keyvhq/core";
const keyv = new Keyv({ store: sqliteStore, namespace: "main" });
const keyv2 = new Keyv({ store: sqliteStore, namespace: "other" });
await keyv.set("foo", "bar");
console.log(await keyv.get("foo"));
await keyv.set("baz", { num: 42, str: "hello" });
console.log(await keyv.get("baz"));
await keyv.set("temp", "test");
await keyv.delete("temp");
console.log();
for await (const [key, val] of keyv.iterator()) {
console.log("%s=%o", key, val);
}
await keyv2.set("keyv", "is cool");
console.log();
const result = await sqlite.execute("select * from keyv");
console.log(result.rows);
console.log();
{
await keyv.clear();
const result = await sqlite.execute("select * from keyv");
console.log(result.rows);
}
1
Next