@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
package/dist/adapter/postgres.js
CHANGED
|
@@ -10,27 +10,6 @@ import { createClog } from "@marianmeres/clog";
|
|
|
10
10
|
*
|
|
11
11
|
* Provides persistent key-value storage using PostgreSQL with JSONB values.
|
|
12
12
|
* Automatically creates the required table and indexes on initialization.
|
|
13
|
-
*
|
|
14
|
-
* @remarks
|
|
15
|
-
* - Uses UPSERT (INSERT ... ON CONFLICT) for atomic set operations
|
|
16
|
-
* - Uses PostgreSQL transactions for the `transaction()` method
|
|
17
|
-
* - Values are stored as JSONB for efficient querying
|
|
18
|
-
* - Supports automatic TTL cleanup via background timer
|
|
19
|
-
* - Table schema: key (VARCHAR), value (JSONB), expires_at, created_at, updated_at
|
|
20
|
-
*
|
|
21
|
-
* @example
|
|
22
|
-
* ```typescript
|
|
23
|
-
* import pg from 'pg';
|
|
24
|
-
*
|
|
25
|
-
* const pool = new pg.Pool({ connectionString: 'postgres://...' });
|
|
26
|
-
* const client = createKVClient("myapp:", "postgres", {
|
|
27
|
-
* db: pool,
|
|
28
|
-
* tableName: "kv_store",
|
|
29
|
-
* ttlCleanupIntervalSec: 300, // Clean expired keys every 5 minutes
|
|
30
|
-
* });
|
|
31
|
-
* await client.initialize();
|
|
32
|
-
* await client.set("config:theme", { dark: true });
|
|
33
|
-
* ```
|
|
34
13
|
*/
|
|
35
14
|
export class AdapterPostgres extends AdapterAbstract {
|
|
36
15
|
namespace;
|
|
@@ -41,6 +20,7 @@ export class AdapterPostgres extends AdapterAbstract {
|
|
|
41
20
|
db: null, // will be set in constructor via options merge
|
|
42
21
|
tableName: "__kv",
|
|
43
22
|
ttlCleanupIntervalSec: 0,
|
|
23
|
+
validateKeys: true,
|
|
44
24
|
};
|
|
45
25
|
#cleanupTimer;
|
|
46
26
|
constructor(namespace = "", options = {}) {
|
|
@@ -51,12 +31,19 @@ export class AdapterPostgres extends AdapterAbstract {
|
|
|
51
31
|
if (!this.options.db) {
|
|
52
32
|
throw new Error("Missing pg instance");
|
|
53
33
|
}
|
|
34
|
+
// validate tableName — it is spliced into SQL verbatim, so guard
|
|
35
|
+
// against surprises. Allow optional schema prefix (one dot).
|
|
36
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z_][A-Za-z0-9_]*)?$/.test(this.options.tableName)) {
|
|
37
|
+
throw new Error(`Invalid tableName "${this.options.tableName}". Only word characters and a single "schema." prefix are allowed.`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/** Safe slug derived from tableName for use inside other identifiers (e.g. index names). */
|
|
41
|
+
get #safeSlug() {
|
|
42
|
+
return this.options.tableName.replace(/\W/g, "_");
|
|
54
43
|
}
|
|
55
44
|
/** @inheritdoc */
|
|
56
45
|
async initialize() {
|
|
57
46
|
const { db, tableName } = this.options;
|
|
58
|
-
// so we can work with "schema." prefix in naming things...
|
|
59
|
-
const safe = (name) => `${name}`.replace(/\W/g, "");
|
|
60
47
|
await db.query(`
|
|
61
48
|
CREATE TABLE IF NOT EXISTS ${tableName} (
|
|
62
49
|
key VARCHAR(512) PRIMARY KEY,
|
|
@@ -67,13 +54,13 @@ export class AdapterPostgres extends AdapterAbstract {
|
|
|
67
54
|
)
|
|
68
55
|
`);
|
|
69
56
|
await db.query(`
|
|
70
|
-
CREATE INDEX IF NOT EXISTS idx_${
|
|
57
|
+
CREATE INDEX IF NOT EXISTS idx_${this.#safeSlug}_expires_at
|
|
71
58
|
ON ${tableName} (expires_at)
|
|
72
59
|
WHERE expires_at IS NOT NULL
|
|
73
60
|
`);
|
|
74
61
|
// Index for efficient prefix lookups with LIKE 'prefix%' queries
|
|
75
62
|
await db.query(`
|
|
76
|
-
CREATE INDEX IF NOT EXISTS idx_${
|
|
63
|
+
CREATE INDEX IF NOT EXISTS idx_${this.#safeSlug}_key_prefix
|
|
77
64
|
ON ${tableName} (key text_pattern_ops)
|
|
78
65
|
`);
|
|
79
66
|
this._initialized = true;
|
|
@@ -81,12 +68,14 @@ export class AdapterPostgres extends AdapterAbstract {
|
|
|
81
68
|
}
|
|
82
69
|
/** @inheritdoc */
|
|
83
70
|
async destroy(hard) {
|
|
71
|
+
// stop the timer first so a pending tick cannot fire during DROP
|
|
72
|
+
clearTimeout(this.#cleanupTimer);
|
|
73
|
+
this.#cleanupTimer = undefined;
|
|
84
74
|
if (hard) {
|
|
85
75
|
const { db, tableName } = this.options;
|
|
86
76
|
await db.query(`DROP TABLE IF EXISTS ${tableName}`);
|
|
87
77
|
}
|
|
88
78
|
this._initialized = false;
|
|
89
|
-
clearTimeout(this.#cleanupTimer);
|
|
90
79
|
}
|
|
91
80
|
async #maybeTTLCleanup() {
|
|
92
81
|
clearTimeout(this.#cleanupTimer); // safety
|
|
@@ -95,7 +84,7 @@ export class AdapterPostgres extends AdapterAbstract {
|
|
|
95
84
|
// do the cleanup now
|
|
96
85
|
try {
|
|
97
86
|
await db.query(`
|
|
98
|
-
DELETE FROM ${tableName}
|
|
87
|
+
DELETE FROM ${tableName}
|
|
99
88
|
WHERE expires_at IS NOT NULL AND expires_at <= NOW()`);
|
|
100
89
|
}
|
|
101
90
|
catch (err) {
|
|
@@ -105,44 +94,178 @@ export class AdapterPostgres extends AdapterAbstract {
|
|
|
105
94
|
this.#cleanupTimer = setTimeout(this.#maybeTTLCleanup.bind(this), this.options.ttlCleanupIntervalSec * 1000);
|
|
106
95
|
}
|
|
107
96
|
}
|
|
108
|
-
/**
|
|
97
|
+
/**
|
|
98
|
+
* Convert Redis-style glob to a PostgreSQL LIKE pattern.
|
|
99
|
+
* Literal `%`, `_`, and `\` are escaped with `\`; `*`/`?` become `%`/`_`.
|
|
100
|
+
* LIKE queries using this output must include `ESCAPE '\'`.
|
|
101
|
+
*/
|
|
109
102
|
#likePattern(pattern) {
|
|
110
|
-
return pattern
|
|
103
|
+
return pattern
|
|
104
|
+
.replace(/([\\%_])/g, "\\$1")
|
|
105
|
+
.replace(/\*/g, "%")
|
|
106
|
+
.replace(/\?/g, "_");
|
|
107
|
+
}
|
|
108
|
+
/** Suffix to append to any LIKE clause built from {@link #likePattern}. */
|
|
109
|
+
#likeEscape = ` ESCAPE '\\'`;
|
|
110
|
+
#withNsLike(pattern) {
|
|
111
|
+
// namespace is a literal prefix — its own `%`/`_`/`\` must be escaped too
|
|
112
|
+
const ns = this.namespace.replace(/([\\%_])/g, "\\$1");
|
|
113
|
+
return ns + (pattern === "*" ? "%" : this.#likePattern(pattern));
|
|
111
114
|
}
|
|
112
115
|
/** @inheritdoc */
|
|
113
|
-
|
|
116
|
+
set(key, value, options = {}) {
|
|
117
|
+
return this.#setOn(this.options.db, key, value, options);
|
|
118
|
+
}
|
|
119
|
+
async #setOn(exec, key, value, options = {}) {
|
|
114
120
|
this._assertInitialized();
|
|
115
121
|
key = this._withNs(key);
|
|
116
|
-
const {
|
|
117
|
-
const ttl =
|
|
122
|
+
const { tableName } = this.options;
|
|
123
|
+
const ttl = this._resolveTtl(options);
|
|
118
124
|
const expiresAt = ttl ? new Date(Date.now() + ttl * 1000) : null;
|
|
119
|
-
|
|
120
|
-
await db.query(`INSERT INTO ${tableName} (key, value, expires_at, updated_at)
|
|
125
|
+
await exec.query(`INSERT INTO ${tableName} (key, value, expires_at, updated_at)
|
|
121
126
|
VALUES ($1, $2, $3, NOW())
|
|
122
|
-
ON CONFLICT (key) DO UPDATE SET
|
|
127
|
+
ON CONFLICT (key) DO UPDATE SET
|
|
123
128
|
value = EXCLUDED.value,
|
|
124
129
|
expires_at = EXCLUDED.expires_at,
|
|
125
130
|
updated_at = NOW()`, [key, JSON.stringify(value ?? null), expiresAt]);
|
|
126
131
|
return true;
|
|
127
132
|
}
|
|
128
133
|
/** @inheritdoc */
|
|
129
|
-
async
|
|
134
|
+
async setIfAbsent(key, value, options = {}) {
|
|
130
135
|
this._assertInitialized();
|
|
131
|
-
|
|
136
|
+
const nsKey = this._withNs(key);
|
|
132
137
|
const { db, tableName } = this.options;
|
|
133
|
-
const
|
|
134
|
-
|
|
138
|
+
const ttl = this._resolveTtl(options);
|
|
139
|
+
const expiresAt = ttl ? new Date(Date.now() + ttl * 1000) : null;
|
|
140
|
+
// INSERT on new keys; UPDATE only when the existing row is expired.
|
|
141
|
+
// (Live keys stay untouched — the hallmark of setIfAbsent.)
|
|
142
|
+
const { rowCount } = await db.query(`INSERT INTO ${tableName} (key, value, expires_at, updated_at)
|
|
143
|
+
VALUES ($1, $2, $3, NOW())
|
|
144
|
+
ON CONFLICT (key) DO UPDATE SET
|
|
145
|
+
value = EXCLUDED.value,
|
|
146
|
+
expires_at = EXCLUDED.expires_at,
|
|
147
|
+
updated_at = NOW()
|
|
148
|
+
WHERE ${tableName}.expires_at IS NOT NULL AND ${tableName}.expires_at <= NOW()`, [nsKey, JSON.stringify(value ?? null), expiresAt]);
|
|
149
|
+
return (rowCount ?? 0) > 0;
|
|
150
|
+
}
|
|
151
|
+
/** @inheritdoc */
|
|
152
|
+
async getSet(key, value, options = {}) {
|
|
153
|
+
this._assertInitialized();
|
|
154
|
+
const nsKey = this._withNs(key);
|
|
155
|
+
const { db, tableName } = this.options;
|
|
156
|
+
const ttl = this._resolveTtl(options);
|
|
157
|
+
const expiresAt = ttl ? new Date(Date.now() + ttl * 1000) : null;
|
|
158
|
+
// CTE captures pre-write state; UPSERT writes; RETURNING surfaces the prev.
|
|
159
|
+
const { rows } = await db.query(`WITH prev AS (
|
|
160
|
+
SELECT value FROM ${tableName}
|
|
161
|
+
WHERE key = $1 AND (expires_at IS NULL OR expires_at > NOW())
|
|
162
|
+
), upserted AS (
|
|
163
|
+
INSERT INTO ${tableName} (key, value, expires_at, updated_at)
|
|
164
|
+
VALUES ($1, $2, $3, NOW())
|
|
165
|
+
ON CONFLICT (key) DO UPDATE SET
|
|
166
|
+
value = EXCLUDED.value,
|
|
167
|
+
expires_at = EXCLUDED.expires_at,
|
|
168
|
+
updated_at = NOW()
|
|
169
|
+
RETURNING 1
|
|
170
|
+
)
|
|
171
|
+
SELECT (SELECT value FROM prev) AS prev`, [nsKey, JSON.stringify(value ?? null), expiresAt]);
|
|
172
|
+
return rows[0]?.prev ?? null;
|
|
173
|
+
}
|
|
174
|
+
async #incrBy(key, delta, options = {}) {
|
|
175
|
+
this._assertInitialized();
|
|
176
|
+
const nsKey = this._withNs(key);
|
|
177
|
+
const { db, tableName } = this.options;
|
|
178
|
+
const ttl = this._resolveTtl(options);
|
|
179
|
+
const expiresAt = ttl ? new Date(Date.now() + ttl * 1000) : null;
|
|
180
|
+
// - New key OR expired key: value = delta, expires_at = $ttl
|
|
181
|
+
// - Live key: value = old + delta, preserve existing expires_at
|
|
182
|
+
// The nested cast `value::text::numeric` throws 22P02 when the stored
|
|
183
|
+
// value is not a JSON number — we translate that to TypeError.
|
|
184
|
+
try {
|
|
185
|
+
const { rows } = await db.query(`INSERT INTO ${tableName} (key, value, expires_at, updated_at)
|
|
186
|
+
VALUES ($1, to_jsonb($2::numeric), $3, NOW())
|
|
187
|
+
ON CONFLICT (key) DO UPDATE SET
|
|
188
|
+
value = CASE
|
|
189
|
+
WHEN ${tableName}.expires_at IS NOT NULL AND ${tableName}.expires_at <= NOW()
|
|
190
|
+
THEN to_jsonb($2::numeric)
|
|
191
|
+
ELSE to_jsonb(${tableName}.value::text::numeric + $2::numeric)
|
|
192
|
+
END,
|
|
193
|
+
expires_at = CASE
|
|
194
|
+
WHEN ${tableName}.expires_at IS NOT NULL AND ${tableName}.expires_at <= NOW()
|
|
195
|
+
THEN EXCLUDED.expires_at
|
|
196
|
+
ELSE ${tableName}.expires_at
|
|
197
|
+
END,
|
|
198
|
+
updated_at = NOW()
|
|
199
|
+
RETURNING value`, [nsKey, delta, expiresAt]);
|
|
200
|
+
return Number(rows[0].value);
|
|
201
|
+
}
|
|
202
|
+
catch (err) {
|
|
203
|
+
if (err?.code === "22P02" ||
|
|
204
|
+
/invalid input syntax for type numeric/i.test(err?.message ?? "")) {
|
|
205
|
+
throw new TypeError("KV value is not a number");
|
|
206
|
+
}
|
|
207
|
+
throw err;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
/** @inheritdoc */
|
|
211
|
+
incr(key, by = 1, options = {}) {
|
|
212
|
+
return this.#incrBy(key, by, options);
|
|
213
|
+
}
|
|
214
|
+
/** @inheritdoc */
|
|
215
|
+
decr(key, by = 1, options = {}) {
|
|
216
|
+
return this.#incrBy(key, -by, options);
|
|
217
|
+
}
|
|
218
|
+
/** @inheritdoc */
|
|
219
|
+
async cas(key, expected, next, options = {}) {
|
|
220
|
+
this._assertInitialized();
|
|
221
|
+
const nsKey = this._withNs(key);
|
|
222
|
+
const { db, tableName } = this.options;
|
|
223
|
+
const hasTtl = options.ttl !== undefined;
|
|
224
|
+
const ttl = hasTtl ? this._resolveTtl(options) : undefined;
|
|
225
|
+
const expiresAt = ttl ? new Date(Date.now() + ttl * 1000) : null;
|
|
226
|
+
const expectedJson = JSON.stringify(expected ?? null);
|
|
227
|
+
const nextJson = JSON.stringify(next ?? null);
|
|
228
|
+
const sql = hasTtl
|
|
229
|
+
? `UPDATE ${tableName}
|
|
230
|
+
SET value = $2::jsonb, expires_at = $4, updated_at = NOW()
|
|
231
|
+
WHERE key = $1
|
|
232
|
+
AND value = $3::jsonb
|
|
233
|
+
AND (expires_at IS NULL OR expires_at > NOW())`
|
|
234
|
+
: `UPDATE ${tableName}
|
|
235
|
+
SET value = $2::jsonb, updated_at = NOW()
|
|
236
|
+
WHERE key = $1
|
|
237
|
+
AND value = $3::jsonb
|
|
238
|
+
AND (expires_at IS NULL OR expires_at > NOW())`;
|
|
239
|
+
const params = [nsKey, nextJson, expectedJson];
|
|
240
|
+
if (hasTtl)
|
|
241
|
+
params.push(expiresAt);
|
|
242
|
+
const { rowCount } = await db.query(sql, params);
|
|
243
|
+
return (rowCount ?? 0) > 0;
|
|
244
|
+
}
|
|
245
|
+
/** @inheritdoc */
|
|
246
|
+
get(key) {
|
|
247
|
+
return this.#getOn(this.options.db, key);
|
|
248
|
+
}
|
|
249
|
+
async #getOn(exec, key) {
|
|
250
|
+
this._assertInitialized();
|
|
251
|
+
key = this._withNs(key);
|
|
252
|
+
const { tableName } = this.options;
|
|
253
|
+
const { rows } = await exec.query(`SELECT value, expires_at
|
|
254
|
+
FROM ${tableName}
|
|
135
255
|
WHERE key = $1 AND (expires_at IS NULL OR expires_at > NOW())`, [key]);
|
|
136
256
|
if (rows.length === 0)
|
|
137
257
|
return null;
|
|
138
258
|
return rows[0].value;
|
|
139
259
|
}
|
|
140
260
|
/** @inheritdoc */
|
|
141
|
-
|
|
261
|
+
delete(key) {
|
|
262
|
+
return this.#deleteOn(this.options.db, key);
|
|
263
|
+
}
|
|
264
|
+
async #deleteOn(exec, key) {
|
|
142
265
|
this._assertInitialized();
|
|
143
266
|
key = this._withNs(key);
|
|
144
|
-
const {
|
|
145
|
-
const { rowCount } = await
|
|
267
|
+
const { tableName } = this.options;
|
|
268
|
+
const { rowCount } = await exec.query(`DELETE FROM ${tableName} WHERE key = $1`, [key]);
|
|
146
269
|
return rowCount > 0;
|
|
147
270
|
}
|
|
148
271
|
/** @inheritdoc */
|
|
@@ -150,7 +273,7 @@ export class AdapterPostgres extends AdapterAbstract {
|
|
|
150
273
|
this._assertInitialized();
|
|
151
274
|
key = this._withNs(key);
|
|
152
275
|
const { db, tableName } = this.options;
|
|
153
|
-
const { rows } = await db.query(`SELECT 1 FROM ${tableName}
|
|
276
|
+
const { rows } = await db.query(`SELECT 1 FROM ${tableName}
|
|
154
277
|
WHERE key = $1 AND (expires_at IS NULL OR expires_at > NOW())`, [key]);
|
|
155
278
|
return rows.length > 0;
|
|
156
279
|
}
|
|
@@ -158,43 +281,73 @@ export class AdapterPostgres extends AdapterAbstract {
|
|
|
158
281
|
async keys(pattern) {
|
|
159
282
|
this._assertInitialized();
|
|
160
283
|
const { db, tableName } = this.options;
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
SELECT key FROM ${tableName}
|
|
164
|
-
WHERE key LIKE $1
|
|
284
|
+
const { rows } = await db.query(`SELECT key FROM ${tableName}
|
|
285
|
+
WHERE key LIKE $1${this.#likeEscape}
|
|
165
286
|
AND (expires_at IS NULL OR expires_at > NOW())
|
|
166
|
-
ORDER BY key
|
|
167
|
-
|
|
168
|
-
|
|
287
|
+
ORDER BY key`, [this.#withNsLike(pattern)]);
|
|
288
|
+
return rows.map((row) => this._withoutNs(row.key)).toSorted();
|
|
289
|
+
}
|
|
290
|
+
/** @inheritdoc */
|
|
291
|
+
async *keysIter(pattern) {
|
|
292
|
+
this._assertInitialized();
|
|
293
|
+
const { tableName } = this.options;
|
|
294
|
+
const { client, release } = await this.#acquireClient();
|
|
295
|
+
// Use a per-call cursor name so multiple iterators on the same PG
|
|
296
|
+
// client can't collide. `toString(36)` only emits [0-9a-z].
|
|
297
|
+
const cursorName = `kv_iter_${Math.random().toString(36).slice(2)}`;
|
|
298
|
+
let inTxn = false;
|
|
299
|
+
try {
|
|
300
|
+
await client.query("BEGIN");
|
|
301
|
+
inTxn = true;
|
|
302
|
+
await client.query(`DECLARE ${cursorName} NO SCROLL CURSOR FOR
|
|
303
|
+
SELECT key FROM ${tableName}
|
|
304
|
+
WHERE key LIKE $1${this.#likeEscape}
|
|
305
|
+
AND (expires_at IS NULL OR expires_at > NOW())
|
|
306
|
+
ORDER BY key`, [this.#withNsLike(pattern)]);
|
|
307
|
+
while (true) {
|
|
308
|
+
const { rows } = await client.query(`FETCH 500 FROM ${cursorName}`);
|
|
309
|
+
if (rows.length === 0)
|
|
310
|
+
break;
|
|
311
|
+
for (const row of rows) {
|
|
312
|
+
yield this._withoutNs(row.key);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
169
315
|
}
|
|
170
|
-
|
|
171
|
-
|
|
316
|
+
finally {
|
|
317
|
+
if (inTxn) {
|
|
318
|
+
try {
|
|
319
|
+
await client.query(`CLOSE ${cursorName}`);
|
|
320
|
+
}
|
|
321
|
+
catch {
|
|
322
|
+
// cursor may already be gone if the txn aborted
|
|
323
|
+
}
|
|
324
|
+
try {
|
|
325
|
+
await client.query("COMMIT");
|
|
326
|
+
}
|
|
327
|
+
catch {
|
|
328
|
+
try {
|
|
329
|
+
await client.query("ROLLBACK");
|
|
330
|
+
}
|
|
331
|
+
catch {
|
|
332
|
+
// ignore — caller will see the original error
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
release();
|
|
172
337
|
}
|
|
173
|
-
const { rows } = await db.query(query, params);
|
|
174
|
-
return rows
|
|
175
|
-
.map((row) => {
|
|
176
|
-
let k = row.key;
|
|
177
|
-
// strip namespace if exists
|
|
178
|
-
if (this.namespace)
|
|
179
|
-
k = k.slice(this.namespace.length);
|
|
180
|
-
return k;
|
|
181
|
-
})
|
|
182
|
-
.toSorted();
|
|
183
338
|
}
|
|
184
339
|
/** @inheritdoc */
|
|
185
340
|
async clear(pattern) {
|
|
186
341
|
this._assertInitialized();
|
|
187
342
|
const { db, tableName } = this.options;
|
|
188
|
-
const likePattern = pattern
|
|
189
|
-
? this.namespace + "%"
|
|
190
|
-
: this.namespace + this.#likePattern(pattern);
|
|
343
|
+
const likePattern = this.#withNsLike(pattern);
|
|
191
344
|
// Batch large deletions to prevent long-running transactions
|
|
192
345
|
const batchSize = 10000;
|
|
193
346
|
let totalDeleted = 0;
|
|
194
347
|
let deleted;
|
|
195
348
|
do {
|
|
196
349
|
const { rowCount } = await db.query(`DELETE FROM ${tableName} WHERE key IN (
|
|
197
|
-
SELECT key FROM ${tableName} WHERE key LIKE $1 LIMIT $2
|
|
350
|
+
SELECT key FROM ${tableName} WHERE key LIKE $1${this.#likeEscape} LIMIT $2
|
|
198
351
|
)`, [likePattern, batchSize]);
|
|
199
352
|
deleted = rowCount ?? 0;
|
|
200
353
|
totalDeleted += deleted;
|
|
@@ -202,27 +355,31 @@ export class AdapterPostgres extends AdapterAbstract {
|
|
|
202
355
|
return totalDeleted;
|
|
203
356
|
}
|
|
204
357
|
/** @inheritdoc */
|
|
205
|
-
async setMultiple(
|
|
358
|
+
async setMultiple(entries, options = {}) {
|
|
206
359
|
this._assertInitialized();
|
|
360
|
+
if (entries.length === 0)
|
|
361
|
+
return [];
|
|
362
|
+
const normalized = this._normalizePairs(entries);
|
|
207
363
|
const { db, tableName } = this.options;
|
|
208
|
-
const
|
|
209
|
-
const
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
.
|
|
213
|
-
|
|
214
|
-
|
|
364
|
+
const placeholders = [];
|
|
365
|
+
const params = [];
|
|
366
|
+
let idx = 1;
|
|
367
|
+
for (const { key, value, ttl: pairTtl } of normalized) {
|
|
368
|
+
const ttl = this._resolveTtl({ ttl: pairTtl ?? options.ttl });
|
|
369
|
+
const expiresAt = ttl ? new Date(Date.now() + ttl * 1000) : null;
|
|
370
|
+
placeholders.push(`($${idx++}, $${idx++}, $${idx++})`);
|
|
371
|
+
params.push(this._withNs(key), JSON.stringify(value ?? null), expiresAt);
|
|
372
|
+
}
|
|
215
373
|
const sql = `
|
|
216
374
|
INSERT INTO ${tableName} (key, value, expires_at)
|
|
217
|
-
VALUES ${
|
|
218
|
-
ON CONFLICT (key) DO UPDATE SET
|
|
375
|
+
VALUES ${placeholders.join(", ")}
|
|
376
|
+
ON CONFLICT (key) DO UPDATE SET
|
|
219
377
|
value = EXCLUDED.value,
|
|
220
378
|
expires_at = EXCLUDED.expires_at,
|
|
221
379
|
updated_at = NOW()
|
|
222
380
|
`;
|
|
223
|
-
// console.log(sql, params);
|
|
224
381
|
await db.query(sql, params);
|
|
225
|
-
return
|
|
382
|
+
return normalized.map(() => true);
|
|
226
383
|
}
|
|
227
384
|
/** @inheritdoc */
|
|
228
385
|
async getMultiple(keys) {
|
|
@@ -232,23 +389,21 @@ export class AdapterPostgres extends AdapterAbstract {
|
|
|
232
389
|
const placeholders = keys.map((_, i) => `$${i + 1}`).join(", ");
|
|
233
390
|
const { db, tableName } = this.options;
|
|
234
391
|
const sql = `
|
|
235
|
-
SELECT key, value
|
|
236
|
-
FROM ${tableName}
|
|
392
|
+
SELECT key, value
|
|
393
|
+
FROM ${tableName}
|
|
237
394
|
WHERE key IN (${placeholders}) AND (expires_at IS NULL OR expires_at > NOW())
|
|
238
395
|
`;
|
|
239
396
|
const params = keys.map((k) => this._withNs(k));
|
|
240
|
-
// console.log(sql, params);
|
|
241
397
|
const { rows } = await db.query(sql, params);
|
|
242
|
-
const found =
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
// ensure all requested keys are in the result
|
|
398
|
+
const found = Object.create(null);
|
|
399
|
+
for (const row of rows) {
|
|
400
|
+
found[this._withoutNs(row.key)] = row.value;
|
|
401
|
+
}
|
|
402
|
+
// preserve explicit null / false / 0 / "" — missing keys become null
|
|
248
403
|
const resultMap = {};
|
|
249
|
-
|
|
250
|
-
resultMap[key] = found[key]
|
|
251
|
-
}
|
|
404
|
+
for (const key of keys) {
|
|
405
|
+
resultMap[key] = key in found ? found[key] : null;
|
|
406
|
+
}
|
|
252
407
|
return resultMap;
|
|
253
408
|
}
|
|
254
409
|
/** @inheritdoc */
|
|
@@ -257,7 +412,7 @@ export class AdapterPostgres extends AdapterAbstract {
|
|
|
257
412
|
key = this._withNs(key);
|
|
258
413
|
const { db, tableName } = this.options;
|
|
259
414
|
const expiresAt = new Date(Date.now() + ttl * 1000);
|
|
260
|
-
const { rowCount } = await db.query(`UPDATE ${tableName}
|
|
415
|
+
const { rowCount } = await db.query(`UPDATE ${tableName}
|
|
261
416
|
SET expires_at = $2, updated_at = NOW()
|
|
262
417
|
WHERE key = $1 AND (expires_at IS NULL OR expires_at > NOW())`, [key, expiresAt]);
|
|
263
418
|
return rowCount > 0;
|
|
@@ -268,53 +423,75 @@ export class AdapterPostgres extends AdapterAbstract {
|
|
|
268
423
|
key = this._withNs(key);
|
|
269
424
|
const { db, tableName } = this.options;
|
|
270
425
|
const { rows } = await db.query(`SELECT expires_at FROM ${tableName} WHERE key = $1`, [key]);
|
|
271
|
-
// Key doesn't exist
|
|
272
426
|
if (rows.length === 0)
|
|
273
|
-
return
|
|
427
|
+
return { state: "missing" };
|
|
274
428
|
const expiresAt = rows[0].expires_at;
|
|
275
|
-
// No expiration set
|
|
276
429
|
if (!expiresAt)
|
|
277
|
-
return
|
|
278
|
-
|
|
430
|
+
return { state: "no-ttl" };
|
|
431
|
+
// expired-but-not-yet-cleaned counts as missing (matches get() filter)
|
|
432
|
+
const at = new Date(expiresAt);
|
|
433
|
+
if (at.valueOf() <= Date.now())
|
|
434
|
+
return { state: "missing" };
|
|
435
|
+
return { state: "expires", at };
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* Acquire a single pinned connection for a transaction.
|
|
439
|
+
*/
|
|
440
|
+
async #acquireClient() {
|
|
441
|
+
const { db } = this.options;
|
|
442
|
+
if (typeof db.totalCount === "number" && typeof db.connect === "function") {
|
|
443
|
+
const client = await db.connect();
|
|
444
|
+
return { client, release: () => client.release() };
|
|
445
|
+
}
|
|
446
|
+
return { client: db, release: () => { } };
|
|
279
447
|
}
|
|
280
448
|
/** @inheritdoc */
|
|
281
449
|
async transaction(operations) {
|
|
282
450
|
this._assertInitialized();
|
|
283
|
-
const {
|
|
451
|
+
const { logger } = this.options;
|
|
452
|
+
const { client, release } = await this.#acquireClient();
|
|
284
453
|
const results = [];
|
|
285
|
-
await db.query("BEGIN");
|
|
286
454
|
try {
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
455
|
+
await client.query("BEGIN");
|
|
456
|
+
try {
|
|
457
|
+
for (const op of operations) {
|
|
458
|
+
switch (op.type) {
|
|
459
|
+
case "set":
|
|
460
|
+
results.push(await this.#setOn(client, op.key, op.value, op.options || {}));
|
|
461
|
+
break;
|
|
462
|
+
case "get":
|
|
463
|
+
results.push(await this.#getOn(client, op.key));
|
|
464
|
+
break;
|
|
465
|
+
case "delete":
|
|
466
|
+
results.push(await this.#deleteOn(client, op.key));
|
|
467
|
+
break;
|
|
468
|
+
}
|
|
298
469
|
}
|
|
470
|
+
await client.query("COMMIT");
|
|
471
|
+
}
|
|
472
|
+
catch (err) {
|
|
473
|
+
try {
|
|
474
|
+
await client.query("ROLLBACK");
|
|
475
|
+
}
|
|
476
|
+
catch (rollbackErr) {
|
|
477
|
+
logger?.error?.(`Rollback failed: ${rollbackErr}`);
|
|
478
|
+
}
|
|
479
|
+
throw err;
|
|
299
480
|
}
|
|
300
|
-
await db.query("COMMIT");
|
|
301
481
|
}
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
logger?.error?.(err);
|
|
305
|
-
throw err;
|
|
482
|
+
finally {
|
|
483
|
+
release();
|
|
306
484
|
}
|
|
307
485
|
return results;
|
|
308
486
|
}
|
|
309
487
|
async __debug_dump() {
|
|
310
488
|
this._assertInitialized();
|
|
311
489
|
const { db, tableName } = this.options;
|
|
312
|
-
const { rows } = await db.query(`SELECT key, value, expires_at FROM ${tableName} WHERE key LIKE $1`, [this
|
|
313
|
-
// console.log(12312, rows);
|
|
490
|
+
const { rows } = await db.query(`SELECT key, value, expires_at FROM ${tableName} WHERE key LIKE $1${this.#likeEscape}`, [this.#withNsLike("*")]);
|
|
314
491
|
return rows.reduce((m, row) => {
|
|
315
492
|
m[this._withoutNs(row.key)] = {
|
|
316
493
|
value: row.value,
|
|
317
|
-
ttl: row.expires_at,
|
|
494
|
+
ttl: row.expires_at ? new Date(row.expires_at) : null,
|
|
318
495
|
};
|
|
319
496
|
return m;
|
|
320
497
|
}, {});
|
package/dist/adapter/redis.d.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* Redis key-value storage adapter implementation.
|
|
5
5
|
*/
|
|
6
6
|
import type { createClient, createClientPool } from "redis";
|
|
7
|
-
import { AdapterAbstract, type AdapterAbstractOptions, type Operation, type SetOptions } from "./abstract.js";
|
|
7
|
+
import { AdapterAbstract, type AdapterAbstractOptions, type Operation, type SetMultipleEntry, type SetOptions, type TtlResult } from "./abstract.js";
|
|
8
8
|
/**
|
|
9
9
|
* Configuration options for the Redis KV adapter.
|
|
10
10
|
*/
|
|
@@ -46,6 +46,7 @@ export interface AdapterRedisOptions extends AdapterAbstractOptions {
|
|
|
46
46
|
* ```
|
|
47
47
|
*/
|
|
48
48
|
export declare class AdapterRedis extends AdapterAbstract {
|
|
49
|
+
#private;
|
|
49
50
|
readonly namespace: string;
|
|
50
51
|
_type: string;
|
|
51
52
|
readonly options: AdapterRedisOptions;
|
|
@@ -57,6 +58,16 @@ export declare class AdapterRedis extends AdapterAbstract {
|
|
|
57
58
|
/** @inheritdoc */
|
|
58
59
|
set(key: string, value: any, options?: Partial<SetOptions>): Promise<boolean>;
|
|
59
60
|
/** @inheritdoc */
|
|
61
|
+
setIfAbsent(key: string, value: any, options?: Partial<SetOptions>): Promise<boolean>;
|
|
62
|
+
/** @inheritdoc */
|
|
63
|
+
getSet(key: string, value: any, options?: Partial<SetOptions>): Promise<any>;
|
|
64
|
+
/** @inheritdoc */
|
|
65
|
+
incr(key: string, by?: number, options?: Partial<SetOptions>): Promise<number>;
|
|
66
|
+
/** @inheritdoc */
|
|
67
|
+
decr(key: string, by?: number, options?: Partial<SetOptions>): Promise<number>;
|
|
68
|
+
/** @inheritdoc */
|
|
69
|
+
cas(key: string, expected: any, next: any, options?: Partial<SetOptions>): Promise<boolean>;
|
|
70
|
+
/** @inheritdoc */
|
|
60
71
|
get(key: string): Promise<any>;
|
|
61
72
|
/** @inheritdoc */
|
|
62
73
|
delete(key: string): Promise<boolean>;
|
|
@@ -65,19 +76,21 @@ export declare class AdapterRedis extends AdapterAbstract {
|
|
|
65
76
|
/** @inheritdoc */
|
|
66
77
|
keys(pattern: string): Promise<string[]>;
|
|
67
78
|
/** @inheritdoc */
|
|
79
|
+
keysIter(pattern: string): AsyncIterable<string>;
|
|
80
|
+
/** @inheritdoc */
|
|
68
81
|
clear(pattern: string): Promise<number>;
|
|
69
82
|
/** @inheritdoc */
|
|
70
|
-
setMultiple(
|
|
83
|
+
setMultiple(entries: readonly SetMultipleEntry[], options?: Partial<SetOptions>): Promise<boolean[]>;
|
|
71
84
|
/** @inheritdoc */
|
|
72
85
|
getMultiple(keys: string[]): Promise<Record<string, any>>;
|
|
73
86
|
/** @inheritdoc */
|
|
74
87
|
expire(key: string, ttl: number): Promise<boolean>;
|
|
75
88
|
/** @inheritdoc */
|
|
76
|
-
ttl(key: string): Promise<
|
|
89
|
+
ttl(key: string): Promise<TtlResult>;
|
|
77
90
|
/** @inheritdoc */
|
|
78
91
|
transaction(operations: Operation[]): Promise<any[]>;
|
|
79
92
|
__debug_dump(): Promise<Record<string, {
|
|
80
93
|
value: any;
|
|
81
|
-
ttl: Date | null
|
|
94
|
+
ttl: Date | null;
|
|
82
95
|
}>>;
|
|
83
96
|
}
|