@postgresx/noredis 0.1.0 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +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-Dpoe2LdN.d.ts +494 -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 +321 -989
- 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,130 @@
|
|
|
1
|
+
import { N as PgredisClient, Y as PipelineFacade, S as PgredisMigrationAliases } from '../client-Dpoe2LdN.js';
|
|
2
|
+
import '../kv-cache.js';
|
|
3
|
+
import '../sql-DKf2TDlu.js';
|
|
4
|
+
import '../hash-index.js';
|
|
5
|
+
import '../list-index.js';
|
|
6
|
+
import '../pubsub-index.js';
|
|
7
|
+
import '../set-index.js';
|
|
8
|
+
import '../sorted-set-index.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* ioredis 兼容外观适配器
|
|
12
|
+
*
|
|
13
|
+
* 提供 ioredis 风格的小写方法名和 chainable 接口,
|
|
14
|
+
* 内部委托给 @postgresx/noredis 的 PgredisMigrationAliases。
|
|
15
|
+
*
|
|
16
|
+
* 不安装 ioredis 运行时依赖。用于 NestJS microservices、
|
|
17
|
+
* cache-manager 等期望 ioredis 接口的场景。
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
interface IoredisAdapterOptions {
|
|
21
|
+
client: PgredisClient;
|
|
22
|
+
/** 是否将不支持命令静默返回 null 而非抛错,默认 false */
|
|
23
|
+
silentUnsupported?: boolean;
|
|
24
|
+
}
|
|
25
|
+
interface IoredisLikeAdapter {
|
|
26
|
+
get(key: string): Promise<string | null>;
|
|
27
|
+
set(key: string, value: string, ...args: unknown[]): Promise<"OK" | null>;
|
|
28
|
+
del(...keys: string[]): Promise<number>;
|
|
29
|
+
unlink(...keys: string[]): Promise<number>;
|
|
30
|
+
exists(...keys: string[]): Promise<number>;
|
|
31
|
+
type(key: string): Promise<string>;
|
|
32
|
+
expire(key: string, seconds: number): Promise<number>;
|
|
33
|
+
pexpire(key: string, ms: number): Promise<number>;
|
|
34
|
+
ttl(key: string): Promise<number>;
|
|
35
|
+
pttl(key: string): Promise<number>;
|
|
36
|
+
persist(key: string): Promise<number>;
|
|
37
|
+
setex(key: string, seconds: number, value: string): Promise<"OK">;
|
|
38
|
+
psetex(key: string, ms: number, value: string): Promise<"OK">;
|
|
39
|
+
setnx(key: string, value: string): Promise<number>;
|
|
40
|
+
getset(key: string, value: string): Promise<string | null>;
|
|
41
|
+
getdel(key: string): Promise<string | null>;
|
|
42
|
+
mget(...keys: string[]): Promise<Array<string | null>>;
|
|
43
|
+
mset(kv: Record<string, string>): Promise<"OK">;
|
|
44
|
+
keys(pattern?: string): Promise<string[]>;
|
|
45
|
+
scan(cursor?: string | null, options?: {
|
|
46
|
+
count?: number;
|
|
47
|
+
match?: string;
|
|
48
|
+
}): Promise<[string | null, string[]]>;
|
|
49
|
+
rename(key: string, newKey: string): Promise<boolean>;
|
|
50
|
+
incr(key: string): Promise<number>;
|
|
51
|
+
incrby(key: string, amount: number): Promise<number>;
|
|
52
|
+
decr(key: string): Promise<number>;
|
|
53
|
+
decrby(key: string, amount: number): Promise<number>;
|
|
54
|
+
hget(key: string, field: string): Promise<string | null>;
|
|
55
|
+
hset(key: string, field: string, value: string): Promise<number>;
|
|
56
|
+
hmset(key: string, kv: Record<string, string>): Promise<"OK">;
|
|
57
|
+
hmget(key: string, ...fields: string[]): Promise<Array<string | null>>;
|
|
58
|
+
hgetall(key: string): Promise<Record<string, string>>;
|
|
59
|
+
hdel(key: string, ...fields: string[]): Promise<number>;
|
|
60
|
+
hexists(key: string, field: string): Promise<number>;
|
|
61
|
+
hlen(key: string): Promise<number>;
|
|
62
|
+
hincrby(key: string, field: string, amount?: number): Promise<number>;
|
|
63
|
+
hkeys(key: string): Promise<string[]>;
|
|
64
|
+
hvals(key: string): Promise<string[]>;
|
|
65
|
+
hstrlen(key: string, field: string): Promise<number>;
|
|
66
|
+
hscan(key: string, cursor?: string | null, options?: {
|
|
67
|
+
count?: number;
|
|
68
|
+
}): Promise<[string | null, Array<[string, string]>]>;
|
|
69
|
+
lpush(key: string, ...values: string[]): Promise<number>;
|
|
70
|
+
rpush(key: string, ...values: string[]): Promise<number>;
|
|
71
|
+
lpop(key: string): Promise<string | null>;
|
|
72
|
+
rpop(key: string): Promise<string | null>;
|
|
73
|
+
blpop(keys: string | string[], timeout?: number): Promise<[string, string] | null>;
|
|
74
|
+
brpop(keys: string | string[], timeout?: number): Promise<[string, string] | null>;
|
|
75
|
+
llen(key: string): Promise<number>;
|
|
76
|
+
lrange(key: string, start?: number, stop?: number): Promise<string[]>;
|
|
77
|
+
lrem(key: string, count: number, value: string): Promise<number>;
|
|
78
|
+
sadd(key: string, ...members: string[]): Promise<number>;
|
|
79
|
+
srem(key: string, ...members: string[]): Promise<number>;
|
|
80
|
+
smembers(key: string): Promise<string[]>;
|
|
81
|
+
sismember(key: string, member: string): Promise<number>;
|
|
82
|
+
scard(key: string): Promise<number>;
|
|
83
|
+
sinter(...keys: string[]): Promise<string[]>;
|
|
84
|
+
sunion(...keys: string[]): Promise<string[]>;
|
|
85
|
+
sdiff(...keys: string[]): Promise<string[]>;
|
|
86
|
+
spop(key: string, count?: number): Promise<string | string[] | null>;
|
|
87
|
+
srandmember(key: string, count?: number): Promise<string | string[] | null>;
|
|
88
|
+
smove(src: string, dst: string, member: string): Promise<number>;
|
|
89
|
+
sscan(key: string, cursor?: string | null, options?: {
|
|
90
|
+
count?: number;
|
|
91
|
+
}): Promise<[string | null, string[]]>;
|
|
92
|
+
zadd(key: string, ...args: Array<number | string>): Promise<number>;
|
|
93
|
+
zscore(key: string, member: string): Promise<number | null>;
|
|
94
|
+
zrange(key: string, start: number, stop: number): Promise<string[]>;
|
|
95
|
+
zrevrange(key: string, start: number, stop: number): Promise<string[]>;
|
|
96
|
+
zrangebyscore(key: string, min: number, max: number): Promise<string[]>;
|
|
97
|
+
zrank(key: string, member: string): Promise<number | null>;
|
|
98
|
+
zcount(key: string, min: number, max: number): Promise<number>;
|
|
99
|
+
zrem(key: string, ...members: string[]): Promise<number>;
|
|
100
|
+
zcard(key: string): Promise<number>;
|
|
101
|
+
zincrby(key: string, amount: number, member: string): Promise<number>;
|
|
102
|
+
zpopmin(key: string, count?: number): Promise<Array<{
|
|
103
|
+
member: string;
|
|
104
|
+
score: number;
|
|
105
|
+
}>>;
|
|
106
|
+
zpopmax(key: string, count?: number): Promise<Array<{
|
|
107
|
+
member: string;
|
|
108
|
+
score: number;
|
|
109
|
+
}>>;
|
|
110
|
+
zscan(key: string, cursor?: string | null, options?: {
|
|
111
|
+
count?: number;
|
|
112
|
+
}): Promise<[string | null, Array<{
|
|
113
|
+
member: string;
|
|
114
|
+
score: number;
|
|
115
|
+
}>]>;
|
|
116
|
+
publish(channel: string, message: string): Promise<number>;
|
|
117
|
+
connect(): Promise<void>;
|
|
118
|
+
quit(): Promise<void>;
|
|
119
|
+
disconnect(): void;
|
|
120
|
+
duplicate(): IoredisLikeAdapter;
|
|
121
|
+
on(event: string, handler: (...args: unknown[]) => void): IoredisLikeAdapter;
|
|
122
|
+
off(_event: string, _handler: (...args: unknown[]) => void): IoredisLikeAdapter;
|
|
123
|
+
pipeline(): PipelineFacade;
|
|
124
|
+
multi(): PipelineFacade;
|
|
125
|
+
/** 底层 noredis 客户端 */
|
|
126
|
+
readonly noredis: PgredisMigrationAliases;
|
|
127
|
+
}
|
|
128
|
+
declare function createIoredisAdapter(options: IoredisAdapterOptions): IoredisLikeAdapter;
|
|
129
|
+
|
|
130
|
+
export { type IoredisAdapterOptions, type IoredisLikeAdapter, createIoredisAdapter };
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createPgredisMigrationAliases
|
|
3
|
+
} from "../chunk-WRMEEA6T.js";
|
|
4
|
+
|
|
5
|
+
// src/adapters/ioredis.ts
|
|
6
|
+
function str(v) {
|
|
7
|
+
if (v === null || v === void 0) return null;
|
|
8
|
+
return typeof v === "string" ? v : JSON.stringify(v);
|
|
9
|
+
}
|
|
10
|
+
function strArr(vals) {
|
|
11
|
+
return vals.map((v) => typeof v === "string" ? v : JSON.stringify(v));
|
|
12
|
+
}
|
|
13
|
+
function createIoredisAdapter(options) {
|
|
14
|
+
const aliases = createPgredisMigrationAliases(options.client);
|
|
15
|
+
const silent = options.silentUnsupported ?? false;
|
|
16
|
+
const adapter = {
|
|
17
|
+
noredis: aliases,
|
|
18
|
+
// KV 基础
|
|
19
|
+
get: (key) => aliases.get(key).then(str),
|
|
20
|
+
set: (key, value, ...args) => {
|
|
21
|
+
const opts = {};
|
|
22
|
+
for (let i = 0; i < args.length; i++) {
|
|
23
|
+
const a = String(args[i]).toUpperCase();
|
|
24
|
+
if (a === "EX" && i + 1 < args.length) {
|
|
25
|
+
opts.EX = Number(args[++i]);
|
|
26
|
+
} else if (a === "PX" && i + 1 < args.length) {
|
|
27
|
+
opts.PX = Number(args[++i]);
|
|
28
|
+
} else if (a === "NX") {
|
|
29
|
+
opts.NX = true;
|
|
30
|
+
} else if (a === "XX") {
|
|
31
|
+
opts.XX = true;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return aliases.set(key, value, opts);
|
|
35
|
+
},
|
|
36
|
+
del: (...keys) => aliases.del(...keys),
|
|
37
|
+
unlink: (...keys) => aliases.unlink(...keys),
|
|
38
|
+
exists: (...keys) => aliases.exists(...keys),
|
|
39
|
+
type: (key) => aliases.type(key).then((t) => t === "none" ? "none" : "string"),
|
|
40
|
+
// KV 过期
|
|
41
|
+
expire: (key, seconds) => aliases.expire(key, seconds),
|
|
42
|
+
pexpire: (key, ms) => aliases.pexpire(key, ms),
|
|
43
|
+
ttl: (key) => aliases.ttl(key),
|
|
44
|
+
pttl: (key) => aliases.pttl(key),
|
|
45
|
+
persist: (key) => aliases.persist(key),
|
|
46
|
+
// KV 条件
|
|
47
|
+
setex: (key, seconds, value) => aliases.setex(key, seconds, value),
|
|
48
|
+
psetex: (key, ms, value) => aliases.psetex(key, ms, value),
|
|
49
|
+
setnx: (key, value) => aliases.setnx(key, value),
|
|
50
|
+
getset: (key, value) => aliases.getset(key, value).then(str),
|
|
51
|
+
getdel: (key) => aliases.getdel(key).then(str),
|
|
52
|
+
// KV 批量
|
|
53
|
+
mget: (...keys) => aliases.mget(...keys).then((vals) => vals.map(str)),
|
|
54
|
+
mset: (kv) => aliases.mset(kv),
|
|
55
|
+
// KV 扫描
|
|
56
|
+
keys: (pattern) => aliases.keys(pattern),
|
|
57
|
+
scan: (cursor, options2) => aliases.scan(cursor, options2?.count, options2?.match).then((r) => [r.cursor, r.keys]),
|
|
58
|
+
rename: (key, newKey) => aliases.rename(key, newKey),
|
|
59
|
+
// 计数器
|
|
60
|
+
incr: (key) => aliases.incr(key),
|
|
61
|
+
incrby: (key, amount) => aliases.incrby(key, amount),
|
|
62
|
+
decr: (key) => aliases.decr(key),
|
|
63
|
+
decrby: (key, amount) => aliases.decrby(key, amount),
|
|
64
|
+
// Hash
|
|
65
|
+
hget: (key, field) => aliases.hget(key, field).then(str),
|
|
66
|
+
hset: (key, field, value) => aliases.hset(key, field, value),
|
|
67
|
+
hmset: (key, kv) => aliases.hmset(key, kv),
|
|
68
|
+
hmget: (key, ...fields) => aliases.hmget(key, ...fields).then((vals) => vals.map(str)),
|
|
69
|
+
hgetall: (key) => aliases.hgetall(key),
|
|
70
|
+
hdel: (key, ...fields) => aliases.hdel(key, ...fields),
|
|
71
|
+
hexists: (key, field) => aliases.hexists(key, field),
|
|
72
|
+
hlen: (key) => aliases.hlen(key),
|
|
73
|
+
hincrby: (key, field, amount) => aliases.hincrby(key, field, amount),
|
|
74
|
+
hkeys: (key) => aliases.hkeys(key),
|
|
75
|
+
hvals: (key) => aliases.hvals(key).then(strArr),
|
|
76
|
+
hstrlen: (key, field) => aliases.hstrlen(key, field),
|
|
77
|
+
hscan: (key, cursor, options2) => aliases.hscan(key, cursor, options2?.count).then((r) => [r.cursor, r.entries]),
|
|
78
|
+
// List
|
|
79
|
+
lpush: (key, ...values) => aliases.lpush(key, ...values),
|
|
80
|
+
rpush: (key, ...values) => aliases.rpush(key, ...values),
|
|
81
|
+
lpop: (key) => aliases.lpop(key).then(str),
|
|
82
|
+
rpop: (key) => aliases.rpop(key).then(str),
|
|
83
|
+
blpop: (keys, timeout) => aliases.blpop(keys, timeout),
|
|
84
|
+
brpop: (keys, timeout) => aliases.brpop(keys, timeout),
|
|
85
|
+
llen: (key) => aliases.llen(key),
|
|
86
|
+
lrange: (key, start, stop) => aliases.lrange(key, start, stop).then(strArr),
|
|
87
|
+
lrem: (key, count, value) => aliases.lrem(key, count, value),
|
|
88
|
+
// Set
|
|
89
|
+
sadd: (key, ...members) => aliases.sadd(key, ...members),
|
|
90
|
+
srem: (key, ...members) => aliases.srem(key, ...members),
|
|
91
|
+
smembers: (key) => aliases.smembers(key),
|
|
92
|
+
sismember: (key, member) => aliases.sismember(key, member),
|
|
93
|
+
scard: (key) => aliases.scard(key),
|
|
94
|
+
sinter: (...keys) => aliases.sinter(...keys),
|
|
95
|
+
sunion: (...keys) => aliases.sunion(...keys),
|
|
96
|
+
sdiff: (...keys) => aliases.sdiff(...keys),
|
|
97
|
+
spop: (key, count) => {
|
|
98
|
+
if (count === void 0) return aliases.spop(key, 1).then((r) => r[0] ?? null);
|
|
99
|
+
return aliases.spop(key, count);
|
|
100
|
+
},
|
|
101
|
+
srandmember: (key, count) => {
|
|
102
|
+
if (count === void 0) return aliases.srandmember(key, 1).then((r) => r[0] ?? null);
|
|
103
|
+
return aliases.srandmember(key, count);
|
|
104
|
+
},
|
|
105
|
+
smove: (src, dst, member) => aliases.smove(src, dst, member),
|
|
106
|
+
sscan: (key, cursor, options2) => aliases.sscan(key, cursor, options2?.count).then((r) => [r.cursor, r.members]),
|
|
107
|
+
// Sorted Set
|
|
108
|
+
zadd: (key, ...args) => aliases.zadd(key, ...args),
|
|
109
|
+
zscore: (key, member) => aliases.zscore(key, member),
|
|
110
|
+
zrange: (key, start, stop) => aliases.zrange(key, start, stop),
|
|
111
|
+
zrevrange: (key, start, stop) => aliases.zrevrange(key, start, stop),
|
|
112
|
+
zrangebyscore: (key, min, max) => aliases.zrangeByScore(key, min, max),
|
|
113
|
+
zrank: (key, member) => aliases.zrank(key, member),
|
|
114
|
+
zcount: (key, min, max) => aliases.zcount(key, min, max),
|
|
115
|
+
zrem: (key, ...members) => aliases.zrem(key, ...members),
|
|
116
|
+
zcard: (key) => aliases.zcard(key),
|
|
117
|
+
zincrby: (key, amount, member) => aliases.zincrby(key, amount, member),
|
|
118
|
+
zpopmin: (key, count) => aliases.zpopmin(key, count),
|
|
119
|
+
zpopmax: (key, count) => aliases.zpopmax(key, count),
|
|
120
|
+
zscan: (key, cursor, options2) => aliases.zscan(key, cursor, options2?.count).then((r) => [r.cursor, r.entries]),
|
|
121
|
+
// Pub/Sub
|
|
122
|
+
publish: (channel, message) => aliases.publish(channel, message),
|
|
123
|
+
// 连接
|
|
124
|
+
connect: () => aliases.connect(),
|
|
125
|
+
quit: () => aliases.quit(),
|
|
126
|
+
disconnect: () => aliases.disconnect(),
|
|
127
|
+
duplicate: () => createIoredisAdapter({ client: options.client, silentUnsupported: silent }),
|
|
128
|
+
on: (event, handler) => {
|
|
129
|
+
aliases.on(event, handler);
|
|
130
|
+
return adapter;
|
|
131
|
+
},
|
|
132
|
+
off: () => adapter,
|
|
133
|
+
// Pipeline
|
|
134
|
+
pipeline: () => aliases.pipeline(),
|
|
135
|
+
multi: () => aliases.multi()
|
|
136
|
+
};
|
|
137
|
+
return adapter;
|
|
138
|
+
}
|
|
139
|
+
export {
|
|
140
|
+
createIoredisAdapter
|
|
141
|
+
};
|
package/dist/adapters/node.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Pool, ClientConfig, PoolConfig } from 'pg';
|
|
2
|
-
import {
|
|
2
|
+
import { NotifyHandler, PgListenerHandle } from '../pubsub-index.js';
|
|
3
3
|
import { P as PgSqlLike } from '../sql-DKf2TDlu.js';
|
|
4
4
|
|
|
5
5
|
type PgConnectionInput = string | PoolConfig | Pool;
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { N as PgredisClient, Y as PipelineFacade, S as PgredisMigrationAliases } from '../client-Dpoe2LdN.js';
|
|
2
|
+
import '../kv-cache.js';
|
|
3
|
+
import '../sql-DKf2TDlu.js';
|
|
4
|
+
import '../hash-index.js';
|
|
5
|
+
import '../list-index.js';
|
|
6
|
+
import '../pubsub-index.js';
|
|
7
|
+
import '../set-index.js';
|
|
8
|
+
import '../sorted-set-index.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* redis.js (node-redis) 兼容外观适配器
|
|
12
|
+
*
|
|
13
|
+
* 提供 redis.js 风格的 camelCase 方法名和返回值结构,
|
|
14
|
+
* 内部委托给 @postgresx/noredis 的 PgredisMigrationAliases。
|
|
15
|
+
*
|
|
16
|
+
* 不安装 redis 运行时依赖。
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
interface RedisJsAdapterOptions {
|
|
20
|
+
client: PgredisClient;
|
|
21
|
+
}
|
|
22
|
+
interface RedisJsLikeAdapter {
|
|
23
|
+
get(key: string): Promise<string | null>;
|
|
24
|
+
set(key: string, value: string, options?: {
|
|
25
|
+
EX?: number;
|
|
26
|
+
PX?: number;
|
|
27
|
+
NX?: boolean;
|
|
28
|
+
XX?: boolean;
|
|
29
|
+
}): Promise<"OK" | null>;
|
|
30
|
+
del(...keys: string[]): Promise<number>;
|
|
31
|
+
unlink(...keys: string[]): Promise<number>;
|
|
32
|
+
exists(...keys: string[]): Promise<number>;
|
|
33
|
+
type(key: string): Promise<string>;
|
|
34
|
+
expire(key: string, seconds: number): Promise<number>;
|
|
35
|
+
pExpire(key: string, ms: number): Promise<number>;
|
|
36
|
+
ttl(key: string): Promise<number>;
|
|
37
|
+
pTtl(key: string): Promise<number>;
|
|
38
|
+
persist(key: string): Promise<number>;
|
|
39
|
+
setEx(key: string, seconds: number, value: string): Promise<"OK">;
|
|
40
|
+
pSetEx(key: string, ms: number, value: string): Promise<"OK">;
|
|
41
|
+
setNx(key: string, value: string): Promise<number>;
|
|
42
|
+
getSet(key: string, value: string): Promise<string | null>;
|
|
43
|
+
getDel(key: string): Promise<string | null>;
|
|
44
|
+
mGet(...keys: string[]): Promise<Array<string | null>>;
|
|
45
|
+
mSet(kv: Record<string, string>): Promise<"OK">;
|
|
46
|
+
keys(pattern?: string): Promise<string[]>;
|
|
47
|
+
scan(cursor?: string | null, options?: {
|
|
48
|
+
count?: number;
|
|
49
|
+
match?: string;
|
|
50
|
+
}): Promise<{
|
|
51
|
+
cursor: string | null;
|
|
52
|
+
keys: string[];
|
|
53
|
+
}>;
|
|
54
|
+
rename(key: string, newKey: string): Promise<boolean>;
|
|
55
|
+
incr(key: string): Promise<number>;
|
|
56
|
+
incrBy(key: string, amount: number): Promise<number>;
|
|
57
|
+
decr(key: string): Promise<number>;
|
|
58
|
+
decrBy(key: string, amount: number): Promise<number>;
|
|
59
|
+
hGet(key: string, field: string): Promise<string | null>;
|
|
60
|
+
hSet(key: string, field: string, value: string): Promise<number>;
|
|
61
|
+
hmSet(key: string, kv: Record<string, string>): Promise<"OK">;
|
|
62
|
+
hmGet(key: string, ...fields: string[]): Promise<Array<string | null>>;
|
|
63
|
+
hGetAll(key: string): Promise<Record<string, string>>;
|
|
64
|
+
hDel(key: string, ...fields: string[]): Promise<number>;
|
|
65
|
+
hExists(key: string, field: string): Promise<number>;
|
|
66
|
+
hLen(key: string): Promise<number>;
|
|
67
|
+
hIncrBy(key: string, field: string, amount?: number): Promise<number>;
|
|
68
|
+
hKeys(key: string): Promise<string[]>;
|
|
69
|
+
hVals(key: string): Promise<string[]>;
|
|
70
|
+
hStrLen(key: string, field: string): Promise<number>;
|
|
71
|
+
hScan(key: string, cursor?: string | null, options?: {
|
|
72
|
+
count?: number;
|
|
73
|
+
}): Promise<{
|
|
74
|
+
cursor: string | null;
|
|
75
|
+
entries: Array<[string, string]>;
|
|
76
|
+
}>;
|
|
77
|
+
lPush(key: string, ...values: string[]): Promise<number>;
|
|
78
|
+
rPush(key: string, ...values: string[]): Promise<number>;
|
|
79
|
+
lPop(key: string): Promise<string | null>;
|
|
80
|
+
rPop(key: string): Promise<string | null>;
|
|
81
|
+
blPop(keys: string | string[], timeout?: number): Promise<[string, string] | null>;
|
|
82
|
+
brPop(keys: string | string[], timeout?: number): Promise<[string, string] | null>;
|
|
83
|
+
lLen(key: string): Promise<number>;
|
|
84
|
+
lRange(key: string, start?: number, stop?: number): Promise<string[]>;
|
|
85
|
+
lRem(key: string, count: number, value: string): Promise<number>;
|
|
86
|
+
sAdd(key: string, ...members: string[]): Promise<number>;
|
|
87
|
+
sRem(key: string, ...members: string[]): Promise<number>;
|
|
88
|
+
sMembers(key: string): Promise<string[]>;
|
|
89
|
+
sIsMember(key: string, member: string): Promise<number>;
|
|
90
|
+
sCard(key: string): Promise<number>;
|
|
91
|
+
sInter(...keys: string[]): Promise<string[]>;
|
|
92
|
+
sUnion(...keys: string[]): Promise<string[]>;
|
|
93
|
+
sDiff(...keys: string[]): Promise<string[]>;
|
|
94
|
+
sPop(key: string, count?: number): Promise<string[]>;
|
|
95
|
+
sRandMember(key: string, count?: number): Promise<string[]>;
|
|
96
|
+
sMove(src: string, dst: string, member: string): Promise<number>;
|
|
97
|
+
sScan(key: string, cursor?: string | null, options?: {
|
|
98
|
+
count?: number;
|
|
99
|
+
}): Promise<{
|
|
100
|
+
cursor: string | null;
|
|
101
|
+
members: string[];
|
|
102
|
+
}>;
|
|
103
|
+
zAdd(key: string, ...args: Array<number | string>): Promise<number>;
|
|
104
|
+
zScore(key: string, member: string): Promise<number | null>;
|
|
105
|
+
zRange(key: string, start: number, stop: number): Promise<string[]>;
|
|
106
|
+
zRevRange(key: string, start: number, stop: number): Promise<string[]>;
|
|
107
|
+
zRangeByScore(key: string, min: number, max: number): Promise<string[]>;
|
|
108
|
+
zRank(key: string, member: string): Promise<number | null>;
|
|
109
|
+
zCount(key: string, min: number, max: number): Promise<number>;
|
|
110
|
+
zRem(key: string, ...members: string[]): Promise<number>;
|
|
111
|
+
zCard(key: string): Promise<number>;
|
|
112
|
+
zIncrBy(key: string, amount: number, member: string): Promise<number>;
|
|
113
|
+
zPopMin(key: string, count?: number): Promise<Array<{
|
|
114
|
+
member: string;
|
|
115
|
+
score: number;
|
|
116
|
+
}>>;
|
|
117
|
+
zPopMax(key: string, count?: number): Promise<Array<{
|
|
118
|
+
member: string;
|
|
119
|
+
score: number;
|
|
120
|
+
}>>;
|
|
121
|
+
zScan(key: string, cursor?: string | null, options?: {
|
|
122
|
+
count?: number;
|
|
123
|
+
}): Promise<{
|
|
124
|
+
cursor: string | null;
|
|
125
|
+
entries: Array<{
|
|
126
|
+
member: string;
|
|
127
|
+
score: number;
|
|
128
|
+
}>;
|
|
129
|
+
}>;
|
|
130
|
+
publish(channel: string, message: string): Promise<number>;
|
|
131
|
+
connect(): Promise<void>;
|
|
132
|
+
quit(): Promise<void>;
|
|
133
|
+
disconnect(): void;
|
|
134
|
+
duplicate(): RedisJsLikeAdapter;
|
|
135
|
+
on(event: string, handler: (...args: unknown[]) => void): RedisJsLikeAdapter;
|
|
136
|
+
pipeline(): PipelineFacade;
|
|
137
|
+
multi(): PipelineFacade;
|
|
138
|
+
/** 底层 noredis 客户端 */
|
|
139
|
+
readonly noredis: PgredisMigrationAliases;
|
|
140
|
+
}
|
|
141
|
+
declare function createRedisJsAdapter(options: RedisJsAdapterOptions): RedisJsLikeAdapter;
|
|
142
|
+
|
|
143
|
+
export { type RedisJsAdapterOptions, type RedisJsLikeAdapter, createRedisJsAdapter };
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createPgredisMigrationAliases
|
|
3
|
+
} from "../chunk-WRMEEA6T.js";
|
|
4
|
+
|
|
5
|
+
// src/adapters/redis.ts
|
|
6
|
+
function str(v) {
|
|
7
|
+
if (v === null || v === void 0) return null;
|
|
8
|
+
return typeof v === "string" ? v : JSON.stringify(v);
|
|
9
|
+
}
|
|
10
|
+
function strArr(vals) {
|
|
11
|
+
return vals.map((v) => typeof v === "string" ? v : JSON.stringify(v));
|
|
12
|
+
}
|
|
13
|
+
function createRedisJsAdapter(options) {
|
|
14
|
+
const aliases = createPgredisMigrationAliases(options.client);
|
|
15
|
+
const adapter = {
|
|
16
|
+
noredis: aliases,
|
|
17
|
+
// KV 基础
|
|
18
|
+
get: (key) => aliases.get(key).then(str),
|
|
19
|
+
set: (key, value, options2) => aliases.set(key, value, options2 ?? {}),
|
|
20
|
+
del: (...keys) => aliases.del(...keys),
|
|
21
|
+
unlink: (...keys) => aliases.unlink(...keys),
|
|
22
|
+
exists: (...keys) => aliases.exists(...keys),
|
|
23
|
+
type: (key) => aliases.type(key).then((t) => t === "none" ? "none" : "string"),
|
|
24
|
+
// KV 过期
|
|
25
|
+
expire: (key, seconds) => aliases.expire(key, seconds),
|
|
26
|
+
pExpire: (key, ms) => aliases.pexpire(key, ms),
|
|
27
|
+
ttl: (key) => aliases.ttl(key),
|
|
28
|
+
pTtl: (key) => aliases.pttl(key),
|
|
29
|
+
persist: (key) => aliases.persist(key),
|
|
30
|
+
// KV 条件
|
|
31
|
+
setEx: (key, seconds, value) => aliases.setex(key, seconds, value),
|
|
32
|
+
pSetEx: (key, ms, value) => aliases.psetex(key, ms, value),
|
|
33
|
+
setNx: (key, value) => aliases.setnx(key, value),
|
|
34
|
+
getSet: (key, value) => aliases.getset(key, value).then(str),
|
|
35
|
+
getDel: (key) => aliases.getdel(key).then(str),
|
|
36
|
+
// KV 批量
|
|
37
|
+
mGet: (...keys) => aliases.mget(...keys).then((vals) => vals.map(str)),
|
|
38
|
+
mSet: (kv) => aliases.mset(kv),
|
|
39
|
+
// KV 扫描
|
|
40
|
+
keys: (pattern) => aliases.keys(pattern),
|
|
41
|
+
scan: (cursor, options2) => aliases.scan(cursor, options2?.count, options2?.match),
|
|
42
|
+
rename: (key, newKey) => aliases.rename(key, newKey),
|
|
43
|
+
// 计数器
|
|
44
|
+
incr: (key) => aliases.incr(key),
|
|
45
|
+
incrBy: (key, amount) => aliases.incrby(key, amount),
|
|
46
|
+
decr: (key) => aliases.decr(key),
|
|
47
|
+
decrBy: (key, amount) => aliases.decrby(key, amount),
|
|
48
|
+
// Hash
|
|
49
|
+
hGet: (key, field) => aliases.hget(key, field).then(str),
|
|
50
|
+
hSet: (key, field, value) => aliases.hset(key, field, value),
|
|
51
|
+
hmSet: (key, kv) => aliases.hmset(key, kv),
|
|
52
|
+
hmGet: (key, ...fields) => aliases.hmget(key, ...fields).then((vals) => vals.map(str)),
|
|
53
|
+
hGetAll: (key) => aliases.hgetall(key),
|
|
54
|
+
hDel: (key, ...fields) => aliases.hdel(key, ...fields),
|
|
55
|
+
hExists: (key, field) => aliases.hexists(key, field),
|
|
56
|
+
hLen: (key) => aliases.hlen(key),
|
|
57
|
+
hIncrBy: (key, field, amount) => aliases.hincrby(key, field, amount),
|
|
58
|
+
hKeys: (key) => aliases.hkeys(key),
|
|
59
|
+
hVals: (key) => aliases.hvals(key).then(strArr),
|
|
60
|
+
hStrLen: (key, field) => aliases.hstrlen(key, field),
|
|
61
|
+
hScan: (key, cursor, options2) => aliases.hscan(key, cursor, options2?.count).then((r) => ({ cursor: r.cursor, entries: r.entries })),
|
|
62
|
+
// List
|
|
63
|
+
lPush: (key, ...values) => aliases.lpush(key, ...values),
|
|
64
|
+
rPush: (key, ...values) => aliases.rpush(key, ...values),
|
|
65
|
+
lPop: (key) => aliases.lpop(key).then(str),
|
|
66
|
+
rPop: (key) => aliases.rpop(key).then(str),
|
|
67
|
+
blPop: (keys, timeout) => aliases.blpop(keys, timeout),
|
|
68
|
+
brPop: (keys, timeout) => aliases.brpop(keys, timeout),
|
|
69
|
+
lLen: (key) => aliases.llen(key),
|
|
70
|
+
lRange: (key, start, stop) => aliases.lrange(key, start, stop).then(strArr),
|
|
71
|
+
lRem: (key, count, value) => aliases.lrem(key, count, value),
|
|
72
|
+
// Set
|
|
73
|
+
sAdd: (key, ...members) => aliases.sadd(key, ...members),
|
|
74
|
+
sRem: (key, ...members) => aliases.srem(key, ...members),
|
|
75
|
+
sMembers: (key) => aliases.smembers(key),
|
|
76
|
+
sIsMember: (key, member) => aliases.sismember(key, member),
|
|
77
|
+
sCard: (key) => aliases.scard(key),
|
|
78
|
+
sInter: (...keys) => aliases.sinter(...keys),
|
|
79
|
+
sUnion: (...keys) => aliases.sunion(...keys),
|
|
80
|
+
sDiff: (...keys) => aliases.sdiff(...keys),
|
|
81
|
+
sPop: (key, count) => aliases.spop(key, count),
|
|
82
|
+
sRandMember: (key, count) => aliases.srandmember(key, count),
|
|
83
|
+
sMove: (src, dst, member) => aliases.smove(src, dst, member),
|
|
84
|
+
sScan: (key, cursor, options2) => aliases.sscan(key, cursor, options2?.count),
|
|
85
|
+
// Sorted Set
|
|
86
|
+
zAdd: (key, ...args) => aliases.zadd(key, ...args),
|
|
87
|
+
zScore: (key, member) => aliases.zscore(key, member),
|
|
88
|
+
zRange: (key, start, stop) => aliases.zrange(key, start, stop),
|
|
89
|
+
zRevRange: (key, start, stop) => aliases.zrevrange(key, start, stop),
|
|
90
|
+
zRangeByScore: (key, min, max) => aliases.zrangeByScore(key, min, max),
|
|
91
|
+
zRank: (key, member) => aliases.zrank(key, member),
|
|
92
|
+
zCount: (key, min, max) => aliases.zcount(key, min, max),
|
|
93
|
+
zRem: (key, ...members) => aliases.zrem(key, ...members),
|
|
94
|
+
zCard: (key) => aliases.zcard(key),
|
|
95
|
+
zIncrBy: (key, amount, member) => aliases.zincrby(key, amount, member),
|
|
96
|
+
zPopMin: (key, count) => aliases.zpopmin(key, count),
|
|
97
|
+
zPopMax: (key, count) => aliases.zpopmax(key, count),
|
|
98
|
+
zScan: (key, cursor, options2) => aliases.zscan(key, cursor, options2?.count),
|
|
99
|
+
// Pub/Sub
|
|
100
|
+
publish: (channel, message) => aliases.publish(channel, message),
|
|
101
|
+
// 连接
|
|
102
|
+
connect: () => aliases.connect(),
|
|
103
|
+
quit: () => aliases.quit(),
|
|
104
|
+
disconnect: () => aliases.disconnect(),
|
|
105
|
+
duplicate: () => createRedisJsAdapter({ client: options.client }),
|
|
106
|
+
on: (event, handler) => {
|
|
107
|
+
aliases.on(event, handler);
|
|
108
|
+
return adapter;
|
|
109
|
+
},
|
|
110
|
+
// Pipeline
|
|
111
|
+
pipeline: () => aliases.pipeline(),
|
|
112
|
+
multi: () => aliases.multi()
|
|
113
|
+
};
|
|
114
|
+
return adapter;
|
|
115
|
+
}
|
|
116
|
+
export {
|
|
117
|
+
createRedisJsAdapter
|
|
118
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { PgKvSetOptions, PgKvCache } from '../kv-cache.js';
|
|
2
|
+
|
|
3
|
+
type SessionCallback<T = unknown> = (error?: unknown, value?: T | null) => void;
|
|
4
|
+
interface PgredisSessionStoreOptions {
|
|
5
|
+
prefix?: string;
|
|
6
|
+
ttlMs?: number;
|
|
7
|
+
}
|
|
8
|
+
interface PgredisCacheHelperOptions {
|
|
9
|
+
prefix?: string;
|
|
10
|
+
ttlMs?: number;
|
|
11
|
+
}
|
|
12
|
+
interface PgredisSessionStore {
|
|
13
|
+
get(sessionId: string, callback?: SessionCallback): Promise<unknown | null> | void;
|
|
14
|
+
set(sessionId: string, session: unknown, callback?: SessionCallback<void>): Promise<void> | void;
|
|
15
|
+
destroy(sessionId: string, callback?: SessionCallback<void>): Promise<void> | void;
|
|
16
|
+
touch(sessionId: string, session: unknown, callback?: SessionCallback<void>): Promise<void> | void;
|
|
17
|
+
}
|
|
18
|
+
interface PgredisCacheHelpers {
|
|
19
|
+
get<T = unknown>(key: string): Promise<T | null>;
|
|
20
|
+
set<T = unknown>(key: string, value: T, options?: PgKvSetOptions): Promise<boolean>;
|
|
21
|
+
delete(key: string): Promise<boolean>;
|
|
22
|
+
wrap<T>(key: string, loader: () => Promise<T> | T, options?: PgKvSetOptions): Promise<T>;
|
|
23
|
+
}
|
|
24
|
+
declare function createPgredisSessionStore(cache: PgKvCache, options?: PgredisSessionStoreOptions): PgredisSessionStore;
|
|
25
|
+
declare const createExpressSessionStore: typeof createPgredisSessionStore;
|
|
26
|
+
declare const createFastifySessionStore: typeof createPgredisSessionStore;
|
|
27
|
+
declare const createElysiaSessionStore: typeof createPgredisSessionStore;
|
|
28
|
+
declare function createPgredisCacheHelpers(cache: PgKvCache, options?: PgredisCacheHelperOptions): PgredisCacheHelpers;
|
|
29
|
+
|
|
30
|
+
export { type PgredisCacheHelperOptions, type PgredisCacheHelpers, type PgredisSessionStore, type PgredisSessionStoreOptions, type SessionCallback, createElysiaSessionStore, createExpressSessionStore, createFastifySessionStore, createPgredisCacheHelpers, createPgredisSessionStore };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// src/adapters/web.ts
|
|
2
|
+
function keyWithPrefix(prefix, key) {
|
|
3
|
+
return `${prefix}${key}`;
|
|
4
|
+
}
|
|
5
|
+
function withCallback(promise, callback) {
|
|
6
|
+
if (!callback) return promise;
|
|
7
|
+
promise.then((value) => callback(void 0, value)).catch((error) => callback(error));
|
|
8
|
+
}
|
|
9
|
+
function createPgredisSessionStore(cache, options = {}) {
|
|
10
|
+
const prefix = options.prefix ?? "sess:";
|
|
11
|
+
const ttlMs = options.ttlMs;
|
|
12
|
+
return {
|
|
13
|
+
get(sessionId, callback) {
|
|
14
|
+
return withCallback(cache.get(keyWithPrefix(prefix, sessionId)), callback);
|
|
15
|
+
},
|
|
16
|
+
set(sessionId, session, callback) {
|
|
17
|
+
return withCallback(
|
|
18
|
+
cache.set(keyWithPrefix(prefix, sessionId), session, { ttlMs }).then(() => void 0),
|
|
19
|
+
callback
|
|
20
|
+
);
|
|
21
|
+
},
|
|
22
|
+
destroy(sessionId, callback) {
|
|
23
|
+
return withCallback(cache.delete(keyWithPrefix(prefix, sessionId)).then(() => void 0), callback);
|
|
24
|
+
},
|
|
25
|
+
touch(sessionId, _session, callback) {
|
|
26
|
+
const key = keyWithPrefix(prefix, sessionId);
|
|
27
|
+
const promise = ttlMs === void 0 ? cache.touch(key).then(() => void 0) : cache.expire(key, ttlMs).then(() => void 0);
|
|
28
|
+
return withCallback(promise, callback);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
var createExpressSessionStore = createPgredisSessionStore;
|
|
33
|
+
var createFastifySessionStore = createPgredisSessionStore;
|
|
34
|
+
var createElysiaSessionStore = createPgredisSessionStore;
|
|
35
|
+
function createPgredisCacheHelpers(cache, options = {}) {
|
|
36
|
+
const prefix = options.prefix ?? "";
|
|
37
|
+
const ttlMs = options.ttlMs;
|
|
38
|
+
return {
|
|
39
|
+
get(key) {
|
|
40
|
+
return cache.get(keyWithPrefix(prefix, key));
|
|
41
|
+
},
|
|
42
|
+
set(key, value, setOptions = {}) {
|
|
43
|
+
return cache.set(keyWithPrefix(prefix, key), value, { ttlMs, ...setOptions });
|
|
44
|
+
},
|
|
45
|
+
delete(key) {
|
|
46
|
+
return cache.delete(keyWithPrefix(prefix, key));
|
|
47
|
+
},
|
|
48
|
+
async wrap(key, loader, setOptions = {}) {
|
|
49
|
+
const cacheKey = keyWithPrefix(prefix, key);
|
|
50
|
+
const cached = await cache.get(cacheKey);
|
|
51
|
+
if (cached !== null) return cached;
|
|
52
|
+
const value = await loader();
|
|
53
|
+
await cache.set(cacheKey, value, { ttlMs, ...setOptions });
|
|
54
|
+
return value;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
export {
|
|
59
|
+
createElysiaSessionStore,
|
|
60
|
+
createExpressSessionStore,
|
|
61
|
+
createFastifySessionStore,
|
|
62
|
+
createPgredisCacheHelpers,
|
|
63
|
+
createPgredisSessionStore
|
|
64
|
+
};
|