befly 3.0.0 → 3.1.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/checks/conflict.ts +35 -114
- package/checks/table.ts +31 -63
- package/config/env.ts +3 -3
- package/config/fields.ts +55 -0
- package/config/regexAliases.ts +51 -0
- package/config/reserved.ts +1 -1
- package/main.ts +17 -71
- package/package.json +7 -28
- package/plugins/db.ts +11 -10
- package/plugins/redis.ts +5 -9
- package/scripts/syncDb/apply.ts +3 -3
- package/scripts/syncDb/constants.ts +2 -1
- package/scripts/syncDb/ddl.ts +15 -8
- package/scripts/syncDb/helpers.ts +3 -2
- package/scripts/syncDb/index.ts +23 -35
- package/scripts/syncDb/state.ts +8 -6
- package/scripts/syncDb/table.ts +32 -22
- package/scripts/syncDb/tableCreate.ts +9 -3
- package/scripts/syncDb/tests/constants.test.ts +2 -1
- package/scripts/syncDb.ts +10 -9
- package/types/addon.d.ts +53 -0
- package/types/api.d.ts +17 -14
- package/types/befly.d.ts +2 -6
- package/types/context.d.ts +7 -0
- package/types/database.d.ts +9 -14
- package/types/index.d.ts +442 -8
- package/types/index.ts +35 -56
- package/types/redis.d.ts +2 -0
- package/types/validator.d.ts +0 -2
- package/types/validator.ts +43 -0
- package/utils/colors.ts +117 -37
- package/utils/database.ts +348 -0
- package/utils/dbHelper.ts +687 -116
- package/utils/helper.ts +812 -0
- package/utils/index.ts +10 -23
- package/utils/logger.ts +78 -171
- package/utils/redisHelper.ts +135 -152
- package/{types/context.ts → utils/requestContext.ts} +3 -3
- package/utils/sqlBuilder.ts +142 -165
- package/utils/validate.ts +51 -9
- package/apis/health/info.ts +0 -64
- package/apis/tool/tokenCheck.ts +0 -51
- package/bin/befly.ts +0 -202
- package/bunfig.toml +0 -3
- package/plugins/tool.ts +0 -34
- package/scripts/syncDev.ts +0 -112
- package/system.ts +0 -149
- package/tables/_common.json +0 -21
- package/tables/admin.json +0 -10
- package/utils/addonHelper.ts +0 -60
- package/utils/api.ts +0 -23
- package/utils/datetime.ts +0 -51
- package/utils/errorHandler.ts +0 -68
- package/utils/objectHelper.ts +0 -68
- package/utils/pluginHelper.ts +0 -62
- package/utils/response.ts +0 -38
- package/utils/sqlHelper.ts +0 -447
- package/utils/tableHelper.ts +0 -167
- package/utils/tool.ts +0 -230
- package/utils/typeHelper.ts +0 -101
package/types/index.d.ts
CHANGED
|
@@ -1,16 +1,450 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Befly
|
|
3
|
-
*
|
|
2
|
+
* Befly 核心类型定义
|
|
3
|
+
* 统一导出所有类型,简化导入路径
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```typescript
|
|
7
|
+
* import type { ApiRoute, BeflyContext, Plugin } from 'befly/types';
|
|
8
|
+
* ```
|
|
4
9
|
*/
|
|
5
10
|
|
|
6
|
-
//
|
|
11
|
+
// 基础类型
|
|
7
12
|
export * from './common.js';
|
|
13
|
+
export * from './context.js';
|
|
8
14
|
|
|
9
|
-
//
|
|
15
|
+
// 核心功能类型
|
|
16
|
+
export * from './api.js';
|
|
17
|
+
export * from './befly.js';
|
|
10
18
|
export * from './plugin.js';
|
|
11
19
|
|
|
12
|
-
//
|
|
13
|
-
export * from './
|
|
20
|
+
// 功能模块类型
|
|
21
|
+
export * from './database.js';
|
|
22
|
+
export * from './crypto.js';
|
|
23
|
+
export * from './jwt.js';
|
|
24
|
+
export * from './validator.js';
|
|
25
|
+
export * from './redis.js';
|
|
26
|
+
export * from './logger.js';
|
|
14
27
|
|
|
15
|
-
|
|
16
|
-
|
|
28
|
+
/**
|
|
29
|
+
* API 请求上下文
|
|
30
|
+
*/
|
|
31
|
+
export interface RequestContext {
|
|
32
|
+
/** 原始请求对象 */
|
|
33
|
+
req: Request;
|
|
34
|
+
/** 解析后的 URL */
|
|
35
|
+
url: URL;
|
|
36
|
+
/** 路径名 */
|
|
37
|
+
pathname: string;
|
|
38
|
+
/** 查询参数 */
|
|
39
|
+
query: URLSearchParams;
|
|
40
|
+
/** 请求体数据 */
|
|
41
|
+
body: any;
|
|
42
|
+
/** 请求头 */
|
|
43
|
+
headers: Headers;
|
|
44
|
+
/** 路由参数 */
|
|
45
|
+
params?: Record<string, string>;
|
|
46
|
+
/** JWT 载荷(认证后) */
|
|
47
|
+
jwt?: JwtPayload;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* API 响应格式
|
|
52
|
+
*/
|
|
53
|
+
export interface ApiResponse<T = any> {
|
|
54
|
+
/** 状态码:0=成功,1=失败 */
|
|
55
|
+
code: 0 | 1;
|
|
56
|
+
/** 提示信息 */
|
|
57
|
+
msg: string;
|
|
58
|
+
/** 响应数据 */
|
|
59
|
+
data: T;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* API 处理函数
|
|
64
|
+
*/
|
|
65
|
+
export type ApiHandler = (befly: BeflyContext, ctx: RequestContext, req: Request) => Promise<ApiResponse>;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* API 路由定义
|
|
69
|
+
*/
|
|
70
|
+
export interface ApiRoute {
|
|
71
|
+
/** HTTP 方法 */
|
|
72
|
+
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS';
|
|
73
|
+
/** 接口名称 */
|
|
74
|
+
name: string;
|
|
75
|
+
/** 认证要求:false=公开,true=需登录,['admin']=需特定角色 */
|
|
76
|
+
auth: boolean | string[];
|
|
77
|
+
/** 字段规则定义 */
|
|
78
|
+
fields: Record<string, string>;
|
|
79
|
+
/** 必填字段列表 */
|
|
80
|
+
required: string[];
|
|
81
|
+
/** 处理函数 */
|
|
82
|
+
handler: ApiHandler;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* JWT 载荷
|
|
87
|
+
*/
|
|
88
|
+
export interface JwtPayload {
|
|
89
|
+
/** 用户 ID */
|
|
90
|
+
userId?: string;
|
|
91
|
+
/** 用户名 */
|
|
92
|
+
username?: string;
|
|
93
|
+
/** 角色 */
|
|
94
|
+
role?: string;
|
|
95
|
+
/** 权限列表 */
|
|
96
|
+
permissions?: string[];
|
|
97
|
+
/** 签发时间 */
|
|
98
|
+
iat?: number;
|
|
99
|
+
/** 过期时间 */
|
|
100
|
+
exp?: number;
|
|
101
|
+
/** 主题 */
|
|
102
|
+
sub?: string;
|
|
103
|
+
/** 签发者 */
|
|
104
|
+
iss?: string;
|
|
105
|
+
/** 受众 */
|
|
106
|
+
aud?: string;
|
|
107
|
+
/** JWT ID */
|
|
108
|
+
jti?: string;
|
|
109
|
+
/** 生效时间 */
|
|
110
|
+
nbf?: number;
|
|
111
|
+
/** 其他自定义字段 */
|
|
112
|
+
[key: string]: any;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* JWT 签名选项
|
|
117
|
+
*/
|
|
118
|
+
export interface JwtSignOptions {
|
|
119
|
+
/** 密钥 */
|
|
120
|
+
secret?: string;
|
|
121
|
+
/** 算法 */
|
|
122
|
+
algorithm?: string;
|
|
123
|
+
/** 过期时间 */
|
|
124
|
+
expiresIn?: string;
|
|
125
|
+
/** 签发者 */
|
|
126
|
+
issuer?: string;
|
|
127
|
+
/** 受众 */
|
|
128
|
+
audience?: string;
|
|
129
|
+
/** 主题 */
|
|
130
|
+
subject?: string;
|
|
131
|
+
/** 生效时间 */
|
|
132
|
+
notBefore?: string | number;
|
|
133
|
+
/** JWT ID */
|
|
134
|
+
jwtId?: string;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Befly 上下文接口
|
|
139
|
+
*/
|
|
140
|
+
export interface BeflyContext {
|
|
141
|
+
/** 数据库管理器 */
|
|
142
|
+
db: DbHelper;
|
|
143
|
+
/** Redis 助手 */
|
|
144
|
+
redis: typeof RedisHelper;
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* 插件定义
|
|
148
|
+
*/
|
|
149
|
+
export interface Plugin {
|
|
150
|
+
/** 插件名称 */
|
|
151
|
+
name?: string;
|
|
152
|
+
/** 依赖的插件列表 */
|
|
153
|
+
after?: string[];
|
|
154
|
+
/** 初始化函数 */
|
|
155
|
+
onInit?: (befly: BeflyContext) => Promise<any>;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* 服务器配置
|
|
160
|
+
*/
|
|
161
|
+
export interface ServerConfig {
|
|
162
|
+
/** 服务名称 */
|
|
163
|
+
name: string;
|
|
164
|
+
/** 端口号 */
|
|
165
|
+
port: number;
|
|
166
|
+
/** 主机 */
|
|
167
|
+
host?: string;
|
|
168
|
+
/** 静态文件目录 */
|
|
169
|
+
static?: string;
|
|
170
|
+
/** 自定义插件 */
|
|
171
|
+
plugins?: Plugin[];
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* SQL 查询操作符
|
|
176
|
+
*/
|
|
177
|
+
export interface WhereOperator {
|
|
178
|
+
/** 大于 */
|
|
179
|
+
$gt?: number;
|
|
180
|
+
/** 大于等于 */
|
|
181
|
+
$gte?: number;
|
|
182
|
+
/** 小于 */
|
|
183
|
+
$lt?: number;
|
|
184
|
+
/** 小于等于 */
|
|
185
|
+
$lte?: number;
|
|
186
|
+
/** 不等于 */
|
|
187
|
+
$ne?: any;
|
|
188
|
+
/** 不等于($ne 的别名) */
|
|
189
|
+
$not?: any;
|
|
190
|
+
/** 在...之中 */
|
|
191
|
+
$in?: any[];
|
|
192
|
+
/** 不在...之中 */
|
|
193
|
+
$nin?: any[];
|
|
194
|
+
/** 不在...之中($nin 的别名) */
|
|
195
|
+
$notIn?: any[];
|
|
196
|
+
/** LIKE 模糊匹配 */
|
|
197
|
+
$like?: string;
|
|
198
|
+
/** NOT LIKE 模糊匹配 */
|
|
199
|
+
$notLike?: string;
|
|
200
|
+
/** BETWEEN...AND 范围查询 */
|
|
201
|
+
$between?: [any, any];
|
|
202
|
+
/** NOT BETWEEN...AND 范围查询 */
|
|
203
|
+
$notBetween?: [any, any];
|
|
204
|
+
/** IS NULL 空值检查 */
|
|
205
|
+
$null?: boolean;
|
|
206
|
+
/** IS NOT NULL 非空检查 */
|
|
207
|
+
$notNull?: boolean;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* WHERE 条件
|
|
212
|
+
* 支持两种格式:
|
|
213
|
+
* 1. 嵌套格式:{ age: { $gt: 18 } }
|
|
214
|
+
* 2. 一级属性格式:{ 'age$gt': 18 }(推荐)
|
|
215
|
+
*/
|
|
216
|
+
export type WhereConditions = {
|
|
217
|
+
/** 普通字段条件(支持一级属性格式:'field$operator') */
|
|
218
|
+
[field: string]:
|
|
219
|
+
| string
|
|
220
|
+
| number
|
|
221
|
+
| boolean
|
|
222
|
+
| null
|
|
223
|
+
| any[] // 支持 $in、$nin 的数组值
|
|
224
|
+
| WhereOperator // 支持嵌套格式
|
|
225
|
+
| WhereConditions[]; // 支持 $or、$and 的数组值
|
|
226
|
+
|
|
227
|
+
/** OR 逻辑操作符 */
|
|
228
|
+
$or?: WhereConditions[];
|
|
229
|
+
|
|
230
|
+
/** AND 逻辑操作符 */
|
|
231
|
+
$and?: WhereConditions[];
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* SQL 查询选项
|
|
236
|
+
*/
|
|
237
|
+
export interface QueryOptions {
|
|
238
|
+
/** 表名 */
|
|
239
|
+
table: string;
|
|
240
|
+
/** 查询字段,默认 ['*'] */
|
|
241
|
+
fields?: string[];
|
|
242
|
+
/** WHERE 条件 */
|
|
243
|
+
where?: WhereConditions;
|
|
244
|
+
/** 排序(格式:["字段#ASC", "字段#DESC"]) */
|
|
245
|
+
orderBy?: string[];
|
|
246
|
+
/** 页码(从 1 开始) */
|
|
247
|
+
page?: number;
|
|
248
|
+
/** 每页数量 */
|
|
249
|
+
limit?: number;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* 插入选项
|
|
254
|
+
*/
|
|
255
|
+
export interface InsertOptions {
|
|
256
|
+
/** 表名 */
|
|
257
|
+
table: string;
|
|
258
|
+
/** 插入数据(ID、时间戳、state 会自动生成) */
|
|
259
|
+
data: Record<string, any>;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* 更新选项
|
|
264
|
+
*/
|
|
265
|
+
export interface UpdateOptions {
|
|
266
|
+
/** 表名 */
|
|
267
|
+
table: string;
|
|
268
|
+
/** 更新数据(updated_at 会自动更新) */
|
|
269
|
+
data: Record<string, any>;
|
|
270
|
+
/** WHERE 条件 */
|
|
271
|
+
where: WhereConditions;
|
|
272
|
+
/** 是否包含已删除数据,默认 false */
|
|
273
|
+
includeDeleted?: boolean;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* 删除选项
|
|
278
|
+
*/
|
|
279
|
+
export interface DeleteOptions {
|
|
280
|
+
/** 表名 */
|
|
281
|
+
table: string;
|
|
282
|
+
/** WHERE 条件 */
|
|
283
|
+
where: WhereConditions;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* 列表查询结果
|
|
288
|
+
*/
|
|
289
|
+
export interface ListResult<T = any> {
|
|
290
|
+
/** 数据列表 */
|
|
291
|
+
list: T[];
|
|
292
|
+
/** 总条数 */
|
|
293
|
+
total: number;
|
|
294
|
+
/** 当前页码 */
|
|
295
|
+
page: number;
|
|
296
|
+
/** 每页数量 */
|
|
297
|
+
limit: number;
|
|
298
|
+
/** 总页数 */
|
|
299
|
+
pages: number;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* 事务回调函数
|
|
304
|
+
*/
|
|
305
|
+
export type TransactionCallback<T = any> = (trans: DbHelper) => Promise<T>;
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* SQL 查询对象
|
|
309
|
+
*/
|
|
310
|
+
export interface SqlQuery {
|
|
311
|
+
/** SQL 语句 */
|
|
312
|
+
sql: string;
|
|
313
|
+
/** 参数列表 */
|
|
314
|
+
params: any[];
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* 数据库管理器接口
|
|
319
|
+
*/
|
|
320
|
+
export interface DbHelper {
|
|
321
|
+
/** 查询单条数据 */
|
|
322
|
+
getOne<T = any>(options: QueryOptions): Promise<T | null>;
|
|
323
|
+
/** 查询列表(带分页) */
|
|
324
|
+
getList<T = any>(options: QueryOptions): Promise<ListResult<T>>;
|
|
325
|
+
/** 查询所有数据 */
|
|
326
|
+
getAll<T = any>(options: Omit<QueryOptions, 'page' | 'limit'>): Promise<T[]>;
|
|
327
|
+
/** 插入数据 */
|
|
328
|
+
insData(options: InsertOptions): Promise<number>;
|
|
329
|
+
/** 更新数据 */
|
|
330
|
+
updData(options: UpdateOptions): Promise<number>;
|
|
331
|
+
/** 删除数据 */
|
|
332
|
+
delData(options: DeleteOptions): Promise<number>;
|
|
333
|
+
/** 执行事务 */
|
|
334
|
+
trans<T = any>(callback: TransactionCallback<T>): Promise<T>;
|
|
335
|
+
/** 执行原始 SQL */
|
|
336
|
+
query(sql: string, params?: any[]): Promise<any>;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Redis 助手接口
|
|
341
|
+
*/
|
|
342
|
+
export interface RedisHelper {
|
|
343
|
+
/** 设置对象到 Redis */
|
|
344
|
+
setObject<T = any>(key: string, obj: T, ttl?: number | null): Promise<string | null>;
|
|
345
|
+
/** 从 Redis 获取对象 */
|
|
346
|
+
getObject<T = any>(key: string): Promise<T | null>;
|
|
347
|
+
/** 从 Redis 删除对象 */
|
|
348
|
+
delObject(key: string): Promise<void>;
|
|
349
|
+
/** 生成基于时间的唯一 ID */
|
|
350
|
+
genTimeID(): Promise<number>;
|
|
351
|
+
/** 设置字符串值 */
|
|
352
|
+
setString(key: string, value: string, ttl?: number | null): Promise<string | null>;
|
|
353
|
+
/** 获取字符串值 */
|
|
354
|
+
getString(key: string): Promise<string | null>;
|
|
355
|
+
/** 检查键是否存在 */
|
|
356
|
+
exists(key: string): Promise<number>;
|
|
357
|
+
/** 设置过期时间 */
|
|
358
|
+
expire(key: string, seconds: number): Promise<number>;
|
|
359
|
+
/** 获取剩余过期时间 */
|
|
360
|
+
ttl(key: string): Promise<number>;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* 日志记录器接口
|
|
365
|
+
*/
|
|
366
|
+
export interface Logger {
|
|
367
|
+
/** 记录信息日志 */
|
|
368
|
+
info(message: string | object): void;
|
|
369
|
+
/** 记录警告日志 */
|
|
370
|
+
warn(message: string | object): void;
|
|
371
|
+
/** 记录错误日志 */
|
|
372
|
+
error(message: string | object): void;
|
|
373
|
+
/** 记录调试日志 */
|
|
374
|
+
debug(message: string | object): void;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* 工具类接口
|
|
379
|
+
*/
|
|
380
|
+
export interface Tool {
|
|
381
|
+
/** 处理更新数据 */
|
|
382
|
+
updData(data: Record<string, any>, now?: number): Promise<Record<string, any>>;
|
|
383
|
+
/** 处理插入数据 */
|
|
384
|
+
insData(data: Record<string, any> | Record<string, any>[], now?: number): Promise<Record<string, any> | Record<string, any>[]>;
|
|
385
|
+
/** 处理删除数据 */
|
|
386
|
+
delData(now?: number): Promise<Record<string, any>>;
|
|
387
|
+
/** 批量生成 ID */
|
|
388
|
+
genIds(count: number): Promise<number[]>;
|
|
389
|
+
/** 清理数据对象 */
|
|
390
|
+
cleanData(data: Record<string, any>, removeNull?: boolean, removeEmptyString?: boolean): Record<string, any>;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* 验证结果
|
|
395
|
+
*/
|
|
396
|
+
export interface ValidationResult {
|
|
397
|
+
/** 是否有效 */
|
|
398
|
+
valid: boolean;
|
|
399
|
+
/** 处理后的值 */
|
|
400
|
+
value: any;
|
|
401
|
+
/** 错误列表 */
|
|
402
|
+
errors: string[];
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* 数据库类型
|
|
407
|
+
*/
|
|
408
|
+
export type DbType = 'mysql' | 'postgresql' | 'sqlite';
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* 列信息
|
|
412
|
+
*/
|
|
413
|
+
export interface ColumnInfo {
|
|
414
|
+
/** 列名 */
|
|
415
|
+
name: string;
|
|
416
|
+
/** 数据类型 */
|
|
417
|
+
type: string;
|
|
418
|
+
/** 是否可空 */
|
|
419
|
+
nullable: boolean;
|
|
420
|
+
/** 默认值 */
|
|
421
|
+
default: any;
|
|
422
|
+
/** 注释 */
|
|
423
|
+
comment: string;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* 索引信息
|
|
428
|
+
*/
|
|
429
|
+
export interface IndexInfo {
|
|
430
|
+
/** 索引名 */
|
|
431
|
+
name: string;
|
|
432
|
+
/** 列名 */
|
|
433
|
+
column: string;
|
|
434
|
+
/** 是否唯一 */
|
|
435
|
+
unique: boolean;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* 同步统计
|
|
440
|
+
*/
|
|
441
|
+
export interface SyncStats {
|
|
442
|
+
/** 创建的表数 */
|
|
443
|
+
created: number;
|
|
444
|
+
/** 修改的表数 */
|
|
445
|
+
modified: number;
|
|
446
|
+
/** 创建的索引数 */
|
|
447
|
+
indexesCreated: number;
|
|
448
|
+
/** 删除的索引数 */
|
|
449
|
+
indexesDropped: number;
|
|
450
|
+
}
|
package/types/index.ts
CHANGED
|
@@ -1,24 +1,30 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Befly 核心类型定义
|
|
3
|
+
* 统一导出所有类型,简化导入路径
|
|
3
4
|
*
|
|
4
|
-
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```typescript
|
|
7
|
+
* import type { ApiRoute, BeflyContext, Plugin } from 'befly/types';
|
|
8
|
+
* ```
|
|
5
9
|
*/
|
|
6
10
|
|
|
7
|
-
//
|
|
8
|
-
export * from './common';
|
|
9
|
-
export * from './
|
|
10
|
-
export * from './befly';
|
|
11
|
-
export * from './plugin';
|
|
12
|
-
export * from './context';
|
|
11
|
+
// 基础类型
|
|
12
|
+
export * from './common.js';
|
|
13
|
+
export * from './context.js';
|
|
13
14
|
|
|
14
|
-
//
|
|
15
|
-
export * from './
|
|
16
|
-
export * from './
|
|
17
|
-
export * from './
|
|
18
|
-
export * from './
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
export * from './
|
|
15
|
+
// 核心功能类型
|
|
16
|
+
export * from './api.js';
|
|
17
|
+
export * from './befly.js';
|
|
18
|
+
export * from './plugin.js';
|
|
19
|
+
export * from './addon.js';
|
|
20
|
+
|
|
21
|
+
// 功能模块类型
|
|
22
|
+
export * from './database.js';
|
|
23
|
+
export * from './crypto.js';
|
|
24
|
+
export * from './jwt.js';
|
|
25
|
+
export * from './validator.js';
|
|
26
|
+
export * from './redis.js';
|
|
27
|
+
export * from './logger.js';
|
|
22
28
|
|
|
23
29
|
/**
|
|
24
30
|
* API 请求上下文
|
|
@@ -67,8 +73,8 @@ export interface ApiRoute {
|
|
|
67
73
|
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS';
|
|
68
74
|
/** 接口名称 */
|
|
69
75
|
name: string;
|
|
70
|
-
/**
|
|
71
|
-
auth: boolean
|
|
76
|
+
/** 认证要求: false=公开,true=需登录 */
|
|
77
|
+
auth: boolean;
|
|
72
78
|
/** 字段规则定义 */
|
|
73
79
|
fields: Record<string, string>;
|
|
74
80
|
/** 必填字段列表 */
|
|
@@ -130,20 +136,13 @@ export interface JwtSignOptions {
|
|
|
130
136
|
}
|
|
131
137
|
|
|
132
138
|
/**
|
|
133
|
-
* Befly
|
|
134
|
-
* 包含所有插件提供的功能
|
|
139
|
+
* Befly 上下文接口
|
|
135
140
|
*/
|
|
136
141
|
export interface BeflyContext {
|
|
137
142
|
/** 数据库管理器 */
|
|
138
|
-
db:
|
|
143
|
+
db: DbHelper;
|
|
139
144
|
/** Redis 助手 */
|
|
140
145
|
redis: typeof RedisHelper;
|
|
141
|
-
/** 日志记录器 */
|
|
142
|
-
logger: typeof Logger;
|
|
143
|
-
/** 数据处理工具 */
|
|
144
|
-
tool: Tool;
|
|
145
|
-
/** 自定义插件 */
|
|
146
|
-
[key: string]: any;
|
|
147
146
|
}
|
|
148
147
|
|
|
149
148
|
/**
|
|
@@ -244,16 +243,12 @@ export interface QueryOptions {
|
|
|
244
243
|
fields?: string[];
|
|
245
244
|
/** WHERE 条件 */
|
|
246
245
|
where?: WhereConditions;
|
|
247
|
-
/**
|
|
248
|
-
orderBy?: string;
|
|
246
|
+
/** 排序(格式:["字段#ASC", "字段#DESC"]) */
|
|
247
|
+
orderBy?: string[];
|
|
249
248
|
/** 页码(从 1 开始) */
|
|
250
249
|
page?: number;
|
|
251
250
|
/** 每页数量 */
|
|
252
251
|
limit?: number;
|
|
253
|
-
/** 是否包含已删除数据 */
|
|
254
|
-
includeDeleted?: boolean;
|
|
255
|
-
/** 自定义 state 条件 */
|
|
256
|
-
customState?: WhereConditions;
|
|
257
252
|
}
|
|
258
253
|
|
|
259
254
|
/**
|
|
@@ -276,8 +271,6 @@ export interface UpdateOptions {
|
|
|
276
271
|
data: Record<string, any>;
|
|
277
272
|
/** WHERE 条件 */
|
|
278
273
|
where: WhereConditions;
|
|
279
|
-
/** 是否包含已删除数据,默认 false */
|
|
280
|
-
includeDeleted?: boolean;
|
|
281
274
|
}
|
|
282
275
|
|
|
283
276
|
/**
|
|
@@ -288,8 +281,6 @@ export interface DeleteOptions {
|
|
|
288
281
|
table: string;
|
|
289
282
|
/** WHERE 条件 */
|
|
290
283
|
where: WhereConditions;
|
|
291
|
-
/** 是否物理删除,默认 false(软删除) */
|
|
292
|
-
hard?: boolean;
|
|
293
284
|
}
|
|
294
285
|
|
|
295
286
|
/**
|
|
@@ -311,7 +302,7 @@ export interface ListResult<T = any> {
|
|
|
311
302
|
/**
|
|
312
303
|
* 事务回调函数
|
|
313
304
|
*/
|
|
314
|
-
export type TransactionCallback<T = any> = (trans:
|
|
305
|
+
export type TransactionCallback<T = any> = (trans: DbHelper) => Promise<T>;
|
|
315
306
|
|
|
316
307
|
/**
|
|
317
308
|
* SQL 查询对象
|
|
@@ -324,11 +315,13 @@ export interface SqlQuery {
|
|
|
324
315
|
}
|
|
325
316
|
|
|
326
317
|
/**
|
|
327
|
-
*
|
|
318
|
+
* 数据库管理器接口
|
|
328
319
|
*/
|
|
329
|
-
export interface
|
|
320
|
+
export interface DbHelper {
|
|
321
|
+
/** 查询记录数 */
|
|
322
|
+
getCount(options: Omit<QueryOptions, 'fields' | 'page' | 'limit' | 'orderBy'>): Promise<number>;
|
|
330
323
|
/** 查询单条数据 */
|
|
331
|
-
|
|
324
|
+
getOne<T = any>(options: QueryOptions): Promise<T | null>;
|
|
332
325
|
/** 查询列表(带分页) */
|
|
333
326
|
getList<T = any>(options: QueryOptions): Promise<ListResult<T>>;
|
|
334
327
|
/** 查询所有数据 */
|
|
@@ -367,6 +360,8 @@ export interface RedisHelper {
|
|
|
367
360
|
expire(key: string, seconds: number): Promise<number>;
|
|
368
361
|
/** 获取剩余过期时间 */
|
|
369
362
|
ttl(key: string): Promise<number>;
|
|
363
|
+
/** 测试 Redis 连接 */
|
|
364
|
+
ping(): Promise<string>;
|
|
370
365
|
}
|
|
371
366
|
|
|
372
367
|
/**
|
|
@@ -383,22 +378,6 @@ export interface Logger {
|
|
|
383
378
|
debug(message: string | object): void;
|
|
384
379
|
}
|
|
385
380
|
|
|
386
|
-
/**
|
|
387
|
-
* 工具类接口
|
|
388
|
-
*/
|
|
389
|
-
export interface Tool {
|
|
390
|
-
/** 处理更新数据 */
|
|
391
|
-
updData(data: Record<string, any>, now?: number): Promise<Record<string, any>>;
|
|
392
|
-
/** 处理插入数据 */
|
|
393
|
-
insData(data: Record<string, any> | Record<string, any>[], now?: number): Promise<Record<string, any> | Record<string, any>[]>;
|
|
394
|
-
/** 处理删除数据 */
|
|
395
|
-
delData(now?: number): Promise<Record<string, any>>;
|
|
396
|
-
/** 批量生成 ID */
|
|
397
|
-
genIds(count: number): Promise<number[]>;
|
|
398
|
-
/** 清理数据对象 */
|
|
399
|
-
cleanData(data: Record<string, any>, removeNull?: boolean, removeEmptyString?: boolean): Record<string, any>;
|
|
400
|
-
}
|
|
401
|
-
|
|
402
381
|
/**
|
|
403
382
|
* 验证结果
|
|
404
383
|
*/
|
package/types/redis.d.ts
CHANGED
package/types/validator.d.ts
CHANGED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 验证器相关类型定义
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 字段验证错误信息
|
|
7
|
+
*/
|
|
8
|
+
export type ValidationError = string | null;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 字段类型
|
|
12
|
+
*/
|
|
13
|
+
export type FieldType = 'string' | 'number' | 'text' | 'array';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* 验证结果
|
|
17
|
+
*/
|
|
18
|
+
export interface ValidationResult {
|
|
19
|
+
/** 验证状态码:0=成功,1=失败 */
|
|
20
|
+
code: 0 | 1;
|
|
21
|
+
/** 字段验证结果 */
|
|
22
|
+
fields: Record<string, any>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 字段规则
|
|
27
|
+
*/
|
|
28
|
+
export interface FieldRule {
|
|
29
|
+
/** 字段名 */
|
|
30
|
+
name: string;
|
|
31
|
+
/** 字段类型 */
|
|
32
|
+
type: FieldType;
|
|
33
|
+
/** 最小值/长度 */
|
|
34
|
+
min: number | null;
|
|
35
|
+
/** 最大值/长度 */
|
|
36
|
+
max: number | null;
|
|
37
|
+
/** 默认值 */
|
|
38
|
+
default: any;
|
|
39
|
+
/** 是否索引 */
|
|
40
|
+
index: 0 | 1;
|
|
41
|
+
/** 正则约束 */
|
|
42
|
+
regex: string | null;
|
|
43
|
+
}
|