@quark-fw/plugin-redis 0.1.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/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@quark-fw/plugin-redis",
3
+ "version": "0.1.2",
4
+ "main": "src/index.ts",
5
+ "types": "src/index.ts",
6
+ "type": "module",
7
+ "scripts": {
8
+ "dev": "tsx watch src/index.ts",
9
+ "build": "echo raw-src package, no build"
10
+ },
11
+ "files": [
12
+ "src"
13
+ ],
14
+ "keywords": [
15
+ "quark"
16
+ ],
17
+ "author": "",
18
+ "license": "ISC",
19
+ "description": "Redis (ioredis) plugin for the Quark framework (published as raw TS sources; consumers run tsx)",
20
+ "publishConfig": {
21
+ "access": "public"
22
+ },
23
+ "dependencies": {
24
+ "@quark-fw/atom": "0.1.2",
25
+ "@quark-fw/plugin": "0.1.2",
26
+ "ioredis": "^5.9.3"
27
+ },
28
+ "peerDependencies": {
29
+ "@quark-fw/core": "0.1.2"
30
+ }
31
+ }
@@ -0,0 +1,16 @@
1
+ import "@quark-fw/core/src/global.d.ts";
2
+ import { Atom } from "@quark-fw/atom";
3
+ import { Redis } from "ioredis";
4
+
5
+ export type RedisConfig = {
6
+ url: string;
7
+ };
8
+
9
+ declare global {
10
+ interface Quark {
11
+ redis: {
12
+ config: Atom<RedisConfig>;
13
+ client: Atom<Redis>;
14
+ };
15
+ }
16
+ }
package/src/index.ts ADDED
@@ -0,0 +1,43 @@
1
+ import atom from "@quark-fw/atom";
2
+ import { QuarkPlugin } from "@quark-fw/plugin";
3
+ import { RedisConfig } from "@quark-fw/plugin-redis/src/global.js";
4
+ import { Redis } from "ioredis";
5
+
6
+ function createClient(config: RedisConfig) {
7
+ return new Redis(config.url, {
8
+ maxRetriesPerRequest: null,
9
+ family: 4,
10
+ // не подключаемся, пока клиент реально не используется
11
+ lazyConnect: true,
12
+ retryStrategy: (times) => Math.min(times * 200, 2000),
13
+ });
14
+ }
15
+
16
+ await quark.registerPlugin({
17
+ name: "redis",
18
+ extend(quark) {
19
+ // в проде адрес приходит из окружения (в Coolify — внутренний URL
20
+ // сервиса), локально работает дефолт
21
+ const config = atom<RedisConfig>("redis.config", {
22
+ url: process.env.REDIS_URL || "redis://127.0.0.1:6379",
23
+ });
24
+
25
+ const client = atom("redis.client", createClient(config.value));
26
+
27
+ // при смене конфига создаём новый клиент и закрываем старый
28
+ config.subscribe((cfg) => {
29
+ const old = client.value;
30
+ client.value = createClient(cfg);
31
+ void old.quit().catch(() => old.disconnect());
32
+ });
33
+
34
+ quark.redis = {
35
+ client,
36
+ config,
37
+ };
38
+ },
39
+ async onDestroy(quark) {
40
+ const client = quark.redis.client.value;
41
+ await client.quit().catch(() => client.disconnect());
42
+ },
43
+ } satisfies QuarkPlugin);