@marianmeres/kv 1.0.4 → 1.1.2
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/README.md +2 -3
- package/dist/adapter/deno-kv.d.ts +38 -1
- package/dist/adapter/deno-kv.js +177 -2
- package/dist/adapter/memory.js +1 -1
- package/dist/adapter/postgres.js +1 -1
- package/dist/kv.d.ts +5 -0
- package/dist/kv.js +7 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -4,11 +4,10 @@ Key-value storage abstraction layer with support of multiple backend adapters:
|
|
|
4
4
|
- redis
|
|
5
5
|
- postgresql
|
|
6
6
|
- memory
|
|
7
|
+
- Deno KV (only in Deno runtime)
|
|
7
8
|
|
|
8
9
|
The API is inspired by the Redis API.
|
|
9
10
|
|
|
10
|
-
TODO: Deno KV adapter
|
|
11
|
-
|
|
12
11
|
## Installation
|
|
13
12
|
|
|
14
13
|
```shell
|
|
@@ -26,7 +25,7 @@ import { createKVClient } from '@marianmeres/kv';
|
|
|
26
25
|
|
|
27
26
|
const client = createKVClient(
|
|
28
27
|
"my-app-namespace",
|
|
29
|
-
type: 'redis' | 'postgres' | 'memory' = 'memory',
|
|
28
|
+
type: 'redis' | 'postgres' | 'deno-kv' | 'memory' = 'memory',
|
|
30
29
|
options, // options... redisClient (for redis) or pg.Pool/pg.Client (for postgres)...
|
|
31
30
|
);
|
|
32
31
|
|
|
@@ -1 +1,38 @@
|
|
|
1
|
-
|
|
1
|
+
import { AdapterAbstract, type Operation, type SetOptions, type AdapterAbstractOptions } from "./abstract.js";
|
|
2
|
+
export interface AdapterDenoKvOptions extends AdapterAbstractOptions {
|
|
3
|
+
db: any;
|
|
4
|
+
}
|
|
5
|
+
export declare class AdapterDenoKv extends AdapterAbstract {
|
|
6
|
+
#private;
|
|
7
|
+
readonly namespace: string;
|
|
8
|
+
_type: string;
|
|
9
|
+
readonly options: AdapterDenoKvOptions;
|
|
10
|
+
constructor(namespace?: string, options?: Partial<AdapterDenoKvOptions>);
|
|
11
|
+
/** Will initialize the client instance */
|
|
12
|
+
initialize(): Promise<void>;
|
|
13
|
+
/** Will destroy (do cleanups) on the instance */
|
|
14
|
+
destroy(_hard?: boolean): Promise<void>;
|
|
15
|
+
/** Will set key-value pair to the underlying store with given options */
|
|
16
|
+
set(key: string, value: any, options?: Partial<SetOptions>): Promise<boolean>;
|
|
17
|
+
/** Will get the key from the underlying store */
|
|
18
|
+
get(key: string): Promise<any>;
|
|
19
|
+
/** Will check if the key exists in the underlying store */
|
|
20
|
+
exists(key: string): Promise<boolean>;
|
|
21
|
+
/** Will delete the key from the underlying store */
|
|
22
|
+
delete(key: string): Promise<boolean>;
|
|
23
|
+
/** Will list all existing keys in the underlying store matching given pattern.
|
|
24
|
+
* Recognizes redis-like star wildcard format. */
|
|
25
|
+
keys(pattern: string): Promise<string[]>;
|
|
26
|
+
/** Will clear all existing keys in the underlying store matching given pattern */
|
|
27
|
+
clear(pattern: string): Promise<number>;
|
|
28
|
+
/** Will set multiple kv pairs in one batch */
|
|
29
|
+
setMultiple(keyValuePairs: [string, any][], options?: Partial<SetOptions>): Promise<boolean[]>;
|
|
30
|
+
/** Will get multiple keys in one batch. */
|
|
31
|
+
getMultiple(keys: string[]): Promise<Record<string, any>>;
|
|
32
|
+
/** */
|
|
33
|
+
transaction(operations: Operation[]): Promise<any[]>;
|
|
34
|
+
/** Not supported in Deno.Kv */
|
|
35
|
+
expire(key: string, ttl: number): Promise<boolean>;
|
|
36
|
+
/** NOT SUPPORTED IN DENO KV... Will get the expiration Date for given key */
|
|
37
|
+
ttl(key: string): Promise<Date | null | false>;
|
|
38
|
+
}
|
package/dist/adapter/deno-kv.js
CHANGED
|
@@ -1,2 +1,177 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { createLogger } from "@marianmeres/clog";
|
|
2
|
+
import { AdapterAbstract, } from "./abstract.js";
|
|
3
|
+
export class AdapterDenoKv extends AdapterAbstract {
|
|
4
|
+
namespace;
|
|
5
|
+
_type = "deno-kv";
|
|
6
|
+
options = {
|
|
7
|
+
defaultTtl: 0, // no ttl by default
|
|
8
|
+
db: null,
|
|
9
|
+
logger: createLogger("KV/deno-kv"),
|
|
10
|
+
};
|
|
11
|
+
constructor(namespace = "", options = {}) {
|
|
12
|
+
super();
|
|
13
|
+
this.namespace = namespace;
|
|
14
|
+
this._assertValidNamespace();
|
|
15
|
+
this.options = Object.freeze({ ...this.options, ...(options || {}) });
|
|
16
|
+
if (!this.options.db) {
|
|
17
|
+
throw new Error("Missing Deno.Kv instance");
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/** Will initialize the client instance */
|
|
21
|
+
initialize() {
|
|
22
|
+
this._initialized = true;
|
|
23
|
+
return Promise.resolve();
|
|
24
|
+
}
|
|
25
|
+
/** Will destroy (do cleanups) on the instance */
|
|
26
|
+
destroy(_hard) {
|
|
27
|
+
this._initialized = false;
|
|
28
|
+
return Promise.resolve();
|
|
29
|
+
}
|
|
30
|
+
#denoKvKey(key, full = true) {
|
|
31
|
+
const parts = key.split(":");
|
|
32
|
+
const prefix = parts[0];
|
|
33
|
+
const rest = parts.slice(1).join(":");
|
|
34
|
+
let out = [prefix, rest].filter(Boolean);
|
|
35
|
+
if (full)
|
|
36
|
+
out = [this.namespace, ...out];
|
|
37
|
+
return out;
|
|
38
|
+
}
|
|
39
|
+
/** Will set key-value pair to the underlying store with given options */
|
|
40
|
+
async set(key, value, options = {}) {
|
|
41
|
+
this._assertInitialized();
|
|
42
|
+
const { db } = this.options;
|
|
43
|
+
const ttl = options.ttl || this.options.defaultTtl || undefined;
|
|
44
|
+
if (value === undefined)
|
|
45
|
+
value = null;
|
|
46
|
+
// Deno.Kv does not require this, but for the consistency with other adapters
|
|
47
|
+
// let's just stringify (the toJSON may do magic, so we want consistent behavior
|
|
48
|
+
// across adapters)
|
|
49
|
+
value = JSON.stringify(value);
|
|
50
|
+
await db.set(this.#denoKvKey(key), value, {
|
|
51
|
+
expireIn: ttl ? ttl * 1000 : undefined,
|
|
52
|
+
});
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
/** Will return the internal value */
|
|
56
|
+
#parseValue(row) {
|
|
57
|
+
try {
|
|
58
|
+
return JSON.parse(`${row?.value ?? null}`);
|
|
59
|
+
}
|
|
60
|
+
catch (_err) {
|
|
61
|
+
return row?.value;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/** Will get the key from the underlying store */
|
|
65
|
+
async get(key) {
|
|
66
|
+
this._assertInitialized();
|
|
67
|
+
const { db } = this.options;
|
|
68
|
+
const row = await db.get(this.#denoKvKey(key));
|
|
69
|
+
return this.#parseValue(row);
|
|
70
|
+
}
|
|
71
|
+
/** Will check if the key exists in the underlying store */
|
|
72
|
+
async exists(key) {
|
|
73
|
+
this._assertInitialized();
|
|
74
|
+
return null !== (await this.get(key));
|
|
75
|
+
}
|
|
76
|
+
/** Will delete the key from the underlying store */
|
|
77
|
+
async delete(key) {
|
|
78
|
+
this._assertInitialized();
|
|
79
|
+
const { db } = this.options;
|
|
80
|
+
await db.delete(this.#denoKvKey(key));
|
|
81
|
+
return Promise.resolve(true); // always true
|
|
82
|
+
}
|
|
83
|
+
/** Will list all existing keys in the underlying store matching given pattern.
|
|
84
|
+
* Recognizes redis-like star wildcard format. */
|
|
85
|
+
async keys(pattern) {
|
|
86
|
+
this._assertInitialized();
|
|
87
|
+
const { db } = this.options;
|
|
88
|
+
const [prefix, search] = this.#denoKvKey(pattern, false);
|
|
89
|
+
// convert Redis-style pattern to regex
|
|
90
|
+
const regexPattern = pattern.replace(/\*/g, ".*").replace(/\?/g, ".");
|
|
91
|
+
const regex = new RegExp(`^${regexPattern}$`);
|
|
92
|
+
const iter = db.list({
|
|
93
|
+
prefix: [this.namespace, prefix === "*" ? undefined : prefix].filter(Boolean),
|
|
94
|
+
});
|
|
95
|
+
const out = [];
|
|
96
|
+
for await (const res of iter) {
|
|
97
|
+
// note: here we have full data available, not just keys, so this is pure waste
|
|
98
|
+
const key = res.key.slice(1).join(":");
|
|
99
|
+
if (prefix === "*" || search === "*" || regex.test(key)) {
|
|
100
|
+
out.push(key);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return out;
|
|
104
|
+
}
|
|
105
|
+
/** Will clear all existing keys in the underlying store matching given pattern */
|
|
106
|
+
async clear(pattern) {
|
|
107
|
+
this._assertInitialized();
|
|
108
|
+
const keysToDelete = await this.keys(pattern);
|
|
109
|
+
let deleteCount = 0;
|
|
110
|
+
for (const key of keysToDelete) {
|
|
111
|
+
await this.delete(key);
|
|
112
|
+
deleteCount++;
|
|
113
|
+
}
|
|
114
|
+
return deleteCount;
|
|
115
|
+
}
|
|
116
|
+
/** Will set multiple kv pairs in one batch */
|
|
117
|
+
async setMultiple(keyValuePairs, options = {}) {
|
|
118
|
+
this._assertInitialized();
|
|
119
|
+
const results = [];
|
|
120
|
+
for (const [key, value] of keyValuePairs) {
|
|
121
|
+
await this.set(key, value, options);
|
|
122
|
+
results.push(true);
|
|
123
|
+
}
|
|
124
|
+
return results;
|
|
125
|
+
}
|
|
126
|
+
/** Will get multiple keys in one batch. */
|
|
127
|
+
async getMultiple(keys) {
|
|
128
|
+
this._assertInitialized();
|
|
129
|
+
const { db } = this.options;
|
|
130
|
+
const result = {};
|
|
131
|
+
const denoKvKeys = keys.map((k) => this.#denoKvKey(k));
|
|
132
|
+
const results = await db.getMany(denoKvKeys);
|
|
133
|
+
for (const [index, row] of results.entries()) {
|
|
134
|
+
result[keys[index]] = this.#parseValue(row);
|
|
135
|
+
}
|
|
136
|
+
return result;
|
|
137
|
+
}
|
|
138
|
+
/** */
|
|
139
|
+
async transaction(operations) {
|
|
140
|
+
this._assertInitialized();
|
|
141
|
+
const { db } = this.options;
|
|
142
|
+
this._assertInitialized();
|
|
143
|
+
// note: memory operations are atomic by nature
|
|
144
|
+
const results = [];
|
|
145
|
+
try {
|
|
146
|
+
for (const op of operations) {
|
|
147
|
+
switch (op.type) {
|
|
148
|
+
case "set":
|
|
149
|
+
results.push(await this.set(op.key, op.value, op.options || {}));
|
|
150
|
+
break;
|
|
151
|
+
case "get":
|
|
152
|
+
results.push(await this.get(op.key));
|
|
153
|
+
break;
|
|
154
|
+
case "delete":
|
|
155
|
+
results.push(await this.delete(op.key));
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
catch (err) {
|
|
161
|
+
// rollback here in a real db-based adapter
|
|
162
|
+
throw err;
|
|
163
|
+
}
|
|
164
|
+
return results;
|
|
165
|
+
}
|
|
166
|
+
/** Not supported in Deno.Kv */
|
|
167
|
+
expire(key, ttl) {
|
|
168
|
+
this._assertInitialized();
|
|
169
|
+
return Promise.resolve(false);
|
|
170
|
+
}
|
|
171
|
+
/** NOT SUPPORTED IN DENO KV... Will get the expiration Date for given key */
|
|
172
|
+
async ttl(key) {
|
|
173
|
+
this._assertInitialized();
|
|
174
|
+
//
|
|
175
|
+
return Promise.resolve(null);
|
|
176
|
+
}
|
|
177
|
+
}
|
package/dist/adapter/memory.js
CHANGED
|
@@ -14,7 +14,7 @@ export class AdapterMemory extends AdapterAbstract {
|
|
|
14
14
|
constructor(namespace = "", options = {}) {
|
|
15
15
|
super();
|
|
16
16
|
this.namespace = namespace;
|
|
17
|
-
this.options = { ...this.options, ...(options || {}) };
|
|
17
|
+
this.options = Object.freeze({ ...this.options, ...(options || {}) });
|
|
18
18
|
this._assertValidNamespace();
|
|
19
19
|
}
|
|
20
20
|
/** Will initialize the client instance */
|
package/dist/adapter/postgres.js
CHANGED
|
@@ -15,7 +15,7 @@ export class AdapterPostgres extends AdapterAbstract {
|
|
|
15
15
|
super();
|
|
16
16
|
this.namespace = namespace;
|
|
17
17
|
this._assertValidNamespace();
|
|
18
|
-
this.options = { ...this.options, ...(options || {}) };
|
|
18
|
+
this.options = Object.freeze({ ...this.options, ...(options || {}) });
|
|
19
19
|
if (!this.options.db) {
|
|
20
20
|
throw new Error("Missing pg instance");
|
|
21
21
|
}
|
package/dist/kv.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { AdapterDenoKv, type AdapterDenoKvOptions } from "./adapter/deno-kv.js";
|
|
1
2
|
import { AdapterMemory, type AdapterMemoryOptions } from "./adapter/memory.js";
|
|
2
3
|
import { AdapterPostgres, type AdapterPostgresOptions } from "./adapter/postgres.js";
|
|
3
4
|
import { AdapterRedis, type AdapterRedisOptions } from "./adapter/redis.js";
|
|
@@ -14,6 +15,10 @@ interface KnownTypes {
|
|
|
14
15
|
options: AdapterRedisOptions;
|
|
15
16
|
adapter: AdapterRedis;
|
|
16
17
|
};
|
|
18
|
+
"deno-kv": {
|
|
19
|
+
options: AdapterDenoKvOptions;
|
|
20
|
+
adapter: AdapterDenoKv;
|
|
21
|
+
};
|
|
17
22
|
}
|
|
18
23
|
/** */
|
|
19
24
|
export declare function createKVClient<T extends keyof KnownTypes>(namespace?: string, type?: T, options?: Partial<KnownTypes[T]["options"]>): KnownTypes[T]["adapter"];
|
package/dist/kv.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { AdapterDenoKv } from "./adapter/deno-kv.js";
|
|
1
2
|
import { AdapterMemory } from "./adapter/memory.js";
|
|
2
3
|
import { AdapterPostgres, } from "./adapter/postgres.js";
|
|
3
4
|
import { AdapterRedis } from "./adapter/redis.js";
|
|
@@ -10,6 +11,12 @@ export function createKVClient(namespace = "", type = "redis", options = {}) {
|
|
|
10
11
|
return new AdapterPostgres(namespace, options);
|
|
11
12
|
if (type === "redis")
|
|
12
13
|
return new AdapterRedis(namespace, options);
|
|
14
|
+
if (type === "deno-kv") {
|
|
15
|
+
if (typeof globalThis.Deno === "undefined") {
|
|
16
|
+
throw new Error(`Type "${type}" is only supported under Deno runtime`);
|
|
17
|
+
}
|
|
18
|
+
return new AdapterDenoKv(namespace, options);
|
|
19
|
+
}
|
|
13
20
|
//
|
|
14
21
|
throw new TypeError(`Unsupported KV client type "${options.type}"`);
|
|
15
22
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@marianmeres/kv",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/mod.js",
|
|
6
6
|
"types": "dist/mod.d.ts",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"dependencies": {
|
|
17
17
|
"@marianmeres/clog": "^2.3.3",
|
|
18
18
|
"pg": "^8.16.3",
|
|
19
|
-
"redis": "^5.
|
|
19
|
+
"redis": "^5.9.0"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
22
|
"@types/node": "latest",
|