@hile/ioredis 1.1.2 → 2.0.1

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/dist/index.d.ts CHANGED
@@ -1,13 +1,19 @@
1
- import Redis from 'ioredis';
1
+ import Redis, { RedisOptions } from 'ioredis';
2
+ export type { RedisOptions } from 'ioredis';
2
3
  /**
3
- * 创建 Redis 服务
4
- * 数据从环境变量中获取
4
+ * 创建 Redis 客户端(手动模式)
5
+ * @param options Redis 连接选项,不传则从环境变量读取
6
+ * @returns 等待连接就绪后的 Redis 实例
7
+ */
8
+ export declare function createRedis(options?: RedisOptions): Promise<Redis>;
9
+ /**
10
+ * Redis 服务(容器模式)
5
11
  * 环境变量:
6
- * - REDIS_HOST: Redis 主机
7
- * - REDIS_PORT: Redis 端口
8
- * - REDIS_USERNAME: Redis 用户名
9
- * - REDIS_PASSWORD: Redis 密码
10
- * - REDIS_DB: Redis 数据库
12
+ * - REDIS_HOST
13
+ * - REDIS_PORT
14
+ * - REDIS_USERNAME
15
+ * - REDIS_PASSWORD
16
+ * - REDIS_DB
11
17
  */
12
18
  declare const _default: import("@hile/core").ServiceRegisterProps<Redis>;
13
19
  export default _default;
package/dist/index.js CHANGED
@@ -2,24 +2,24 @@ import pkg from '../package.json' with { type: 'json' };
2
2
  import { defineService } from '@hile/core';
3
3
  import Redis from 'ioredis';
4
4
  /**
5
- * 创建 Redis 服务
6
- * 数据从环境变量中获取
7
- * 环境变量:
8
- * - REDIS_HOST: Redis 主机
9
- * - REDIS_PORT: Redis 端口
10
- * - REDIS_USERNAME: Redis 用户名
11
- * - REDIS_PASSWORD: Redis 密码
12
- * - REDIS_DB: Redis 数据库
5
+ * 从环境变量读取 Redis 配置
13
6
  */
14
- export default defineService(Symbol.for(pkg.name), async (shutdown) => {
15
- const options = {
7
+ function envOptions() {
8
+ return {
16
9
  host: process.env.REDIS_HOST,
17
10
  port: typeof process.env.REDIS_PORT === 'string' ? Number(process.env.REDIS_PORT) : process.env.REDIS_PORT,
18
11
  username: process.env.REDIS_USERNAME,
19
12
  password: process.env.REDIS_PASSWORD,
20
13
  db: typeof process.env.REDIS_DB === 'string' ? Number(process.env.REDIS_DB) : process.env.REDIS_DB || 0,
21
14
  };
22
- const client = new Redis(options);
15
+ }
16
+ /**
17
+ * 创建 Redis 客户端(手动模式)
18
+ * @param options Redis 连接选项,不传则从环境变量读取
19
+ * @returns 等待连接就绪后的 Redis 实例
20
+ */
21
+ export async function createRedis(options) {
22
+ const client = new Redis(options ?? envOptions());
23
23
  await new Promise((resolve, reject) => {
24
24
  const onerror = (e) => reject(e);
25
25
  client.on('error', onerror);
@@ -28,6 +28,19 @@ export default defineService(Symbol.for(pkg.name), async (shutdown) => {
28
28
  resolve();
29
29
  });
30
30
  });
31
+ return client;
32
+ }
33
+ /**
34
+ * Redis 服务(容器模式)
35
+ * 环境变量:
36
+ * - REDIS_HOST
37
+ * - REDIS_PORT
38
+ * - REDIS_USERNAME
39
+ * - REDIS_PASSWORD
40
+ * - REDIS_DB
41
+ */
42
+ export default defineService(Symbol.for(pkg.name), async (shutdown) => {
43
+ const client = await createRedis();
31
44
  shutdown(() => client.disconnect());
32
45
  return client;
33
46
  });
package/package.json CHANGED
@@ -1,16 +1,16 @@
1
1
  {
2
2
  "name": "@hile/ioredis",
3
- "version": "1.1.2",
3
+ "version": "2.0.1",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "scripts": {
7
7
  "build": "tsc -b && fix-esm-import-path --preserve-import-type ./dist",
8
- "dev": "tsc -b --watch"
8
+ "dev": "tsc -b --watch",
9
+ "test": "vitest run"
9
10
  },
10
11
  "files": [
11
12
  "dist",
12
- "README.md",
13
- "SKILL.md"
13
+ "README.md"
14
14
  ],
15
15
  "license": "MIT",
16
16
  "publishConfig": {
@@ -21,8 +21,8 @@
21
21
  "vitest": "^4.0.18"
22
22
  },
23
23
  "dependencies": {
24
- "@hile/core": "^1.1.2",
24
+ "@hile/core": "^2.0.1",
25
25
  "ioredis": "^5.10.0"
26
26
  },
27
- "gitHead": "439a7eb90a233d466f97df932782d74dd64a50f2"
27
+ "gitHead": "2c8011db01f2815e5ce34de964d5492640396828"
28
28
  }
package/SKILL.md DELETED
@@ -1,54 +0,0 @@
1
- ---
2
- name: hile-ioredis
3
- description: "@hile/ioredis 的代码生成与使用规范。适用于 Redis 服务加载、环境变量配置、及与 @hile/core/@hile/cli 集成场景。"
4
- ---
5
-
6
- # @hile/ioredis SKILL
7
-
8
- 本文档规范 `@hile/ioredis` 的使用方式,确保 Redis 客户端生命周期由 Hile 统一管理。
9
-
10
- ## 1. 架构概览
11
-
12
- `@hile/ioredis` 提供一个默认导出的 Hile 服务(服务 key 为稳定 symbol,见包内实现):
13
-
14
- - 使用环境变量构建 ioredis 客户端
15
- - 首次加载时建立连接并等待 `connect`
16
- - 进程退出时调用 `client.disconnect()`
17
-
18
- 依赖:`@hile/core`、`ioredis`。
19
-
20
- ## 2. 环境变量
21
-
22
- | 变量 | 说明 |
23
- |---|---|
24
- | `REDIS_HOST` | Redis 主机 |
25
- | `REDIS_PORT` | Redis 端口(字符串转数字) |
26
- | `REDIS_USERNAME` | Redis 用户名 |
27
- | `REDIS_PASSWORD` | Redis 密码 |
28
- | `REDIS_DB` | Redis 数据库编号(默认 `0`) |
29
-
30
- ## 3. 标准模板
31
-
32
- ### 3.1 加载默认服务
33
-
34
- ```typescript
35
- import { loadService } from '@hile/core'
36
- import ioredisService from '@hile/ioredis'
37
-
38
- const redis = await loadService(ioredisService)
39
- await redis.set('key', 'value')
40
- const value = await redis.get('key')
41
- ```
42
-
43
- ## 4. 强制规则
44
-
45
- 1. 统一通过 `loadService(ioredisService)` 获取 Redis 客户端。
46
- 2. 不要再额外封装一个同用途的全局 Redis 单例与本包混用。
47
- 3. 生产环境应通过 `REDIS_*` 变量配置,避免硬编码。
48
- 4. 依赖 Redis 的服务应在服务函数内部加载,不在模块顶层缓存实例。
49
-
50
- ## 5. API 速查
51
-
52
- | 导出 | 说明 |
53
- |---|---|
54
- | 默认导出 | Hile 服务化 Redis 客户端 |