@marianmeres/kv 1.3.2 → 2.2.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/AGENTS.md +54 -18
- package/API.md +273 -31
- package/CLAUDE.md +3 -0
- package/README.md +126 -9
- package/dist/adapter/abstract.d.ts +140 -24
- package/dist/adapter/abstract.js +141 -23
- package/dist/adapter/deno-kv.d.ts +41 -11
- package/dist/adapter/deno-kv.js +251 -62
- package/dist/adapter/memory.d.ts +16 -4
- package/dist/adapter/memory.js +137 -80
- package/dist/adapter/postgres.d.ts +18 -25
- package/dist/adapter/postgres.js +294 -117
- package/dist/adapter/redis.d.ts +17 -4
- package/dist/adapter/redis.js +213 -40
- package/dist/kv.d.ts +2 -2
- package/dist/kv.js +1 -1
- package/package.json +14 -5
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Deno KV key-value storage adapter implementation.
|
|
5
5
|
*/
|
|
6
|
-
import { AdapterAbstract, type Operation, type SetOptions, type AdapterAbstractOptions } from "./abstract.js";
|
|
6
|
+
import { AdapterAbstract, type Operation, type SetMultipleEntry, type SetOptions, type TtlResult, type AdapterAbstractOptions } from "./abstract.js";
|
|
7
7
|
/**
|
|
8
8
|
* Configuration options for the Deno KV adapter.
|
|
9
9
|
*/
|
|
@@ -13,6 +13,20 @@ export interface AdapterDenoKvOptions extends AdapterAbstractOptions {
|
|
|
13
13
|
* @see https://docs.deno.com/deploy/kv/#testing
|
|
14
14
|
*/
|
|
15
15
|
db: any;
|
|
16
|
+
/**
|
|
17
|
+
* When `true`, `delete()` pre-checks the key's existence and returns
|
|
18
|
+
* the real "did it exist?" flag — at the cost of one extra round-trip.
|
|
19
|
+
* Default `false` preserves the Deno.Kv native "always true" behavior.
|
|
20
|
+
*/
|
|
21
|
+
strictDeleteResult?: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Max retry attempts for CAS-based primitives (`incr`, `decr`, `getSet`,
|
|
24
|
+
* `cas`) before throwing `KvRaceError`. Each attempt waits a small
|
|
25
|
+
* exponential-backoff delay, so 20 retries covers substantial contention
|
|
26
|
+
* without blocking callers for long.
|
|
27
|
+
* @default 20
|
|
28
|
+
*/
|
|
29
|
+
atomicRetryAttempts?: number;
|
|
16
30
|
}
|
|
17
31
|
/**
|
|
18
32
|
* Deno KV key-value storage adapter.
|
|
@@ -22,11 +36,14 @@ export interface AdapterDenoKvOptions extends AdapterAbstractOptions {
|
|
|
22
36
|
*
|
|
23
37
|
* @remarks
|
|
24
38
|
* - Only works in Deno runtime (will throw error in Node.js)
|
|
25
|
-
* - `delete()`
|
|
26
|
-
*
|
|
27
|
-
* - `
|
|
28
|
-
* -
|
|
39
|
+
* - `delete()` returns `true` by default even for missing keys (Deno.Kv
|
|
40
|
+
* limitation); opt in to strict behavior via `strictDeleteResult: true`
|
|
41
|
+
* - `expire()` is not supported — always returns `false`
|
|
42
|
+
* - `ttl()` returns `{ state: "no-ttl" }` for present keys (TTL cannot be
|
|
43
|
+
* queried) and `{ state: "missing" }` for absent ones
|
|
29
44
|
* - Uses `getMany()` for efficient batch reads
|
|
45
|
+
* - Atomic primitives (`incr`, `decr`, `getSet`, `cas`) use a CAS retry loop
|
|
46
|
+
* backed by Deno.Kv's native `atomic().check()`
|
|
30
47
|
*
|
|
31
48
|
* @example
|
|
32
49
|
* ```typescript
|
|
@@ -49,6 +66,16 @@ export declare class AdapterDenoKv extends AdapterAbstract {
|
|
|
49
66
|
/** @inheritdoc */
|
|
50
67
|
set(key: string, value: any, options?: Partial<SetOptions>): Promise<boolean>;
|
|
51
68
|
/** @inheritdoc */
|
|
69
|
+
setIfAbsent(key: string, value: any, options?: Partial<SetOptions>): Promise<boolean>;
|
|
70
|
+
/** @inheritdoc */
|
|
71
|
+
getSet(key: string, value: any, options?: Partial<SetOptions>): Promise<any>;
|
|
72
|
+
/** @inheritdoc */
|
|
73
|
+
incr(key: string, by?: number, options?: Partial<SetOptions>): Promise<number>;
|
|
74
|
+
/** @inheritdoc */
|
|
75
|
+
decr(key: string, by?: number, options?: Partial<SetOptions>): Promise<number>;
|
|
76
|
+
/** @inheritdoc */
|
|
77
|
+
cas(key: string, expected: any, next: any, options?: Partial<SetOptions>): Promise<boolean>;
|
|
78
|
+
/** @inheritdoc */
|
|
52
79
|
get(key: string): Promise<any>;
|
|
53
80
|
/** @inheritdoc */
|
|
54
81
|
exists(key: string): Promise<boolean>;
|
|
@@ -57,21 +84,24 @@ export declare class AdapterDenoKv extends AdapterAbstract {
|
|
|
57
84
|
/** @inheritdoc */
|
|
58
85
|
keys(pattern: string): Promise<string[]>;
|
|
59
86
|
/** @inheritdoc */
|
|
87
|
+
keysIter(pattern: string): AsyncIterable<string>;
|
|
88
|
+
/** @inheritdoc */
|
|
60
89
|
clear(pattern: string): Promise<number>;
|
|
61
90
|
/** @inheritdoc */
|
|
62
|
-
setMultiple(
|
|
91
|
+
setMultiple(entries: readonly SetMultipleEntry[], options?: Partial<SetOptions>): Promise<boolean[]>;
|
|
63
92
|
/** @inheritdoc */
|
|
64
93
|
getMultiple(keys: string[]): Promise<Record<string, any>>;
|
|
65
94
|
/** @inheritdoc */
|
|
66
95
|
transaction(operations: Operation[]): Promise<any[]>;
|
|
67
96
|
/**
|
|
68
|
-
* Not supported in Deno.Kv
|
|
97
|
+
* Not supported in Deno.Kv — always returns `false`.
|
|
69
98
|
* @inheritdoc
|
|
70
99
|
*/
|
|
71
|
-
expire(
|
|
100
|
+
expire(_key: string, _ttl: number): Promise<boolean>;
|
|
72
101
|
/**
|
|
73
|
-
*
|
|
74
|
-
*
|
|
102
|
+
* Deno.Kv cannot report TTL state. Returns `{ state: "no-ttl" }` for
|
|
103
|
+
* existing keys (whether or not they actually have an expireIn set)
|
|
104
|
+
* and `{ state: "missing" }` for absent keys.
|
|
75
105
|
*/
|
|
76
|
-
ttl(key: string): Promise<
|
|
106
|
+
ttl(key: string): Promise<TtlResult>;
|
|
77
107
|
}
|
package/dist/adapter/deno-kv.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* Deno KV key-value storage adapter implementation.
|
|
5
5
|
*/
|
|
6
6
|
import { createClog } from "@marianmeres/clog";
|
|
7
|
-
import { AdapterAbstract, } from "./abstract.js";
|
|
7
|
+
import { AdapterAbstract, KvRaceError, } from "./abstract.js";
|
|
8
8
|
/**
|
|
9
9
|
* Deno KV key-value storage adapter.
|
|
10
10
|
*
|
|
@@ -13,11 +13,14 @@ import { AdapterAbstract, } from "./abstract.js";
|
|
|
13
13
|
*
|
|
14
14
|
* @remarks
|
|
15
15
|
* - Only works in Deno runtime (will throw error in Node.js)
|
|
16
|
-
* - `delete()`
|
|
17
|
-
*
|
|
18
|
-
* - `
|
|
19
|
-
* -
|
|
16
|
+
* - `delete()` returns `true` by default even for missing keys (Deno.Kv
|
|
17
|
+
* limitation); opt in to strict behavior via `strictDeleteResult: true`
|
|
18
|
+
* - `expire()` is not supported — always returns `false`
|
|
19
|
+
* - `ttl()` returns `{ state: "no-ttl" }` for present keys (TTL cannot be
|
|
20
|
+
* queried) and `{ state: "missing" }` for absent ones
|
|
20
21
|
* - Uses `getMany()` for efficient batch reads
|
|
22
|
+
* - Atomic primitives (`incr`, `decr`, `getSet`, `cas`) use a CAS retry loop
|
|
23
|
+
* backed by Deno.Kv's native `atomic().check()`
|
|
21
24
|
*
|
|
22
25
|
* @example
|
|
23
26
|
* ```typescript
|
|
@@ -34,6 +37,9 @@ export class AdapterDenoKv extends AdapterAbstract {
|
|
|
34
37
|
defaultTtl: 0, // no ttl by default
|
|
35
38
|
db: null, // will be set in constructor via options merge
|
|
36
39
|
logger: createClog("KV/deno-kv"),
|
|
40
|
+
validateKeys: true,
|
|
41
|
+
strictDeleteResult: false,
|
|
42
|
+
atomicRetryAttempts: 20,
|
|
37
43
|
};
|
|
38
44
|
constructor(namespace = "", options = {}) {
|
|
39
45
|
super();
|
|
@@ -55,6 +61,9 @@ export class AdapterDenoKv extends AdapterAbstract {
|
|
|
55
61
|
return Promise.resolve();
|
|
56
62
|
}
|
|
57
63
|
#denoKvKey(key, full = true) {
|
|
64
|
+
// validate via base (no-op if validateKeys is false)
|
|
65
|
+
if (full)
|
|
66
|
+
this._withNs(key);
|
|
58
67
|
const parts = key.split(":");
|
|
59
68
|
const prefix = parts[0];
|
|
60
69
|
const rest = parts.slice(1).join(":");
|
|
@@ -67,19 +76,32 @@ export class AdapterDenoKv extends AdapterAbstract {
|
|
|
67
76
|
async set(key, value, options = {}) {
|
|
68
77
|
this._assertInitialized();
|
|
69
78
|
const { db } = this.options;
|
|
70
|
-
const ttl =
|
|
79
|
+
const ttl = this._resolveTtl(options);
|
|
71
80
|
if (value === undefined)
|
|
72
81
|
value = null;
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
// across adapters)
|
|
76
|
-
value = JSON.stringify(value);
|
|
77
|
-
await db.set(this.#denoKvKey(key), value, {
|
|
82
|
+
const serialized = JSON.stringify(value);
|
|
83
|
+
await db.set(this.#denoKvKey(key), serialized, {
|
|
78
84
|
expireIn: ttl ? ttl * 1000 : undefined,
|
|
79
85
|
});
|
|
80
86
|
return true;
|
|
81
87
|
}
|
|
82
|
-
/**
|
|
88
|
+
/** @inheritdoc */
|
|
89
|
+
async setIfAbsent(key, value, options = {}) {
|
|
90
|
+
this._assertInitialized();
|
|
91
|
+
const { db } = this.options;
|
|
92
|
+
const ttl = this._resolveTtl(options);
|
|
93
|
+
if (value === undefined)
|
|
94
|
+
value = null;
|
|
95
|
+
const serialized = JSON.stringify(value);
|
|
96
|
+
const kvKey = this.#denoKvKey(key);
|
|
97
|
+
const res = await db
|
|
98
|
+
.atomic()
|
|
99
|
+
.check({ key: kvKey, versionstamp: null })
|
|
100
|
+
.set(kvKey, serialized, { expireIn: ttl ? ttl * 1000 : undefined })
|
|
101
|
+
.commit();
|
|
102
|
+
return !!res?.ok;
|
|
103
|
+
}
|
|
104
|
+
/** Parses a Deno.Kv entry's stored value back to its JSON form. */
|
|
83
105
|
#parseValue(row) {
|
|
84
106
|
try {
|
|
85
107
|
return JSON.parse(`${row?.value ?? null}`);
|
|
@@ -88,6 +110,106 @@ export class AdapterDenoKv extends AdapterAbstract {
|
|
|
88
110
|
return row?.value;
|
|
89
111
|
}
|
|
90
112
|
}
|
|
113
|
+
/**
|
|
114
|
+
* Runs a CAS-style update. The `mutate` fn takes the current value
|
|
115
|
+
* (or `undefined` when missing) and returns `{ next, ttlMs?, result }`.
|
|
116
|
+
* Retries on atomic-commit contention; throws `KvRaceError` on exhaustion.
|
|
117
|
+
*/
|
|
118
|
+
async #atomicUpdate(key, mutate) {
|
|
119
|
+
const { db, atomicRetryAttempts = 20 } = this.options;
|
|
120
|
+
const kvKey = this.#denoKvKey(key);
|
|
121
|
+
for (let attempt = 0; attempt < atomicRetryAttempts; attempt++) {
|
|
122
|
+
const entry = await db.get(kvKey);
|
|
123
|
+
const exists = entry?.versionstamp !== null && entry?.versionstamp !== undefined;
|
|
124
|
+
const current = exists ? this.#parseValue(entry) : undefined;
|
|
125
|
+
const decision = mutate(current, exists);
|
|
126
|
+
if ("abort" in decision)
|
|
127
|
+
return decision.result;
|
|
128
|
+
const nextSerialized = JSON.stringify(decision.next === undefined ? null : decision.next);
|
|
129
|
+
const atomic = db.atomic();
|
|
130
|
+
atomic.check({
|
|
131
|
+
key: kvKey,
|
|
132
|
+
versionstamp: exists ? entry.versionstamp : null,
|
|
133
|
+
});
|
|
134
|
+
atomic.set(kvKey, nextSerialized, { expireIn: decision.expireInMs });
|
|
135
|
+
const res = await atomic.commit();
|
|
136
|
+
if (res?.ok)
|
|
137
|
+
return decision.result;
|
|
138
|
+
// Exponential backoff with jitter, capped at 50ms. Minimal blocking,
|
|
139
|
+
// enough to break symmetric contention between many parallel callers.
|
|
140
|
+
const backoffMs = Math.min(50, 2 ** Math.min(attempt, 6)) * (0.5 + Math.random());
|
|
141
|
+
await new Promise((r) => setTimeout(r, backoffMs));
|
|
142
|
+
}
|
|
143
|
+
throw new KvRaceError(`Deno.Kv atomic update retries exhausted (${atomicRetryAttempts}) for key "${key}"`);
|
|
144
|
+
}
|
|
145
|
+
/** @inheritdoc */
|
|
146
|
+
async getSet(key, value, options = {}) {
|
|
147
|
+
this._assertInitialized();
|
|
148
|
+
const ttl = this._resolveTtl(options);
|
|
149
|
+
const expireInMs = ttl ? ttl * 1000 : undefined;
|
|
150
|
+
const next = value === undefined ? null : value;
|
|
151
|
+
return this.#atomicUpdate(key, (current) => ({
|
|
152
|
+
next,
|
|
153
|
+
expireInMs,
|
|
154
|
+
result: current === undefined ? null : current,
|
|
155
|
+
}));
|
|
156
|
+
}
|
|
157
|
+
async #incrBy(key, delta, options = {}) {
|
|
158
|
+
this._assertInitialized();
|
|
159
|
+
const ttl = this._resolveTtl(options);
|
|
160
|
+
const expireInMs = ttl ? ttl * 1000 : undefined;
|
|
161
|
+
return this.#atomicUpdate(key, (current, exists) => {
|
|
162
|
+
let base = 0;
|
|
163
|
+
if (exists) {
|
|
164
|
+
if (typeof current !== "number") {
|
|
165
|
+
throw new TypeError("KV value is not a number");
|
|
166
|
+
}
|
|
167
|
+
base = current;
|
|
168
|
+
}
|
|
169
|
+
const next = base + delta;
|
|
170
|
+
return {
|
|
171
|
+
next,
|
|
172
|
+
// Only apply TTL on key creation; preserving TTL on existing
|
|
173
|
+
// keys is not natively possible in Deno.Kv (every atomic set
|
|
174
|
+
// resets expireIn), so we skip expireIn when the key existed.
|
|
175
|
+
expireInMs: exists ? undefined : expireInMs,
|
|
176
|
+
result: next,
|
|
177
|
+
};
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
/** @inheritdoc */
|
|
181
|
+
incr(key, by = 1, options = {}) {
|
|
182
|
+
return this.#incrBy(key, by, options);
|
|
183
|
+
}
|
|
184
|
+
/** @inheritdoc */
|
|
185
|
+
decr(key, by = 1, options = {}) {
|
|
186
|
+
return this.#incrBy(key, -by, options);
|
|
187
|
+
}
|
|
188
|
+
/** @inheritdoc */
|
|
189
|
+
async cas(key, expected, next, options = {}) {
|
|
190
|
+
this._assertInitialized();
|
|
191
|
+
const hasTtl = options.ttl !== undefined;
|
|
192
|
+
const ttl = hasTtl ? this._resolveTtl(options) : undefined;
|
|
193
|
+
const expireInMs = ttl ? ttl * 1000 : undefined;
|
|
194
|
+
const expectedCanonical = JSON.stringify(expected ?? null);
|
|
195
|
+
return this.#atomicUpdate(key, (current, exists) => {
|
|
196
|
+
if (!exists)
|
|
197
|
+
return { abort: true, result: false };
|
|
198
|
+
if (JSON.stringify(current ?? null) !== expectedCanonical) {
|
|
199
|
+
return { abort: true, result: false };
|
|
200
|
+
}
|
|
201
|
+
return {
|
|
202
|
+
next: next === undefined ? null : next,
|
|
203
|
+
// When no TTL option is provided, preserve existing TTL. Deno.Kv
|
|
204
|
+
// doesn't let us read/keep it, so this is best-effort: omit
|
|
205
|
+
// `expireIn` and the atomic set clears any existing expiration.
|
|
206
|
+
// Documented limitation — callers who want to preserve TTL must
|
|
207
|
+
// re-supply it.
|
|
208
|
+
expireInMs: hasTtl ? expireInMs : undefined,
|
|
209
|
+
result: true,
|
|
210
|
+
};
|
|
211
|
+
});
|
|
212
|
+
}
|
|
91
213
|
/** @inheritdoc */
|
|
92
214
|
async get(key) {
|
|
93
215
|
this._assertInitialized();
|
|
@@ -98,110 +220,177 @@ export class AdapterDenoKv extends AdapterAbstract {
|
|
|
98
220
|
/** @inheritdoc */
|
|
99
221
|
async exists(key) {
|
|
100
222
|
this._assertInitialized();
|
|
101
|
-
|
|
223
|
+
const { db } = this.options;
|
|
224
|
+
const entry = await db.get(this.#denoKvKey(key));
|
|
225
|
+
return entry?.versionstamp !== null && entry?.versionstamp !== undefined;
|
|
102
226
|
}
|
|
103
227
|
/** @inheritdoc */
|
|
104
228
|
async delete(key) {
|
|
105
229
|
this._assertInitialized();
|
|
106
|
-
const { db } = this.options;
|
|
107
|
-
|
|
108
|
-
|
|
230
|
+
const { db, strictDeleteResult } = this.options;
|
|
231
|
+
const kvKey = this.#denoKvKey(key);
|
|
232
|
+
if (strictDeleteResult) {
|
|
233
|
+
const entry = await db.get(kvKey);
|
|
234
|
+
const existed = entry?.versionstamp !== null && entry?.versionstamp !== undefined;
|
|
235
|
+
await db.delete(kvKey);
|
|
236
|
+
return existed;
|
|
237
|
+
}
|
|
238
|
+
await db.delete(kvKey);
|
|
239
|
+
return true;
|
|
109
240
|
}
|
|
110
241
|
/** @inheritdoc */
|
|
111
242
|
async keys(pattern) {
|
|
112
243
|
this._assertInitialized();
|
|
113
|
-
const { db } = this.options;
|
|
114
|
-
const [prefix, search] = this.#denoKvKey(pattern, false);
|
|
115
|
-
// convert Redis-style pattern to regex
|
|
116
|
-
const regexPattern = pattern.replace(/\*/g, ".*").replace(/\?/g, ".");
|
|
117
|
-
const regex = new RegExp(`^${regexPattern}$`);
|
|
118
|
-
const iter = db.list({
|
|
119
|
-
prefix: [this.namespace, prefix === "*" ? undefined : prefix].filter(Boolean),
|
|
120
|
-
});
|
|
121
244
|
const out = [];
|
|
122
|
-
for await (const
|
|
123
|
-
|
|
245
|
+
for await (const k of this.keysIter(pattern))
|
|
246
|
+
out.push(k);
|
|
247
|
+
return out.toSorted();
|
|
248
|
+
}
|
|
249
|
+
/** @inheritdoc */
|
|
250
|
+
async *keysIter(pattern) {
|
|
251
|
+
this._assertInitialized();
|
|
252
|
+
const { db } = this.options;
|
|
253
|
+
const [firstSeg] = this.#denoKvKey(pattern, false);
|
|
254
|
+
const regex = this._globToRegex(pattern);
|
|
255
|
+
// Only use the first key segment as a Deno.Kv prefix if it contains
|
|
256
|
+
// no wildcards — otherwise scan the whole namespace and regex-filter.
|
|
257
|
+
const firstIsLiteral = firstSeg !== undefined && firstSeg !== "*" && !/[*?]/.test(firstSeg);
|
|
258
|
+
const listPrefix = firstIsLiteral
|
|
259
|
+
? [this.namespace, firstSeg]
|
|
260
|
+
: [this.namespace];
|
|
261
|
+
for await (const res of db.list({ prefix: listPrefix })) {
|
|
262
|
+
// Iterate entries (Deno.Kv has no keys-only API) but do not touch
|
|
263
|
+
// `res.value` — no JSON parse, no copy.
|
|
124
264
|
const key = res.key.slice(1).join(":");
|
|
125
|
-
if (
|
|
126
|
-
|
|
265
|
+
if (pattern === "*" || regex.test(key)) {
|
|
266
|
+
yield key;
|
|
127
267
|
}
|
|
128
268
|
}
|
|
129
|
-
return out;
|
|
130
269
|
}
|
|
131
270
|
/** @inheritdoc */
|
|
132
271
|
async clear(pattern) {
|
|
133
272
|
this._assertInitialized();
|
|
134
|
-
const
|
|
273
|
+
const { db } = this.options;
|
|
135
274
|
let deleteCount = 0;
|
|
136
|
-
|
|
137
|
-
|
|
275
|
+
// Batch deletes via atomic to reduce round-trips — but Deno.Kv caps
|
|
276
|
+
// atomic operations, so chunk at 500.
|
|
277
|
+
const CHUNK = 500;
|
|
278
|
+
let atomic = db.atomic();
|
|
279
|
+
let inBatch = 0;
|
|
280
|
+
for await (const key of this.keysIter(pattern)) {
|
|
281
|
+
atomic.delete(this.#denoKvKey(key));
|
|
282
|
+
inBatch++;
|
|
138
283
|
deleteCount++;
|
|
284
|
+
if (inBatch >= CHUNK) {
|
|
285
|
+
await atomic.commit();
|
|
286
|
+
atomic = db.atomic();
|
|
287
|
+
inBatch = 0;
|
|
288
|
+
}
|
|
139
289
|
}
|
|
290
|
+
if (inBatch > 0)
|
|
291
|
+
await atomic.commit();
|
|
140
292
|
return deleteCount;
|
|
141
293
|
}
|
|
142
294
|
/** @inheritdoc */
|
|
143
|
-
async setMultiple(
|
|
295
|
+
async setMultiple(entries, options = {}) {
|
|
144
296
|
this._assertInitialized();
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
297
|
+
if (entries.length === 0)
|
|
298
|
+
return [];
|
|
299
|
+
const normalized = this._normalizePairs(entries);
|
|
300
|
+
const { db } = this.options;
|
|
301
|
+
// Atomic commit for all-or-nothing semantics.
|
|
302
|
+
const atomic = db.atomic();
|
|
303
|
+
for (const { key, value, ttl: pairTtl } of normalized) {
|
|
304
|
+
const ttl = this._resolveTtl({ ttl: pairTtl ?? options.ttl });
|
|
305
|
+
const v = value === undefined ? null : value;
|
|
306
|
+
atomic.set(this.#denoKvKey(key), JSON.stringify(v), {
|
|
307
|
+
expireIn: ttl ? ttl * 1000 : undefined,
|
|
308
|
+
});
|
|
149
309
|
}
|
|
150
|
-
|
|
310
|
+
const res = await atomic.commit();
|
|
311
|
+
const ok = !!res?.ok;
|
|
312
|
+
return normalized.map(() => ok);
|
|
151
313
|
}
|
|
152
314
|
/** @inheritdoc */
|
|
153
315
|
async getMultiple(keys) {
|
|
154
316
|
this._assertInitialized();
|
|
155
317
|
const { db } = this.options;
|
|
156
318
|
const result = {};
|
|
319
|
+
if (keys.length === 0)
|
|
320
|
+
return result;
|
|
157
321
|
const denoKvKeys = keys.map((k) => this.#denoKvKey(k));
|
|
158
322
|
const results = await db.getMany(denoKvKeys);
|
|
159
323
|
for (const [index, row] of results.entries()) {
|
|
160
|
-
|
|
324
|
+
const origKey = keys[index];
|
|
325
|
+
const hasValue = row?.versionstamp !== null && row?.versionstamp !== undefined;
|
|
326
|
+
result[origKey] = hasValue ? this.#parseValue(row) : null;
|
|
161
327
|
}
|
|
162
328
|
return result;
|
|
163
329
|
}
|
|
164
330
|
/** @inheritdoc */
|
|
165
331
|
async transaction(operations) {
|
|
166
332
|
this._assertInitialized();
|
|
167
|
-
|
|
168
|
-
//
|
|
169
|
-
|
|
170
|
-
|
|
333
|
+
const { db } = this.options;
|
|
334
|
+
// When the transaction contains only set/delete ops we can use Deno.Kv's
|
|
335
|
+
// native atomic() for true all-or-nothing semantics. A `get` inside the
|
|
336
|
+
// transaction breaks that — Deno.Kv has no atomic-read — so in that
|
|
337
|
+
// case we fall back to sequential execution.
|
|
338
|
+
const hasGet = operations.some((op) => op.type === "get");
|
|
339
|
+
if (!hasGet && operations.length > 0) {
|
|
340
|
+
const atomic = db.atomic();
|
|
171
341
|
for (const op of operations) {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
break;
|
|
179
|
-
case "delete":
|
|
180
|
-
results.push(await this.delete(op.key));
|
|
181
|
-
break;
|
|
342
|
+
if (op.type === "set") {
|
|
343
|
+
const ttl = this._resolveTtl(op.options);
|
|
344
|
+
const v = op.value === undefined ? null : op.value;
|
|
345
|
+
atomic.set(this.#denoKvKey(op.key), JSON.stringify(v), {
|
|
346
|
+
expireIn: ttl ? ttl * 1000 : undefined,
|
|
347
|
+
});
|
|
182
348
|
}
|
|
349
|
+
else if (op.type === "delete") {
|
|
350
|
+
atomic.delete(this.#denoKvKey(op.key));
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
const res = await atomic.commit();
|
|
354
|
+
if (!res?.ok) {
|
|
355
|
+
throw new Error("Deno.Kv atomic transaction failed to commit");
|
|
183
356
|
}
|
|
357
|
+
return operations.map(() => true);
|
|
184
358
|
}
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
359
|
+
// Sequential fallback — operations are NOT atomic.
|
|
360
|
+
const results = [];
|
|
361
|
+
for (const op of operations) {
|
|
362
|
+
switch (op.type) {
|
|
363
|
+
case "set":
|
|
364
|
+
results.push(await this.set(op.key, op.value, op.options || {}));
|
|
365
|
+
break;
|
|
366
|
+
case "get":
|
|
367
|
+
results.push(await this.get(op.key));
|
|
368
|
+
break;
|
|
369
|
+
case "delete":
|
|
370
|
+
results.push(await this.delete(op.key));
|
|
371
|
+
break;
|
|
372
|
+
}
|
|
188
373
|
}
|
|
189
374
|
return results;
|
|
190
375
|
}
|
|
191
376
|
/**
|
|
192
|
-
* Not supported in Deno.Kv
|
|
377
|
+
* Not supported in Deno.Kv — always returns `false`.
|
|
193
378
|
* @inheritdoc
|
|
194
379
|
*/
|
|
195
|
-
expire(
|
|
380
|
+
expire(_key, _ttl) {
|
|
196
381
|
this._assertInitialized();
|
|
197
382
|
return Promise.resolve(false);
|
|
198
383
|
}
|
|
199
384
|
/**
|
|
200
|
-
*
|
|
201
|
-
*
|
|
385
|
+
* Deno.Kv cannot report TTL state. Returns `{ state: "no-ttl" }` for
|
|
386
|
+
* existing keys (whether or not they actually have an expireIn set)
|
|
387
|
+
* and `{ state: "missing" }` for absent keys.
|
|
202
388
|
*/
|
|
203
|
-
ttl(key) {
|
|
389
|
+
async ttl(key) {
|
|
204
390
|
this._assertInitialized();
|
|
205
|
-
|
|
391
|
+
const { db } = this.options;
|
|
392
|
+
const entry = await db.get(this.#denoKvKey(key));
|
|
393
|
+
const exists = entry?.versionstamp !== null && entry?.versionstamp !== undefined;
|
|
394
|
+
return exists ? { state: "no-ttl" } : { state: "missing" };
|
|
206
395
|
}
|
|
207
396
|
}
|
package/dist/adapter/memory.d.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* In-memory key-value storage adapter implementation.
|
|
5
5
|
*/
|
|
6
|
-
import { AdapterAbstract, type AdapterAbstractOptions, type Operation, type SetOptions } from "./abstract.js";
|
|
6
|
+
import { AdapterAbstract, type AdapterAbstractOptions, type Operation, type SetMultipleEntry, type SetOptions, type TtlResult } from "./abstract.js";
|
|
7
7
|
/**
|
|
8
8
|
* Configuration options for the in-memory KV adapter.
|
|
9
9
|
*/
|
|
@@ -50,6 +50,16 @@ export declare class AdapterMemory extends AdapterAbstract {
|
|
|
50
50
|
/** @inheritdoc */
|
|
51
51
|
set(key: string, value: any, options?: Partial<SetOptions>): Promise<boolean>;
|
|
52
52
|
/** @inheritdoc */
|
|
53
|
+
setIfAbsent(key: string, value: any, options?: Partial<SetOptions>): Promise<boolean>;
|
|
54
|
+
/** @inheritdoc */
|
|
55
|
+
getSet(key: string, value: any, options?: Partial<SetOptions>): Promise<any>;
|
|
56
|
+
/** @inheritdoc */
|
|
57
|
+
incr(key: string, by?: number, options?: Partial<SetOptions>): Promise<number>;
|
|
58
|
+
/** @inheritdoc */
|
|
59
|
+
decr(key: string, by?: number, options?: Partial<SetOptions>): Promise<number>;
|
|
60
|
+
/** @inheritdoc */
|
|
61
|
+
cas(key: string, expected: any, next: any, options?: Partial<SetOptions>): Promise<boolean>;
|
|
62
|
+
/** @inheritdoc */
|
|
53
63
|
get(key: string): Promise<any>;
|
|
54
64
|
/** @inheritdoc */
|
|
55
65
|
delete(key: string): Promise<boolean>;
|
|
@@ -58,19 +68,21 @@ export declare class AdapterMemory extends AdapterAbstract {
|
|
|
58
68
|
/** @inheritdoc */
|
|
59
69
|
keys(pattern: string): Promise<string[]>;
|
|
60
70
|
/** @inheritdoc */
|
|
71
|
+
keysIter(pattern: string): AsyncIterable<string>;
|
|
72
|
+
/** @inheritdoc */
|
|
61
73
|
clear(pattern: string): Promise<number>;
|
|
62
74
|
/** @inheritdoc */
|
|
63
|
-
setMultiple(
|
|
75
|
+
setMultiple(entries: readonly SetMultipleEntry[], options?: Partial<SetOptions>): Promise<boolean[]>;
|
|
64
76
|
/** @inheritdoc */
|
|
65
77
|
getMultiple(keys: string[]): Promise<Record<string, any>>;
|
|
66
78
|
/** @inheritdoc */
|
|
67
79
|
expire(key: string, ttl: number): Promise<boolean>;
|
|
68
80
|
/** @inheritdoc */
|
|
69
|
-
ttl(key: string): Promise<
|
|
81
|
+
ttl(key: string): Promise<TtlResult>;
|
|
70
82
|
/** @inheritdoc */
|
|
71
83
|
transaction(operations: Operation[]): Promise<any[]>;
|
|
72
84
|
__debug_dump(): Promise<Record<string, {
|
|
73
85
|
value: unknown;
|
|
74
|
-
ttl: Date | null
|
|
86
|
+
ttl: Date | null;
|
|
75
87
|
}>>;
|
|
76
88
|
}
|