@marianmeres/kv 1.1.2 → 1.2.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/README.md +34 -8
- package/dist/adapter/abstract.d.ts +256 -29
- package/dist/adapter/abstract.js +219 -24
- package/dist/adapter/deno-kv.d.ts +48 -14
- package/dist/adapter/deno-kv.js +46 -21
- package/dist/adapter/memory.d.ts +43 -15
- package/dist/adapter/memory.js +38 -17
- package/dist/adapter/postgres.d.ts +53 -15
- package/dist/adapter/postgres.js +44 -16
- package/dist/adapter/redis.d.ts +49 -12
- package/dist/adapter/redis.js +40 -14
- package/dist/kv.d.ts +52 -1
- package/dist/kv.js +50 -3
- package/package.json +3 -3
|
@@ -1,38 +1,72 @@
|
|
|
1
1
|
import { AdapterAbstract, type Operation, type SetOptions, type AdapterAbstractOptions } from "./abstract.js";
|
|
2
|
+
/**
|
|
3
|
+
* Configuration options for the Deno KV adapter.
|
|
4
|
+
*/
|
|
2
5
|
export interface AdapterDenoKvOptions extends AdapterAbstractOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Deno.Kv instance obtained via `Deno.openKv()`.
|
|
8
|
+
* @see https://docs.deno.com/deploy/kv/#testing
|
|
9
|
+
*/
|
|
3
10
|
db: any;
|
|
4
11
|
}
|
|
12
|
+
/**
|
|
13
|
+
* Deno KV key-value storage adapter.
|
|
14
|
+
*
|
|
15
|
+
* Provides persistent key-value storage using Deno's built-in KV store.
|
|
16
|
+
* Only available when running in the Deno runtime.
|
|
17
|
+
*
|
|
18
|
+
* @remarks
|
|
19
|
+
* - Only works in Deno runtime (will throw error in Node.js)
|
|
20
|
+
* - `delete()` always returns `true` (Deno.Kv limitation)
|
|
21
|
+
* - `expire()` is not supported - always returns `false`
|
|
22
|
+
* - `ttl()` is not supported - always returns `null`
|
|
23
|
+
* - TTL can be set during `set()` but cannot be queried afterward
|
|
24
|
+
* - Uses `getMany()` for efficient batch reads
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* const kv = await Deno.openKv();
|
|
29
|
+
* const client = createKVClient("myapp:", "deno-kv", { db: kv });
|
|
30
|
+
* await client.initialize();
|
|
31
|
+
* await client.set("user:123", { name: "John" }, { ttl: 3600 });
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
5
34
|
export declare class AdapterDenoKv extends AdapterAbstract {
|
|
6
35
|
#private;
|
|
7
36
|
readonly namespace: string;
|
|
8
37
|
_type: string;
|
|
9
38
|
readonly options: AdapterDenoKvOptions;
|
|
10
39
|
constructor(namespace?: string, options?: Partial<AdapterDenoKvOptions>);
|
|
11
|
-
/**
|
|
40
|
+
/** @inheritdoc */
|
|
12
41
|
initialize(): Promise<void>;
|
|
13
|
-
/**
|
|
42
|
+
/** @inheritdoc */
|
|
14
43
|
destroy(_hard?: boolean): Promise<void>;
|
|
15
|
-
/**
|
|
44
|
+
/** @inheritdoc */
|
|
16
45
|
set(key: string, value: any, options?: Partial<SetOptions>): Promise<boolean>;
|
|
17
|
-
/**
|
|
46
|
+
/** @inheritdoc */
|
|
18
47
|
get(key: string): Promise<any>;
|
|
19
|
-
/**
|
|
48
|
+
/** @inheritdoc */
|
|
20
49
|
exists(key: string): Promise<boolean>;
|
|
21
|
-
/**
|
|
50
|
+
/** @inheritdoc */
|
|
22
51
|
delete(key: string): Promise<boolean>;
|
|
23
|
-
/**
|
|
24
|
-
* Recognizes redis-like star wildcard format. */
|
|
52
|
+
/** @inheritdoc */
|
|
25
53
|
keys(pattern: string): Promise<string[]>;
|
|
26
|
-
/**
|
|
54
|
+
/** @inheritdoc */
|
|
27
55
|
clear(pattern: string): Promise<number>;
|
|
28
|
-
/**
|
|
56
|
+
/** @inheritdoc */
|
|
29
57
|
setMultiple(keyValuePairs: [string, any][], options?: Partial<SetOptions>): Promise<boolean[]>;
|
|
30
|
-
/**
|
|
58
|
+
/** @inheritdoc */
|
|
31
59
|
getMultiple(keys: string[]): Promise<Record<string, any>>;
|
|
32
|
-
/**
|
|
60
|
+
/** @inheritdoc */
|
|
33
61
|
transaction(operations: Operation[]): Promise<any[]>;
|
|
34
|
-
/**
|
|
62
|
+
/**
|
|
63
|
+
* Not supported in Deno.Kv - always returns `false`.
|
|
64
|
+
* @inheritdoc
|
|
65
|
+
*/
|
|
35
66
|
expire(key: string, ttl: number): Promise<boolean>;
|
|
36
|
-
/**
|
|
67
|
+
/**
|
|
68
|
+
* Not supported in Deno.Kv - always returns `null`.
|
|
69
|
+
* @inheritdoc
|
|
70
|
+
*/
|
|
37
71
|
ttl(key: string): Promise<Date | null | false>;
|
|
38
72
|
}
|
package/dist/adapter/deno-kv.js
CHANGED
|
@@ -1,12 +1,34 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createClog } from "@marianmeres/clog";
|
|
2
2
|
import { AdapterAbstract, } from "./abstract.js";
|
|
3
|
+
/**
|
|
4
|
+
* Deno KV key-value storage adapter.
|
|
5
|
+
*
|
|
6
|
+
* Provides persistent key-value storage using Deno's built-in KV store.
|
|
7
|
+
* Only available when running in the Deno runtime.
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* - Only works in Deno runtime (will throw error in Node.js)
|
|
11
|
+
* - `delete()` always returns `true` (Deno.Kv limitation)
|
|
12
|
+
* - `expire()` is not supported - always returns `false`
|
|
13
|
+
* - `ttl()` is not supported - always returns `null`
|
|
14
|
+
* - TTL can be set during `set()` but cannot be queried afterward
|
|
15
|
+
* - Uses `getMany()` for efficient batch reads
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* const kv = await Deno.openKv();
|
|
20
|
+
* const client = createKVClient("myapp:", "deno-kv", { db: kv });
|
|
21
|
+
* await client.initialize();
|
|
22
|
+
* await client.set("user:123", { name: "John" }, { ttl: 3600 });
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
3
25
|
export class AdapterDenoKv extends AdapterAbstract {
|
|
4
26
|
namespace;
|
|
5
27
|
_type = "deno-kv";
|
|
6
28
|
options = {
|
|
7
29
|
defaultTtl: 0, // no ttl by default
|
|
8
30
|
db: null,
|
|
9
|
-
logger:
|
|
31
|
+
logger: createClog("KV/deno-kv"),
|
|
10
32
|
};
|
|
11
33
|
constructor(namespace = "", options = {}) {
|
|
12
34
|
super();
|
|
@@ -17,12 +39,12 @@ export class AdapterDenoKv extends AdapterAbstract {
|
|
|
17
39
|
throw new Error("Missing Deno.Kv instance");
|
|
18
40
|
}
|
|
19
41
|
}
|
|
20
|
-
/**
|
|
42
|
+
/** @inheritdoc */
|
|
21
43
|
initialize() {
|
|
22
44
|
this._initialized = true;
|
|
23
45
|
return Promise.resolve();
|
|
24
46
|
}
|
|
25
|
-
/**
|
|
47
|
+
/** @inheritdoc */
|
|
26
48
|
destroy(_hard) {
|
|
27
49
|
this._initialized = false;
|
|
28
50
|
return Promise.resolve();
|
|
@@ -36,7 +58,7 @@ export class AdapterDenoKv extends AdapterAbstract {
|
|
|
36
58
|
out = [this.namespace, ...out];
|
|
37
59
|
return out;
|
|
38
60
|
}
|
|
39
|
-
/**
|
|
61
|
+
/** @inheritdoc */
|
|
40
62
|
async set(key, value, options = {}) {
|
|
41
63
|
this._assertInitialized();
|
|
42
64
|
const { db } = this.options;
|
|
@@ -61,27 +83,26 @@ export class AdapterDenoKv extends AdapterAbstract {
|
|
|
61
83
|
return row?.value;
|
|
62
84
|
}
|
|
63
85
|
}
|
|
64
|
-
/**
|
|
86
|
+
/** @inheritdoc */
|
|
65
87
|
async get(key) {
|
|
66
88
|
this._assertInitialized();
|
|
67
89
|
const { db } = this.options;
|
|
68
90
|
const row = await db.get(this.#denoKvKey(key));
|
|
69
91
|
return this.#parseValue(row);
|
|
70
92
|
}
|
|
71
|
-
/**
|
|
93
|
+
/** @inheritdoc */
|
|
72
94
|
async exists(key) {
|
|
73
95
|
this._assertInitialized();
|
|
74
96
|
return null !== (await this.get(key));
|
|
75
97
|
}
|
|
76
|
-
/**
|
|
98
|
+
/** @inheritdoc */
|
|
77
99
|
async delete(key) {
|
|
78
100
|
this._assertInitialized();
|
|
79
101
|
const { db } = this.options;
|
|
80
102
|
await db.delete(this.#denoKvKey(key));
|
|
81
103
|
return Promise.resolve(true); // always true
|
|
82
104
|
}
|
|
83
|
-
/**
|
|
84
|
-
* Recognizes redis-like star wildcard format. */
|
|
105
|
+
/** @inheritdoc */
|
|
85
106
|
async keys(pattern) {
|
|
86
107
|
this._assertInitialized();
|
|
87
108
|
const { db } = this.options;
|
|
@@ -102,7 +123,7 @@ export class AdapterDenoKv extends AdapterAbstract {
|
|
|
102
123
|
}
|
|
103
124
|
return out;
|
|
104
125
|
}
|
|
105
|
-
/**
|
|
126
|
+
/** @inheritdoc */
|
|
106
127
|
async clear(pattern) {
|
|
107
128
|
this._assertInitialized();
|
|
108
129
|
const keysToDelete = await this.keys(pattern);
|
|
@@ -113,7 +134,7 @@ export class AdapterDenoKv extends AdapterAbstract {
|
|
|
113
134
|
}
|
|
114
135
|
return deleteCount;
|
|
115
136
|
}
|
|
116
|
-
/**
|
|
137
|
+
/** @inheritdoc */
|
|
117
138
|
async setMultiple(keyValuePairs, options = {}) {
|
|
118
139
|
this._assertInitialized();
|
|
119
140
|
const results = [];
|
|
@@ -123,7 +144,7 @@ export class AdapterDenoKv extends AdapterAbstract {
|
|
|
123
144
|
}
|
|
124
145
|
return results;
|
|
125
146
|
}
|
|
126
|
-
/**
|
|
147
|
+
/** @inheritdoc */
|
|
127
148
|
async getMultiple(keys) {
|
|
128
149
|
this._assertInitialized();
|
|
129
150
|
const { db } = this.options;
|
|
@@ -135,12 +156,11 @@ export class AdapterDenoKv extends AdapterAbstract {
|
|
|
135
156
|
}
|
|
136
157
|
return result;
|
|
137
158
|
}
|
|
138
|
-
/**
|
|
159
|
+
/** @inheritdoc */
|
|
139
160
|
async transaction(operations) {
|
|
140
161
|
this._assertInitialized();
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
// note: memory operations are atomic by nature
|
|
162
|
+
// Note: This implementation does not use Deno.Kv's atomic feature
|
|
163
|
+
// Operations are executed sequentially
|
|
144
164
|
const results = [];
|
|
145
165
|
try {
|
|
146
166
|
for (const op of operations) {
|
|
@@ -163,15 +183,20 @@ export class AdapterDenoKv extends AdapterAbstract {
|
|
|
163
183
|
}
|
|
164
184
|
return results;
|
|
165
185
|
}
|
|
166
|
-
/**
|
|
186
|
+
/**
|
|
187
|
+
* Not supported in Deno.Kv - always returns `false`.
|
|
188
|
+
* @inheritdoc
|
|
189
|
+
*/
|
|
167
190
|
expire(key, ttl) {
|
|
168
191
|
this._assertInitialized();
|
|
169
192
|
return Promise.resolve(false);
|
|
170
193
|
}
|
|
171
|
-
/**
|
|
172
|
-
|
|
194
|
+
/**
|
|
195
|
+
* Not supported in Deno.Kv - always returns `null`.
|
|
196
|
+
* @inheritdoc
|
|
197
|
+
*/
|
|
198
|
+
ttl(key) {
|
|
173
199
|
this._assertInitialized();
|
|
174
|
-
//
|
|
175
200
|
return Promise.resolve(null);
|
|
176
201
|
}
|
|
177
202
|
}
|
package/dist/adapter/memory.d.ts
CHANGED
|
@@ -1,40 +1,68 @@
|
|
|
1
1
|
import { AdapterAbstract, type AdapterAbstractOptions, type Operation, type SetOptions } from "./abstract.js";
|
|
2
|
+
/**
|
|
3
|
+
* Configuration options for the in-memory KV adapter.
|
|
4
|
+
*/
|
|
2
5
|
export interface AdapterMemoryOptions extends AdapterAbstractOptions {
|
|
3
|
-
/**
|
|
6
|
+
/**
|
|
7
|
+
* Interval in seconds for automatic cleanup of expired keys.
|
|
8
|
+
* Set to 0 to disable automatic cleanup (expired keys will still be
|
|
9
|
+
* lazily removed on access).
|
|
10
|
+
*/
|
|
4
11
|
ttlCleanupIntervalSec: number;
|
|
5
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
* In-memory key-value storage adapter.
|
|
15
|
+
*
|
|
16
|
+
* Stores all data in memory using JavaScript Maps. Data is not persisted
|
|
17
|
+
* and will be lost when the process exits.
|
|
18
|
+
*
|
|
19
|
+
* @remarks
|
|
20
|
+
* - Fast and simple, ideal for testing and caching
|
|
21
|
+
* - Supports TTL with optional automatic cleanup
|
|
22
|
+
* - Thread-safe within a single Node.js/Deno process
|
|
23
|
+
* - Not suitable for multi-process or distributed environments
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* const client = createKVClient("myapp:", "memory", {
|
|
28
|
+
* defaultTtl: 3600,
|
|
29
|
+
* ttlCleanupIntervalSec: 60, // Clean expired keys every minute
|
|
30
|
+
* });
|
|
31
|
+
* await client.initialize();
|
|
32
|
+
* await client.set("key", "value");
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
6
35
|
export declare class AdapterMemory extends AdapterAbstract {
|
|
7
36
|
#private;
|
|
8
37
|
readonly namespace: string;
|
|
9
38
|
_type: string;
|
|
10
39
|
readonly options: AdapterMemoryOptions;
|
|
11
40
|
constructor(namespace?: string, options?: Partial<AdapterMemoryOptions>);
|
|
12
|
-
/**
|
|
41
|
+
/** @inheritdoc */
|
|
13
42
|
initialize(): Promise<void>;
|
|
14
|
-
/**
|
|
43
|
+
/** @inheritdoc */
|
|
15
44
|
destroy(_hard?: boolean): Promise<void>;
|
|
16
|
-
/**
|
|
45
|
+
/** @inheritdoc */
|
|
17
46
|
set(key: string, value: any, options?: Partial<SetOptions>): Promise<boolean>;
|
|
18
|
-
/**
|
|
47
|
+
/** @inheritdoc */
|
|
19
48
|
get(key: string): Promise<any>;
|
|
20
|
-
/**
|
|
49
|
+
/** @inheritdoc */
|
|
21
50
|
delete(key: string): Promise<boolean>;
|
|
22
|
-
/**
|
|
51
|
+
/** @inheritdoc */
|
|
23
52
|
exists(key: string): Promise<boolean>;
|
|
24
|
-
/**
|
|
25
|
-
* Recognizes redis-like star wildcard format. */
|
|
53
|
+
/** @inheritdoc */
|
|
26
54
|
keys(pattern: string): Promise<string[]>;
|
|
27
|
-
/**
|
|
55
|
+
/** @inheritdoc */
|
|
28
56
|
clear(pattern: string): Promise<number>;
|
|
29
|
-
/**
|
|
57
|
+
/** @inheritdoc */
|
|
30
58
|
setMultiple(keyValuePairs: [string, any][], options?: Partial<SetOptions>): Promise<boolean[]>;
|
|
31
|
-
/**
|
|
59
|
+
/** @inheritdoc */
|
|
32
60
|
getMultiple(keys: string[]): Promise<Record<string, any>>;
|
|
33
|
-
/**
|
|
61
|
+
/** @inheritdoc */
|
|
34
62
|
expire(key: string, ttl: number): Promise<boolean>;
|
|
35
|
-
/**
|
|
63
|
+
/** @inheritdoc */
|
|
36
64
|
ttl(key: string): Promise<Date | null | false>;
|
|
37
|
-
/**
|
|
65
|
+
/** @inheritdoc */
|
|
38
66
|
transaction(operations: Operation[]): Promise<any[]>;
|
|
39
67
|
__debug_dump(): Promise<Record<string, {
|
|
40
68
|
value: any;
|
package/dist/adapter/memory.js
CHANGED
|
@@ -1,12 +1,34 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createClog } from "@marianmeres/clog";
|
|
2
2
|
import { AdapterAbstract, } from "./abstract.js";
|
|
3
|
+
/**
|
|
4
|
+
* In-memory key-value storage adapter.
|
|
5
|
+
*
|
|
6
|
+
* Stores all data in memory using JavaScript Maps. Data is not persisted
|
|
7
|
+
* and will be lost when the process exits.
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* - Fast and simple, ideal for testing and caching
|
|
11
|
+
* - Supports TTL with optional automatic cleanup
|
|
12
|
+
* - Thread-safe within a single Node.js/Deno process
|
|
13
|
+
* - Not suitable for multi-process or distributed environments
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const client = createKVClient("myapp:", "memory", {
|
|
18
|
+
* defaultTtl: 3600,
|
|
19
|
+
* ttlCleanupIntervalSec: 60, // Clean expired keys every minute
|
|
20
|
+
* });
|
|
21
|
+
* await client.initialize();
|
|
22
|
+
* await client.set("key", "value");
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
3
25
|
export class AdapterMemory extends AdapterAbstract {
|
|
4
26
|
namespace;
|
|
5
27
|
_type = "memory";
|
|
6
28
|
options = {
|
|
7
29
|
defaultTtl: 0, // no ttl by default
|
|
8
30
|
ttlCleanupIntervalSec: 0,
|
|
9
|
-
logger:
|
|
31
|
+
logger: createClog("KV/memory"),
|
|
10
32
|
};
|
|
11
33
|
#store = new Map();
|
|
12
34
|
#expirations = new Map();
|
|
@@ -17,13 +39,13 @@ export class AdapterMemory extends AdapterAbstract {
|
|
|
17
39
|
this.options = Object.freeze({ ...this.options, ...(options || {}) });
|
|
18
40
|
this._assertValidNamespace();
|
|
19
41
|
}
|
|
20
|
-
/**
|
|
42
|
+
/** @inheritdoc */
|
|
21
43
|
initialize() {
|
|
22
44
|
this._initialized = true;
|
|
23
45
|
this.#maybeTTLCleanup();
|
|
24
|
-
return Promise.resolve();
|
|
46
|
+
return Promise.resolve();
|
|
25
47
|
}
|
|
26
|
-
/**
|
|
48
|
+
/** @inheritdoc */
|
|
27
49
|
destroy(_hard) {
|
|
28
50
|
this._initialized = false;
|
|
29
51
|
clearTimeout(this.#cleanupTimer);
|
|
@@ -55,7 +77,7 @@ export class AdapterMemory extends AdapterAbstract {
|
|
|
55
77
|
}
|
|
56
78
|
return false;
|
|
57
79
|
}
|
|
58
|
-
/**
|
|
80
|
+
/** @inheritdoc */
|
|
59
81
|
set(key, value, options = {}) {
|
|
60
82
|
this._assertInitialized();
|
|
61
83
|
key = this._withNs(key);
|
|
@@ -70,7 +92,7 @@ export class AdapterMemory extends AdapterAbstract {
|
|
|
70
92
|
}
|
|
71
93
|
return Promise.resolve(true);
|
|
72
94
|
}
|
|
73
|
-
/**
|
|
95
|
+
/** @inheritdoc */
|
|
74
96
|
get(key) {
|
|
75
97
|
this._assertInitialized();
|
|
76
98
|
key = this._withNs(key);
|
|
@@ -82,7 +104,7 @@ export class AdapterMemory extends AdapterAbstract {
|
|
|
82
104
|
return Promise.resolve(null);
|
|
83
105
|
return JSON.parse(value);
|
|
84
106
|
}
|
|
85
|
-
/**
|
|
107
|
+
/** @inheritdoc */
|
|
86
108
|
delete(key) {
|
|
87
109
|
this._assertInitialized();
|
|
88
110
|
key = this._withNs(key);
|
|
@@ -91,7 +113,7 @@ export class AdapterMemory extends AdapterAbstract {
|
|
|
91
113
|
this.#expirations.delete(key);
|
|
92
114
|
return Promise.resolve(existed);
|
|
93
115
|
}
|
|
94
|
-
/**
|
|
116
|
+
/** @inheritdoc */
|
|
95
117
|
exists(key) {
|
|
96
118
|
this._assertInitialized();
|
|
97
119
|
key = this._withNs(key);
|
|
@@ -99,8 +121,7 @@ export class AdapterMemory extends AdapterAbstract {
|
|
|
99
121
|
return Promise.resolve(false);
|
|
100
122
|
return Promise.resolve(this.#store.has(key));
|
|
101
123
|
}
|
|
102
|
-
/**
|
|
103
|
-
* Recognizes redis-like star wildcard format. */
|
|
124
|
+
/** @inheritdoc */
|
|
104
125
|
keys(pattern) {
|
|
105
126
|
this._assertInitialized();
|
|
106
127
|
const all = Array.from(this.#store.keys())
|
|
@@ -119,7 +140,7 @@ export class AdapterMemory extends AdapterAbstract {
|
|
|
119
140
|
const regex = new RegExp(`^${regexPattern}$`);
|
|
120
141
|
return Promise.resolve(all.filter((key) => regex.test(key)));
|
|
121
142
|
}
|
|
122
|
-
/**
|
|
143
|
+
/** @inheritdoc */
|
|
123
144
|
async clear(pattern) {
|
|
124
145
|
this._assertInitialized();
|
|
125
146
|
const keysToDelete = await this.keys(pattern);
|
|
@@ -132,7 +153,7 @@ export class AdapterMemory extends AdapterAbstract {
|
|
|
132
153
|
}
|
|
133
154
|
return deleteCount;
|
|
134
155
|
}
|
|
135
|
-
/**
|
|
156
|
+
/** @inheritdoc */
|
|
136
157
|
async setMultiple(keyValuePairs, options = {}) {
|
|
137
158
|
this._assertInitialized();
|
|
138
159
|
const results = [];
|
|
@@ -142,7 +163,7 @@ export class AdapterMemory extends AdapterAbstract {
|
|
|
142
163
|
}
|
|
143
164
|
return results;
|
|
144
165
|
}
|
|
145
|
-
/**
|
|
166
|
+
/** @inheritdoc */
|
|
146
167
|
async getMultiple(keys) {
|
|
147
168
|
this._assertInitialized();
|
|
148
169
|
const result = {};
|
|
@@ -151,7 +172,7 @@ export class AdapterMemory extends AdapterAbstract {
|
|
|
151
172
|
}
|
|
152
173
|
return result;
|
|
153
174
|
}
|
|
154
|
-
/**
|
|
175
|
+
/** @inheritdoc */
|
|
155
176
|
expire(key, ttl) {
|
|
156
177
|
this._assertInitialized();
|
|
157
178
|
key = this._withNs(key);
|
|
@@ -161,7 +182,7 @@ export class AdapterMemory extends AdapterAbstract {
|
|
|
161
182
|
this.#expirations.set(key, new Date(Date.now() + ttl * 1000));
|
|
162
183
|
return Promise.resolve(true);
|
|
163
184
|
}
|
|
164
|
-
/**
|
|
185
|
+
/** @inheritdoc */
|
|
165
186
|
ttl(key) {
|
|
166
187
|
this._assertInitialized();
|
|
167
188
|
key = this._withNs(key);
|
|
@@ -179,7 +200,7 @@ export class AdapterMemory extends AdapterAbstract {
|
|
|
179
200
|
// const remaining = Math.max(0, expiresAt.valueOf() - Date.now());
|
|
180
201
|
// return Promise.resolve(new Date(Date.now() + Math.ceil(remaining)));
|
|
181
202
|
}
|
|
182
|
-
/**
|
|
203
|
+
/** @inheritdoc */
|
|
183
204
|
async transaction(operations) {
|
|
184
205
|
this._assertInitialized();
|
|
185
206
|
// note: memory operations are atomic by nature
|
|
@@ -1,43 +1,81 @@
|
|
|
1
1
|
import { AdapterAbstract, type Operation, type SetOptions, type AdapterAbstractOptions } from "./abstract.js";
|
|
2
2
|
import type pg from "pg";
|
|
3
|
+
/**
|
|
4
|
+
* Configuration options for the PostgreSQL KV adapter.
|
|
5
|
+
*/
|
|
3
6
|
export interface AdapterPostgresOptions extends AdapterAbstractOptions {
|
|
4
|
-
/** pg.Pool or pg.Client
|
|
7
|
+
/** PostgreSQL connection instance - either `pg.Pool` or `pg.Client`. */
|
|
5
8
|
db: pg.Pool | pg.Client;
|
|
6
|
-
/**
|
|
9
|
+
/**
|
|
10
|
+
* Name of the table to use for storing key-value pairs.
|
|
11
|
+
* The table will be created automatically if it doesn't exist.
|
|
12
|
+
* @default "__kv"
|
|
13
|
+
*/
|
|
7
14
|
tableName: string;
|
|
8
|
-
/**
|
|
15
|
+
/**
|
|
16
|
+
* Interval in seconds for automatic cleanup of expired keys.
|
|
17
|
+
* Set to 0 to disable automatic cleanup.
|
|
18
|
+
*/
|
|
9
19
|
ttlCleanupIntervalSec: number;
|
|
10
20
|
}
|
|
21
|
+
/**
|
|
22
|
+
* PostgreSQL key-value storage adapter.
|
|
23
|
+
*
|
|
24
|
+
* Provides persistent key-value storage using PostgreSQL with JSONB values.
|
|
25
|
+
* Automatically creates the required table and indexes on initialization.
|
|
26
|
+
*
|
|
27
|
+
* @remarks
|
|
28
|
+
* - Uses UPSERT (INSERT ... ON CONFLICT) for atomic set operations
|
|
29
|
+
* - Uses PostgreSQL transactions for the `transaction()` method
|
|
30
|
+
* - Values are stored as JSONB for efficient querying
|
|
31
|
+
* - Supports automatic TTL cleanup via background timer
|
|
32
|
+
* - Table schema: key (VARCHAR), value (JSONB), expires_at, created_at, updated_at
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* import pg from 'pg';
|
|
37
|
+
*
|
|
38
|
+
* const pool = new pg.Pool({ connectionString: 'postgres://...' });
|
|
39
|
+
* const client = createKVClient("myapp:", "postgres", {
|
|
40
|
+
* db: pool,
|
|
41
|
+
* tableName: "kv_store",
|
|
42
|
+
* ttlCleanupIntervalSec: 300, // Clean expired keys every 5 minutes
|
|
43
|
+
* });
|
|
44
|
+
* await client.initialize();
|
|
45
|
+
* await client.set("config:theme", { dark: true });
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
11
48
|
export declare class AdapterPostgres extends AdapterAbstract {
|
|
12
49
|
#private;
|
|
13
50
|
readonly namespace: string;
|
|
14
51
|
_type: string;
|
|
15
52
|
readonly options: AdapterPostgresOptions;
|
|
16
53
|
constructor(namespace?: string, options?: Partial<AdapterPostgresOptions>);
|
|
54
|
+
/** @inheritdoc */
|
|
17
55
|
initialize(): Promise<void>;
|
|
56
|
+
/** @inheritdoc */
|
|
18
57
|
destroy(hard?: boolean): Promise<void>;
|
|
19
|
-
/**
|
|
58
|
+
/** @inheritdoc */
|
|
20
59
|
set(key: string, value: any, options?: Partial<SetOptions>): Promise<boolean>;
|
|
21
|
-
/**
|
|
60
|
+
/** @inheritdoc */
|
|
22
61
|
get(key: string): Promise<any>;
|
|
23
|
-
/**
|
|
62
|
+
/** @inheritdoc */
|
|
24
63
|
delete(key: string): Promise<boolean>;
|
|
25
|
-
/**
|
|
64
|
+
/** @inheritdoc */
|
|
26
65
|
exists(key: string): Promise<boolean>;
|
|
27
|
-
/**
|
|
28
|
-
* Recognizes redis-like star wildcard format. */
|
|
66
|
+
/** @inheritdoc */
|
|
29
67
|
keys(pattern: string): Promise<string[]>;
|
|
30
|
-
/**
|
|
68
|
+
/** @inheritdoc */
|
|
31
69
|
clear(pattern: string): Promise<number>;
|
|
32
|
-
/**
|
|
70
|
+
/** @inheritdoc */
|
|
33
71
|
setMultiple(keyValuePairs: [string, any][], options?: Partial<SetOptions>): Promise<boolean[]>;
|
|
34
|
-
/**
|
|
72
|
+
/** @inheritdoc */
|
|
35
73
|
getMultiple(keys: string[]): Promise<Record<string, any>>;
|
|
36
|
-
/**
|
|
74
|
+
/** @inheritdoc */
|
|
37
75
|
expire(key: string, ttl: number): Promise<boolean>;
|
|
38
|
-
/**
|
|
76
|
+
/** @inheritdoc */
|
|
39
77
|
ttl(key: string): Promise<Date | null | false>;
|
|
40
|
-
/**
|
|
78
|
+
/** @inheritdoc */
|
|
41
79
|
transaction(operations: Operation[]): Promise<any[]>;
|
|
42
80
|
__debug_dump(): Promise<Record<string, {
|
|
43
81
|
value: any;
|