@postgresx/noredis 0.1.0 → 0.6.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/README.md +625 -25
- package/dist/adapters/ioredis.d.ts +130 -0
- package/dist/adapters/ioredis.js +141 -0
- package/dist/adapters/node.d.ts +1 -1
- package/dist/adapters/redis.d.ts +143 -0
- package/dist/adapters/redis.js +118 -0
- package/dist/adapters/web.d.ts +30 -0
- package/dist/adapters/web.js +64 -0
- package/dist/chunk-3DNTNHVD.js +295 -0
- package/dist/chunk-5ZGPCW5N.js +288 -0
- package/dist/chunk-HW3WONAC.js +239 -0
- package/dist/chunk-QI73S2FT.js +101 -0
- package/dist/chunk-RWRM5RVO.js +30 -0
- package/dist/{chunk-GBFG2TOL.js → chunk-VLFUE33D.js} +264 -16
- package/dist/chunk-WRMEEA6T.js +246 -0
- package/dist/chunk-YSWHEFPX.js +328 -0
- package/dist/client-DP3ssToT.d.ts +509 -0
- package/dist/hash-index.d.ts +43 -0
- package/dist/hash-index.js +9 -0
- package/dist/index.d.ts +16 -403
- package/dist/index.js +448 -988
- package/dist/kv-cache.d.ts +38 -2
- package/dist/kv-cache.js +1 -1
- package/dist/kv.d.ts +1 -0
- package/dist/kv.js +8 -0
- package/dist/list-index.d.ts +53 -0
- package/dist/list-index.js +9 -0
- package/dist/{pubsub-CHGs5dXU.d.ts → pubsub-index.d.ts} +1 -1
- package/dist/pubsub-index.js +12 -0
- package/dist/set-index.d.ts +42 -0
- package/dist/set-index.js +9 -0
- package/dist/sorted-set-index.d.ts +58 -0
- package/dist/sorted-set-index.js +9 -0
- package/package.json +54 -4
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
import {
|
|
2
|
+
indexName,
|
|
3
|
+
quoteQualifiedName
|
|
4
|
+
} from "./chunk-RWRM5RVO.js";
|
|
5
|
+
|
|
6
|
+
// src/set.ts
|
|
7
|
+
var DEFAULT_TABLE_NAME = "pgredis_set";
|
|
8
|
+
var DEFAULT_NAMESPACE = "default";
|
|
9
|
+
var PgSet = class {
|
|
10
|
+
namespace;
|
|
11
|
+
tableName;
|
|
12
|
+
quotedTableName;
|
|
13
|
+
sql;
|
|
14
|
+
constructor(options) {
|
|
15
|
+
this.sql = options.sql;
|
|
16
|
+
this.namespace = options.namespace || DEFAULT_NAMESPACE;
|
|
17
|
+
this.tableName = options.tableName || DEFAULT_TABLE_NAME;
|
|
18
|
+
this.quotedTableName = quoteQualifiedName(this.tableName);
|
|
19
|
+
}
|
|
20
|
+
async ensureSchema(options = {}) {
|
|
21
|
+
const persistence = options.unlogged === false ? "" : "UNLOGGED ";
|
|
22
|
+
await this.sql.unsafe(`
|
|
23
|
+
CREATE ${persistence}TABLE IF NOT EXISTS ${this.quotedTableName} (
|
|
24
|
+
namespace TEXT NOT NULL,
|
|
25
|
+
key TEXT NOT NULL,
|
|
26
|
+
member TEXT NOT NULL,
|
|
27
|
+
expires_at TIMESTAMPTZ,
|
|
28
|
+
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
29
|
+
PRIMARY KEY (namespace, key, member)
|
|
30
|
+
)
|
|
31
|
+
`);
|
|
32
|
+
await this.sql.unsafe(`
|
|
33
|
+
ALTER TABLE ${this.quotedTableName}
|
|
34
|
+
ADD COLUMN IF NOT EXISTS expires_at TIMESTAMPTZ
|
|
35
|
+
`);
|
|
36
|
+
await this.sql.unsafe(`
|
|
37
|
+
CREATE INDEX IF NOT EXISTS ${indexName(this.tableName, "namespace_key")}
|
|
38
|
+
ON ${this.quotedTableName} (namespace, key)
|
|
39
|
+
`);
|
|
40
|
+
await this.sql.unsafe(`
|
|
41
|
+
CREATE INDEX IF NOT EXISTS ${indexName(this.tableName, "expires_at")}
|
|
42
|
+
ON ${this.quotedTableName} (expires_at)
|
|
43
|
+
WHERE expires_at IS NOT NULL
|
|
44
|
+
`);
|
|
45
|
+
}
|
|
46
|
+
async sadd(key, ...members) {
|
|
47
|
+
const values = [...new Set(members)];
|
|
48
|
+
if (values.length === 0) return 0;
|
|
49
|
+
const params = [];
|
|
50
|
+
const groups = values.map((member, index) => {
|
|
51
|
+
const base = index * 3;
|
|
52
|
+
params.push(this.namespace, key, member);
|
|
53
|
+
return `($${base + 1}, $${base + 2}, $${base + 3}, (SELECT MAX(expires_at) FROM ${this.quotedTableName} WHERE namespace = $${base + 1} AND key = $${base + 2}), NOW())`;
|
|
54
|
+
}).join(", ");
|
|
55
|
+
const rows = await this.sql.unsafe(
|
|
56
|
+
`INSERT INTO ${this.quotedTableName} (namespace, key, member, expires_at, updated_at)
|
|
57
|
+
VALUES ${groups}
|
|
58
|
+
ON CONFLICT (namespace, key, member) DO NOTHING
|
|
59
|
+
RETURNING member`,
|
|
60
|
+
params
|
|
61
|
+
);
|
|
62
|
+
return rows.length;
|
|
63
|
+
}
|
|
64
|
+
async srem(key, ...members) {
|
|
65
|
+
const values = [...new Set(members)];
|
|
66
|
+
if (values.length === 0) return 0;
|
|
67
|
+
const placeholders = values.map((_, index) => `$${index + 3}`).join(", ");
|
|
68
|
+
const rows = await this.sql.unsafe(
|
|
69
|
+
`DELETE FROM ${this.quotedTableName}
|
|
70
|
+
WHERE namespace = $1 AND key = $2 AND member IN (${placeholders})
|
|
71
|
+
RETURNING member`,
|
|
72
|
+
[this.namespace, key, ...values]
|
|
73
|
+
);
|
|
74
|
+
return rows.length;
|
|
75
|
+
}
|
|
76
|
+
async sismember(key, member) {
|
|
77
|
+
const rows = await this.sql.unsafe(
|
|
78
|
+
`SELECT EXISTS (
|
|
79
|
+
SELECT 1 FROM ${this.quotedTableName}
|
|
80
|
+
WHERE namespace = $1 AND key = $2 AND member = $3
|
|
81
|
+
AND (expires_at IS NULL OR expires_at > NOW())
|
|
82
|
+
) AS exists`,
|
|
83
|
+
[this.namespace, key, member]
|
|
84
|
+
);
|
|
85
|
+
return !!rows[0]?.exists;
|
|
86
|
+
}
|
|
87
|
+
async smembers(key) {
|
|
88
|
+
const rows = await this.sql.unsafe(
|
|
89
|
+
`SELECT member
|
|
90
|
+
FROM ${this.quotedTableName}
|
|
91
|
+
WHERE namespace = $1 AND key = $2
|
|
92
|
+
AND (expires_at IS NULL OR expires_at > NOW())
|
|
93
|
+
ORDER BY member ASC`,
|
|
94
|
+
[this.namespace, key]
|
|
95
|
+
);
|
|
96
|
+
return rows.map((row) => row.member);
|
|
97
|
+
}
|
|
98
|
+
async sinter(...keys) {
|
|
99
|
+
const values = [...new Set(keys)];
|
|
100
|
+
if (values.length === 0) return [];
|
|
101
|
+
const rows = await this.sql.unsafe(
|
|
102
|
+
`SELECT member
|
|
103
|
+
FROM ${this.quotedTableName}
|
|
104
|
+
WHERE namespace = $1 AND key = ANY($2::text[])
|
|
105
|
+
AND (expires_at IS NULL OR expires_at > NOW())
|
|
106
|
+
GROUP BY member
|
|
107
|
+
HAVING COUNT(DISTINCT key) = $3
|
|
108
|
+
ORDER BY member ASC`,
|
|
109
|
+
[this.namespace, values, values.length]
|
|
110
|
+
);
|
|
111
|
+
return rows.map((row) => row.member);
|
|
112
|
+
}
|
|
113
|
+
async sunion(...keys) {
|
|
114
|
+
const values = [...new Set(keys)];
|
|
115
|
+
if (values.length === 0) return [];
|
|
116
|
+
const rows = await this.sql.unsafe(
|
|
117
|
+
`SELECT DISTINCT member
|
|
118
|
+
FROM ${this.quotedTableName}
|
|
119
|
+
WHERE namespace = $1 AND key = ANY($2::text[])
|
|
120
|
+
AND (expires_at IS NULL OR expires_at > NOW())
|
|
121
|
+
ORDER BY member ASC`,
|
|
122
|
+
[this.namespace, values]
|
|
123
|
+
);
|
|
124
|
+
return rows.map((row) => row.member);
|
|
125
|
+
}
|
|
126
|
+
async sdiff(key, ...otherKeys) {
|
|
127
|
+
const values = [...new Set(otherKeys)];
|
|
128
|
+
const rows = await this.sql.unsafe(
|
|
129
|
+
`SELECT member
|
|
130
|
+
FROM ${this.quotedTableName}
|
|
131
|
+
WHERE namespace = $1
|
|
132
|
+
AND key = $2
|
|
133
|
+
AND (expires_at IS NULL OR expires_at > NOW())
|
|
134
|
+
AND (
|
|
135
|
+
$3::text[] = ARRAY[]::text[]
|
|
136
|
+
OR member NOT IN (
|
|
137
|
+
SELECT member
|
|
138
|
+
FROM ${this.quotedTableName}
|
|
139
|
+
WHERE namespace = $1 AND key = ANY($3::text[])
|
|
140
|
+
AND (expires_at IS NULL OR expires_at > NOW())
|
|
141
|
+
)
|
|
142
|
+
)
|
|
143
|
+
ORDER BY member ASC`,
|
|
144
|
+
[this.namespace, key, values]
|
|
145
|
+
);
|
|
146
|
+
return rows.map((row) => row.member);
|
|
147
|
+
}
|
|
148
|
+
async scard(key) {
|
|
149
|
+
const rows = await this.sql.unsafe(
|
|
150
|
+
`SELECT COUNT(*)::bigint AS count
|
|
151
|
+
FROM ${this.quotedTableName}
|
|
152
|
+
WHERE namespace = $1 AND key = $2
|
|
153
|
+
AND (expires_at IS NULL OR expires_at > NOW())`,
|
|
154
|
+
[this.namespace, key]
|
|
155
|
+
);
|
|
156
|
+
return Number(rows[0]?.count ?? 0);
|
|
157
|
+
}
|
|
158
|
+
async sclear(key) {
|
|
159
|
+
const rows = await this.sql.unsafe(
|
|
160
|
+
`DELETE FROM ${this.quotedTableName}
|
|
161
|
+
WHERE namespace = $1 AND key = $2
|
|
162
|
+
RETURNING member`,
|
|
163
|
+
[this.namespace, key]
|
|
164
|
+
);
|
|
165
|
+
return rows.length;
|
|
166
|
+
}
|
|
167
|
+
async sscan(key, cursor = null, count = 100) {
|
|
168
|
+
const limit = Math.max(1, Math.floor(count));
|
|
169
|
+
const rows = await this.sql.unsafe(
|
|
170
|
+
`SELECT member
|
|
171
|
+
FROM ${this.quotedTableName}
|
|
172
|
+
WHERE namespace = $1
|
|
173
|
+
AND key = $2
|
|
174
|
+
AND member > COALESCE($3::text, '')
|
|
175
|
+
AND (expires_at IS NULL OR expires_at > NOW())
|
|
176
|
+
ORDER BY member ASC
|
|
177
|
+
LIMIT $4`,
|
|
178
|
+
[this.namespace, key, cursor, limit]
|
|
179
|
+
);
|
|
180
|
+
return {
|
|
181
|
+
cursor: rows.length === limit ? rows[rows.length - 1].member : null,
|
|
182
|
+
members: rows.map((row) => row.member)
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
async expire(key, ttlMs) {
|
|
186
|
+
const rows = await this.sql.unsafe(
|
|
187
|
+
`UPDATE ${this.quotedTableName}
|
|
188
|
+
SET expires_at = NOW() + ($3::bigint * INTERVAL '1 millisecond'),
|
|
189
|
+
updated_at = NOW()
|
|
190
|
+
WHERE namespace = $1 AND key = $2
|
|
191
|
+
RETURNING member`,
|
|
192
|
+
[this.namespace, key, Math.max(0, Math.floor(ttlMs))]
|
|
193
|
+
);
|
|
194
|
+
return rows.length;
|
|
195
|
+
}
|
|
196
|
+
async persist(key) {
|
|
197
|
+
const rows = await this.sql.unsafe(
|
|
198
|
+
`UPDATE ${this.quotedTableName}
|
|
199
|
+
SET expires_at = NULL,
|
|
200
|
+
updated_at = NOW()
|
|
201
|
+
WHERE namespace = $1 AND key = $2
|
|
202
|
+
RETURNING member`,
|
|
203
|
+
[this.namespace, key]
|
|
204
|
+
);
|
|
205
|
+
return rows.length;
|
|
206
|
+
}
|
|
207
|
+
async ttl(key) {
|
|
208
|
+
const rows = await this.sql.unsafe(
|
|
209
|
+
`SELECT CASE
|
|
210
|
+
WHEN MAX(expires_at) IS NULL THEN NULL
|
|
211
|
+
ELSE GREATEST(0, FLOOR(EXTRACT(EPOCH FROM (MAX(expires_at) - NOW())) * 1000))::bigint
|
|
212
|
+
END AS ttl_ms
|
|
213
|
+
FROM ${this.quotedTableName}
|
|
214
|
+
WHERE namespace = $1 AND key = $2`,
|
|
215
|
+
[this.namespace, key]
|
|
216
|
+
);
|
|
217
|
+
return rows[0]?.ttl_ms === null || rows[0]?.ttl_ms === void 0 ? null : Number(rows[0].ttl_ms);
|
|
218
|
+
}
|
|
219
|
+
async spop(key, count = 1) {
|
|
220
|
+
const limit = Math.max(1, Math.floor(count));
|
|
221
|
+
const rows = await this.sql.unsafe(
|
|
222
|
+
`DELETE FROM ${this.quotedTableName}
|
|
223
|
+
WHERE (namespace, key, member) IN (
|
|
224
|
+
SELECT namespace, key, member FROM ${this.quotedTableName}
|
|
225
|
+
WHERE namespace = $1 AND key = $2
|
|
226
|
+
AND (expires_at IS NULL OR expires_at > NOW())
|
|
227
|
+
ORDER BY RANDOM() LIMIT $3
|
|
228
|
+
)
|
|
229
|
+
RETURNING member`,
|
|
230
|
+
[this.namespace, key, limit]
|
|
231
|
+
);
|
|
232
|
+
return rows.map((r) => r.member);
|
|
233
|
+
}
|
|
234
|
+
async srandmember(key, count = 1) {
|
|
235
|
+
const limit = Math.abs(Math.floor(count));
|
|
236
|
+
if (limit === 0) return [];
|
|
237
|
+
const rows = await this.sql.unsafe(
|
|
238
|
+
`SELECT member FROM ${this.quotedTableName}
|
|
239
|
+
WHERE namespace = $1 AND key = $2
|
|
240
|
+
AND (expires_at IS NULL OR expires_at > NOW())
|
|
241
|
+
ORDER BY RANDOM() LIMIT $3`,
|
|
242
|
+
[this.namespace, key, limit]
|
|
243
|
+
);
|
|
244
|
+
return rows.map((r) => r.member);
|
|
245
|
+
}
|
|
246
|
+
async smove(source, destination, member) {
|
|
247
|
+
const rows = await this.sql.unsafe(
|
|
248
|
+
`WITH moved AS (
|
|
249
|
+
DELETE FROM ${this.quotedTableName}
|
|
250
|
+
WHERE namespace = $1
|
|
251
|
+
AND key = $2
|
|
252
|
+
AND member = $4
|
|
253
|
+
AND (expires_at IS NULL OR expires_at > NOW())
|
|
254
|
+
RETURNING member
|
|
255
|
+
), inserted AS (
|
|
256
|
+
INSERT INTO ${this.quotedTableName} (namespace, key, member, expires_at, updated_at)
|
|
257
|
+
SELECT
|
|
258
|
+
$1,
|
|
259
|
+
$3,
|
|
260
|
+
member,
|
|
261
|
+
(SELECT MAX(expires_at) FROM ${this.quotedTableName} WHERE namespace = $1 AND key = $3),
|
|
262
|
+
NOW()
|
|
263
|
+
FROM moved
|
|
264
|
+
ON CONFLICT (namespace, key, member) DO UPDATE
|
|
265
|
+
SET updated_at = NOW()
|
|
266
|
+
RETURNING member
|
|
267
|
+
)
|
|
268
|
+
SELECT member FROM moved`,
|
|
269
|
+
[this.namespace, source, destination, member]
|
|
270
|
+
);
|
|
271
|
+
return rows.length > 0;
|
|
272
|
+
}
|
|
273
|
+
async cleanupExpired(limit = 1e3) {
|
|
274
|
+
const rows = await this.sql.unsafe(
|
|
275
|
+
`DELETE FROM ${this.quotedTableName}
|
|
276
|
+
WHERE ctid IN (
|
|
277
|
+
SELECT ctid
|
|
278
|
+
FROM ${this.quotedTableName}
|
|
279
|
+
WHERE expires_at IS NOT NULL AND expires_at <= NOW()
|
|
280
|
+
LIMIT $1
|
|
281
|
+
)
|
|
282
|
+
RETURNING member`,
|
|
283
|
+
[Math.max(1, Math.floor(limit))]
|
|
284
|
+
);
|
|
285
|
+
return rows.length;
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
function createPgSet(options) {
|
|
289
|
+
return new PgSet(options);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
export {
|
|
293
|
+
PgSet,
|
|
294
|
+
createPgSet
|
|
295
|
+
};
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
import {
|
|
2
|
+
indexName,
|
|
3
|
+
quoteQualifiedName
|
|
4
|
+
} from "./chunk-RWRM5RVO.js";
|
|
5
|
+
|
|
6
|
+
// src/hash.ts
|
|
7
|
+
var DEFAULT_TABLE_NAME = "pgredis_hash";
|
|
8
|
+
var DEFAULT_NAMESPACE = "default";
|
|
9
|
+
function jsonValue(value) {
|
|
10
|
+
return JSON.stringify(value);
|
|
11
|
+
}
|
|
12
|
+
function parseValue(value) {
|
|
13
|
+
if (typeof value !== "string") return value;
|
|
14
|
+
try {
|
|
15
|
+
return JSON.parse(value);
|
|
16
|
+
} catch {
|
|
17
|
+
return value;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
var PgHash = class {
|
|
21
|
+
namespace;
|
|
22
|
+
tableName;
|
|
23
|
+
quotedTableName;
|
|
24
|
+
sql;
|
|
25
|
+
constructor(options) {
|
|
26
|
+
this.sql = options.sql;
|
|
27
|
+
this.namespace = options.namespace || DEFAULT_NAMESPACE;
|
|
28
|
+
this.tableName = options.tableName || DEFAULT_TABLE_NAME;
|
|
29
|
+
this.quotedTableName = quoteQualifiedName(this.tableName);
|
|
30
|
+
}
|
|
31
|
+
async ensureSchema(options = {}) {
|
|
32
|
+
const persistence = options.unlogged === false ? "" : "UNLOGGED ";
|
|
33
|
+
await this.sql.unsafe(`
|
|
34
|
+
CREATE ${persistence}TABLE IF NOT EXISTS ${this.quotedTableName} (
|
|
35
|
+
namespace TEXT NOT NULL,
|
|
36
|
+
key TEXT NOT NULL,
|
|
37
|
+
field TEXT NOT NULL,
|
|
38
|
+
value JSONB NOT NULL,
|
|
39
|
+
expires_at TIMESTAMPTZ,
|
|
40
|
+
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
41
|
+
PRIMARY KEY (namespace, key, field)
|
|
42
|
+
)
|
|
43
|
+
`);
|
|
44
|
+
await this.sql.unsafe(`
|
|
45
|
+
ALTER TABLE ${this.quotedTableName}
|
|
46
|
+
ADD COLUMN IF NOT EXISTS expires_at TIMESTAMPTZ
|
|
47
|
+
`);
|
|
48
|
+
await this.sql.unsafe(`
|
|
49
|
+
CREATE INDEX IF NOT EXISTS ${indexName(this.tableName, "namespace_key")}
|
|
50
|
+
ON ${this.quotedTableName} (namespace, key)
|
|
51
|
+
`);
|
|
52
|
+
await this.sql.unsafe(`
|
|
53
|
+
CREATE INDEX IF NOT EXISTS ${indexName(this.tableName, "expires_at")}
|
|
54
|
+
ON ${this.quotedTableName} (expires_at)
|
|
55
|
+
WHERE expires_at IS NOT NULL
|
|
56
|
+
`);
|
|
57
|
+
}
|
|
58
|
+
async hget(key, field) {
|
|
59
|
+
const rows = await this.sql.unsafe(
|
|
60
|
+
`SELECT value
|
|
61
|
+
FROM ${this.quotedTableName}
|
|
62
|
+
WHERE namespace = $1 AND key = $2 AND field = $3
|
|
63
|
+
AND (expires_at IS NULL OR expires_at > NOW())
|
|
64
|
+
LIMIT 1`,
|
|
65
|
+
[this.namespace, key, field]
|
|
66
|
+
);
|
|
67
|
+
return rows[0] ? parseValue(rows[0].value) : null;
|
|
68
|
+
}
|
|
69
|
+
async hset(key, field, value) {
|
|
70
|
+
await this.sql.unsafe(
|
|
71
|
+
`INSERT INTO ${this.quotedTableName} (namespace, key, field, value, expires_at, updated_at)
|
|
72
|
+
VALUES (
|
|
73
|
+
$1,
|
|
74
|
+
$2,
|
|
75
|
+
$3,
|
|
76
|
+
$4::jsonb,
|
|
77
|
+
(SELECT MAX(expires_at) FROM ${this.quotedTableName} WHERE namespace = $1 AND key = $2),
|
|
78
|
+
NOW()
|
|
79
|
+
)
|
|
80
|
+
ON CONFLICT (namespace, key, field) DO UPDATE
|
|
81
|
+
SET value = EXCLUDED.value,
|
|
82
|
+
updated_at = NOW()`,
|
|
83
|
+
[this.namespace, key, field, jsonValue(value)]
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
async hmset(key, entries) {
|
|
87
|
+
const values = Array.from(entries);
|
|
88
|
+
if (values.length === 0) return;
|
|
89
|
+
const params = [];
|
|
90
|
+
const groups = values.map(([field, value], index) => {
|
|
91
|
+
const base = index * 4;
|
|
92
|
+
params.push(this.namespace, key, field, jsonValue(value));
|
|
93
|
+
return `($${base + 1}, $${base + 2}, $${base + 3}, $${base + 4}::jsonb, (SELECT MAX(expires_at) FROM ${this.quotedTableName} WHERE namespace = $${base + 1} AND key = $${base + 2}), NOW())`;
|
|
94
|
+
}).join(", ");
|
|
95
|
+
await this.sql.unsafe(
|
|
96
|
+
`INSERT INTO ${this.quotedTableName} (namespace, key, field, value, expires_at, updated_at)
|
|
97
|
+
VALUES ${groups}
|
|
98
|
+
ON CONFLICT (namespace, key, field) DO UPDATE
|
|
99
|
+
SET value = EXCLUDED.value,
|
|
100
|
+
updated_at = NOW()`,
|
|
101
|
+
params
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
async hgetall(key) {
|
|
105
|
+
const rows = await this.sql.unsafe(
|
|
106
|
+
`SELECT field, value
|
|
107
|
+
FROM ${this.quotedTableName}
|
|
108
|
+
WHERE namespace = $1 AND key = $2
|
|
109
|
+
AND (expires_at IS NULL OR expires_at > NOW())
|
|
110
|
+
ORDER BY field ASC`,
|
|
111
|
+
[this.namespace, key]
|
|
112
|
+
);
|
|
113
|
+
return Object.fromEntries(rows.map((row) => [row.field, parseValue(row.value)]));
|
|
114
|
+
}
|
|
115
|
+
async hdel(key, field) {
|
|
116
|
+
const rows = await this.sql.unsafe(
|
|
117
|
+
`DELETE FROM ${this.quotedTableName}
|
|
118
|
+
WHERE namespace = $1 AND key = $2 AND field = $3
|
|
119
|
+
RETURNING field`,
|
|
120
|
+
[this.namespace, key, field]
|
|
121
|
+
);
|
|
122
|
+
return rows.length > 0;
|
|
123
|
+
}
|
|
124
|
+
async hclear(key) {
|
|
125
|
+
const rows = await this.sql.unsafe(
|
|
126
|
+
`DELETE FROM ${this.quotedTableName}
|
|
127
|
+
WHERE namespace = $1 AND key = $2
|
|
128
|
+
RETURNING field`,
|
|
129
|
+
[this.namespace, key]
|
|
130
|
+
);
|
|
131
|
+
return rows.length;
|
|
132
|
+
}
|
|
133
|
+
async hexists(key, field) {
|
|
134
|
+
const rows = await this.sql.unsafe(
|
|
135
|
+
`SELECT EXISTS (
|
|
136
|
+
SELECT 1 FROM ${this.quotedTableName}
|
|
137
|
+
WHERE namespace = $1 AND key = $2 AND field = $3
|
|
138
|
+
AND (expires_at IS NULL OR expires_at > NOW())
|
|
139
|
+
) AS exists`,
|
|
140
|
+
[this.namespace, key, field]
|
|
141
|
+
);
|
|
142
|
+
return !!rows[0]?.exists;
|
|
143
|
+
}
|
|
144
|
+
async hlen(key) {
|
|
145
|
+
const rows = await this.sql.unsafe(
|
|
146
|
+
`SELECT COUNT(*)::bigint AS value
|
|
147
|
+
FROM ${this.quotedTableName}
|
|
148
|
+
WHERE namespace = $1 AND key = $2
|
|
149
|
+
AND (expires_at IS NULL OR expires_at > NOW())`,
|
|
150
|
+
[this.namespace, key]
|
|
151
|
+
);
|
|
152
|
+
return Number(rows[0]?.value ?? 0);
|
|
153
|
+
}
|
|
154
|
+
async hincrby(key, field, amount = 1) {
|
|
155
|
+
if (!Number.isSafeInteger(amount)) throw new Error(`Invalid hash increment: ${amount}`);
|
|
156
|
+
const rows = await this.sql.unsafe(
|
|
157
|
+
`INSERT INTO ${this.quotedTableName} (namespace, key, field, value, expires_at, updated_at)
|
|
158
|
+
VALUES (
|
|
159
|
+
$1,
|
|
160
|
+
$2,
|
|
161
|
+
$3,
|
|
162
|
+
to_jsonb($4::bigint),
|
|
163
|
+
(SELECT MAX(expires_at) FROM ${this.quotedTableName} WHERE namespace = $1 AND key = $2),
|
|
164
|
+
NOW()
|
|
165
|
+
)
|
|
166
|
+
ON CONFLICT (namespace, key, field) DO UPDATE
|
|
167
|
+
SET value = to_jsonb(((${this.quotedTableName}.value #>> '{}')::bigint + $4::bigint)),
|
|
168
|
+
updated_at = NOW()
|
|
169
|
+
RETURNING (value #>> '{}')::bigint AS value`,
|
|
170
|
+
[this.namespace, key, field, Math.trunc(amount)]
|
|
171
|
+
);
|
|
172
|
+
return Number(rows[0]?.value ?? amount);
|
|
173
|
+
}
|
|
174
|
+
async hscan(key, cursor = null, count = 100) {
|
|
175
|
+
const rows = await this.sql.unsafe(
|
|
176
|
+
`SELECT field, value
|
|
177
|
+
FROM ${this.quotedTableName}
|
|
178
|
+
WHERE namespace = $1
|
|
179
|
+
AND key = $2
|
|
180
|
+
AND field > COALESCE($3::text, '')
|
|
181
|
+
AND (expires_at IS NULL OR expires_at > NOW())
|
|
182
|
+
ORDER BY field ASC
|
|
183
|
+
LIMIT $4`,
|
|
184
|
+
[this.namespace, key, cursor, Math.max(1, Math.floor(count))]
|
|
185
|
+
);
|
|
186
|
+
return {
|
|
187
|
+
cursor: rows.length === count ? rows[rows.length - 1].field : null,
|
|
188
|
+
entries: rows.map((row) => [row.field, parseValue(row.value)])
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
async expire(key, ttlMs) {
|
|
192
|
+
const rows = await this.sql.unsafe(
|
|
193
|
+
`UPDATE ${this.quotedTableName}
|
|
194
|
+
SET expires_at = NOW() + ($3::bigint * INTERVAL '1 millisecond'),
|
|
195
|
+
updated_at = NOW()
|
|
196
|
+
WHERE namespace = $1 AND key = $2
|
|
197
|
+
RETURNING field`,
|
|
198
|
+
[this.namespace, key, Math.max(0, Math.floor(ttlMs))]
|
|
199
|
+
);
|
|
200
|
+
return rows.length;
|
|
201
|
+
}
|
|
202
|
+
async persist(key) {
|
|
203
|
+
const rows = await this.sql.unsafe(
|
|
204
|
+
`UPDATE ${this.quotedTableName}
|
|
205
|
+
SET expires_at = NULL,
|
|
206
|
+
updated_at = NOW()
|
|
207
|
+
WHERE namespace = $1 AND key = $2
|
|
208
|
+
RETURNING field`,
|
|
209
|
+
[this.namespace, key]
|
|
210
|
+
);
|
|
211
|
+
return rows.length;
|
|
212
|
+
}
|
|
213
|
+
async ttl(key) {
|
|
214
|
+
const rows = await this.sql.unsafe(
|
|
215
|
+
`SELECT CASE
|
|
216
|
+
WHEN MAX(expires_at) IS NULL THEN NULL
|
|
217
|
+
ELSE GREATEST(0, FLOOR(EXTRACT(EPOCH FROM (MAX(expires_at) - NOW())) * 1000))::bigint
|
|
218
|
+
END AS ttl_ms
|
|
219
|
+
FROM ${this.quotedTableName}
|
|
220
|
+
WHERE namespace = $1 AND key = $2`,
|
|
221
|
+
[this.namespace, key]
|
|
222
|
+
);
|
|
223
|
+
return rows[0]?.ttl_ms === null || rows[0]?.ttl_ms === void 0 ? null : Number(rows[0].ttl_ms);
|
|
224
|
+
}
|
|
225
|
+
async hmget(key, ...fields) {
|
|
226
|
+
if (fields.length === 0) return [];
|
|
227
|
+
const placeholders = fields.map((_, i) => `$${i + 3}`).join(", ");
|
|
228
|
+
const rows = await this.sql.unsafe(
|
|
229
|
+
`SELECT field, value FROM ${this.quotedTableName}
|
|
230
|
+
WHERE namespace = $1 AND key = $2 AND field IN (${placeholders})
|
|
231
|
+
AND (expires_at IS NULL OR expires_at > NOW())`,
|
|
232
|
+
[this.namespace, key, ...fields]
|
|
233
|
+
);
|
|
234
|
+
const map = new Map(rows.map((r) => [r.field, parseValue(r.value)]));
|
|
235
|
+
return fields.map((field) => map.get(field) ?? null);
|
|
236
|
+
}
|
|
237
|
+
async hkeys(key) {
|
|
238
|
+
const rows = await this.sql.unsafe(
|
|
239
|
+
`SELECT field FROM ${this.quotedTableName}
|
|
240
|
+
WHERE namespace = $1 AND key = $2
|
|
241
|
+
AND (expires_at IS NULL OR expires_at > NOW())
|
|
242
|
+
ORDER BY field ASC`,
|
|
243
|
+
[this.namespace, key]
|
|
244
|
+
);
|
|
245
|
+
return rows.map((r) => r.field);
|
|
246
|
+
}
|
|
247
|
+
async hvals(key) {
|
|
248
|
+
const rows = await this.sql.unsafe(
|
|
249
|
+
`SELECT value FROM ${this.quotedTableName}
|
|
250
|
+
WHERE namespace = $1 AND key = $2
|
|
251
|
+
AND (expires_at IS NULL OR expires_at > NOW())
|
|
252
|
+
ORDER BY field ASC`,
|
|
253
|
+
[this.namespace, key]
|
|
254
|
+
);
|
|
255
|
+
return rows.map((r) => parseValue(r.value));
|
|
256
|
+
}
|
|
257
|
+
async hstrlen(key, field) {
|
|
258
|
+
const rows = await this.sql.unsafe(
|
|
259
|
+
`SELECT LENGTH(value #>> '{}') AS length FROM ${this.quotedTableName}
|
|
260
|
+
WHERE namespace = $1 AND key = $2 AND field = $3
|
|
261
|
+
AND (expires_at IS NULL OR expires_at > NOW())`,
|
|
262
|
+
[this.namespace, key, field]
|
|
263
|
+
);
|
|
264
|
+
return rows[0] ? Number(rows[0].length) : 0;
|
|
265
|
+
}
|
|
266
|
+
async cleanupExpired(limit = 1e3) {
|
|
267
|
+
const rows = await this.sql.unsafe(
|
|
268
|
+
`DELETE FROM ${this.quotedTableName}
|
|
269
|
+
WHERE ctid IN (
|
|
270
|
+
SELECT ctid
|
|
271
|
+
FROM ${this.quotedTableName}
|
|
272
|
+
WHERE expires_at IS NOT NULL AND expires_at <= NOW()
|
|
273
|
+
LIMIT $1
|
|
274
|
+
)
|
|
275
|
+
RETURNING field`,
|
|
276
|
+
[Math.max(1, Math.floor(limit))]
|
|
277
|
+
);
|
|
278
|
+
return rows.length;
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
function createPgHash(options) {
|
|
282
|
+
return new PgHash(options);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export {
|
|
286
|
+
PgHash,
|
|
287
|
+
createPgHash
|
|
288
|
+
};
|