@hile/ioredis 1.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/README.md +37 -0
- package/SKILL.md +55 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +32 -0
- package/package.json +30 -0
- package/skill.json +16 -0
package/README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# @hile/ioredis
|
|
2
|
+
|
|
3
|
+
基于 `@hile/core` 的 Redis 集成:将 ioredis 客户端封装为 Hile 服务(单例、随进程退出断开连接)。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @hile/ioredis
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
依赖 `@hile/core` 与 `ioredis`,请一并安装。
|
|
12
|
+
|
|
13
|
+
## 快速开始
|
|
14
|
+
|
|
15
|
+
通过环境变量配置 Redis,用 `loadService` 获取客户端:
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { loadService } from '@hile/core'
|
|
19
|
+
import ioredisService from '@hile/ioredis'
|
|
20
|
+
|
|
21
|
+
const redis = await loadService(ioredisService)
|
|
22
|
+
// 使用 redis.get/set 等 ioredis API
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## 环境变量
|
|
26
|
+
|
|
27
|
+
客户端从以下环境变量读取配置:
|
|
28
|
+
|
|
29
|
+
| 变量 | 说明 |
|
|
30
|
+
|------|------|
|
|
31
|
+
| `REDIS_HOST` | Redis 主机 |
|
|
32
|
+
| `REDIS_PORT` | Redis 端口(字符串会被转为数字) |
|
|
33
|
+
| `REDIS_USERNAME` | Redis 用户名 |
|
|
34
|
+
| `REDIS_PASSWORD` | Redis 密码 |
|
|
35
|
+
| `REDIS_DB` | Redis 数据库编号,默认 `0` |
|
|
36
|
+
|
|
37
|
+
行为:服务在首次加载时创建连接并等待 `connect` 事件;进程退出时通过 Hile 的 shutdown 调用 `client.disconnect()`。
|
package/SKILL.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# @hile/ioredis
|
|
2
|
+
|
|
3
|
+
本文档是面向 AI 编码模型和人类开发者的 **代码生成规范**,阅读后应能正确地使用本库编写符合架构规则的代码。
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## 1. 架构总览
|
|
8
|
+
|
|
9
|
+
`@hile/ioredis` 在 `@hile/core` 之上提供:
|
|
10
|
+
|
|
11
|
+
- **默认导出**:一个通过 `defineService` 定义的 **ioredis Redis 客户端** 服务,配置来自环境变量;创建后等待 `connect` 事件再 resolve,进程退出时通过 `shutdown` 注册 `client.disconnect()`。
|
|
12
|
+
|
|
13
|
+
依赖:`@hile/core`、`ioredis`。生成代码时必须遵循 Hile 的服务定义与加载规则,通过 `loadService` 获取 Redis 实例并使用 ioredis 的标准 API。
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## 2. 类型与环境变量
|
|
18
|
+
|
|
19
|
+
### 2.1 环境变量(默认服务)
|
|
20
|
+
|
|
21
|
+
| 变量 | 说明 |
|
|
22
|
+
|------|------|
|
|
23
|
+
| `REDIS_HOST` | Redis 主机 |
|
|
24
|
+
| `REDIS_PORT` | Redis 端口(字符串会被转为数字) |
|
|
25
|
+
| `REDIS_USERNAME` | Redis 用户名 |
|
|
26
|
+
| `REDIS_PASSWORD` | Redis 密码 |
|
|
27
|
+
| `REDIS_DB` | Redis 数据库编号,未设置时默认为 `0` |
|
|
28
|
+
|
|
29
|
+
行为:服务在 `new Redis(options)` 后监听 `connect`,连接成功后才 resolve;`shutdown` 时调用 `client.disconnect()`。
|
|
30
|
+
|
|
31
|
+
### 2.2 类型(生成代码时须遵循)
|
|
32
|
+
|
|
33
|
+
- 默认服务:`defineService(async (shutdown) => { ... return client; })`,返回值为 ioredis 的 `Redis` 实例。
|
|
34
|
+
- 需要类型时从 `ioredis` 自行引入,例如 `import type { Redis, RedisOptions } from 'ioredis'`;本包仅导出默认服务,不 re-export 类型。
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
## 3. 代码生成模板与规则
|
|
39
|
+
|
|
40
|
+
### 3.1 使用默认服务
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
import { loadService } from '@hile/core'
|
|
44
|
+
import ioredisService from '@hile/ioredis'
|
|
45
|
+
|
|
46
|
+
const redis = await loadService(ioredisService)
|
|
47
|
+
await redis.set('key', 'value')
|
|
48
|
+
const value = await redis.get('key')
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### 3.2 规则与反模式
|
|
52
|
+
|
|
53
|
+
- 不要在本包外再包一层 `defineService` 封装同一个 Redis 连接;直接使用默认服务即可。
|
|
54
|
+
- 不要忽略环境变量:生产环境必须通过 `REDIS_*` 配置,避免硬编码。
|
|
55
|
+
- 需要扩展配置时,应在本包内扩展环境变量或 options 构造方式,保持单例由 Hile 管理。
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import Redis from 'ioredis';
|
|
2
|
+
/**
|
|
3
|
+
* 创建 Redis 服务
|
|
4
|
+
* 数据从环境变量中获取
|
|
5
|
+
* 环境变量:
|
|
6
|
+
* - REDIS_HOST: Redis 主机
|
|
7
|
+
* - REDIS_PORT: Redis 端口
|
|
8
|
+
* - REDIS_USERNAME: Redis 用户名
|
|
9
|
+
* - REDIS_PASSWORD: Redis 密码
|
|
10
|
+
* - REDIS_DB: Redis 数据库
|
|
11
|
+
*/
|
|
12
|
+
declare const _default: import("@hile/core").ServiceRegisterProps<Redis>;
|
|
13
|
+
export default _default;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { defineService } from '@hile/core';
|
|
2
|
+
import Redis from 'ioredis';
|
|
3
|
+
/**
|
|
4
|
+
* 创建 Redis 服务
|
|
5
|
+
* 数据从环境变量中获取
|
|
6
|
+
* 环境变量:
|
|
7
|
+
* - REDIS_HOST: Redis 主机
|
|
8
|
+
* - REDIS_PORT: Redis 端口
|
|
9
|
+
* - REDIS_USERNAME: Redis 用户名
|
|
10
|
+
* - REDIS_PASSWORD: Redis 密码
|
|
11
|
+
* - REDIS_DB: Redis 数据库
|
|
12
|
+
*/
|
|
13
|
+
export default defineService(async (shutdown) => {
|
|
14
|
+
const options = {
|
|
15
|
+
host: process.env.REDIS_HOST,
|
|
16
|
+
port: typeof process.env.REDIS_PORT === 'string' ? Number(process.env.REDIS_PORT) : process.env.REDIS_PORT,
|
|
17
|
+
username: process.env.REDIS_USERNAME,
|
|
18
|
+
password: process.env.REDIS_PASSWORD,
|
|
19
|
+
db: typeof process.env.REDIS_DB === 'string' ? Number(process.env.REDIS_DB) : process.env.REDIS_DB || 0,
|
|
20
|
+
};
|
|
21
|
+
const client = new Redis(options);
|
|
22
|
+
await new Promise((resolve, reject) => {
|
|
23
|
+
const onerror = (e) => reject(e);
|
|
24
|
+
client.on('error', onerror);
|
|
25
|
+
client.on('connect', () => {
|
|
26
|
+
client.off('error', onerror);
|
|
27
|
+
resolve();
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
shutdown(() => client.disconnect());
|
|
31
|
+
return client;
|
|
32
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hile/ioredis",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc -b",
|
|
9
|
+
"dev": "tsc -b --watch",
|
|
10
|
+
"test": "vitest run"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist",
|
|
14
|
+
"README.md",
|
|
15
|
+
"SKILL.md",
|
|
16
|
+
"skill.json"
|
|
17
|
+
],
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"vitest": "^4.0.18"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@hile/core": "1.0.11",
|
|
27
|
+
"ioredis": "^5.10.0"
|
|
28
|
+
},
|
|
29
|
+
"gitHead": "618a63b619956a0c1db4d77dece84a7efe18a11e"
|
|
30
|
+
}
|
package/skill.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hile/ioredis",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Redis client as Hile service (@hile/ioredis); code generation and contribution rules",
|
|
5
|
+
"repository": "https://github.com/cevio/hile/tree/main/packages/ioredis",
|
|
6
|
+
"skills": [
|
|
7
|
+
{
|
|
8
|
+
"id": "ioredis",
|
|
9
|
+
"name": "@hile/ioredis 开发指南",
|
|
10
|
+
"description": "提供 @hile/ioredis 模块的开发与代码生成规范",
|
|
11
|
+
"path": "SKILL.md"
|
|
12
|
+
}
|
|
13
|
+
],
|
|
14
|
+
"authors": [],
|
|
15
|
+
"license": "MIT"
|
|
16
|
+
}
|