befly 3.10.18 → 3.10.19
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 +83 -307
- package/dist/befly.config.d.ts +7 -0
- package/{befly.config.ts → dist/befly.config.js} +10 -33
- package/dist/checks/checkApi.d.ts +1 -0
- package/{checks/checkApi.ts → dist/checks/checkApi.js} +11 -28
- package/dist/checks/checkHook.d.ts +1 -0
- package/{checks/checkHook.ts → dist/checks/checkHook.js} +11 -20
- package/dist/checks/checkMenu.d.ts +7 -0
- package/{checks/checkMenu.ts → dist/checks/checkMenu.js} +18 -53
- package/dist/checks/checkPlugin.d.ts +1 -0
- package/{checks/checkPlugin.ts → dist/checks/checkPlugin.js} +11 -20
- package/dist/checks/checkTable.d.ts +6 -0
- package/{checks/checkTable.ts → dist/checks/checkTable.js} +17 -41
- package/dist/configs/presetFields.d.ts +4 -0
- package/{configs/presetFields.ts → dist/configs/presetFields.js} +1 -1
- package/dist/configs/presetRegexp.d.ts +145 -0
- package/{utils/regex.ts → dist/configs/presetRegexp.js} +8 -31
- package/dist/hooks/auth.d.ts +5 -0
- package/{hooks/auth.ts → dist/hooks/auth.js} +6 -10
- package/dist/hooks/cors.d.ts +9 -0
- package/{hooks/cors.ts → dist/hooks/cors.js} +3 -13
- package/dist/hooks/parser.d.ts +12 -0
- package/{hooks/parser.ts → dist/hooks/parser.js} +29 -44
- package/dist/hooks/permission.d.ts +12 -0
- package/{hooks/permission.ts → dist/hooks/permission.js} +14 -25
- package/dist/hooks/validator.d.ts +9 -0
- package/{hooks/validator.ts → dist/hooks/validator.js} +7 -14
- package/dist/lib/asyncContext.d.ts +21 -0
- package/dist/lib/asyncContext.js +27 -0
- package/dist/lib/cacheHelper.d.ts +95 -0
- package/{lib/cacheHelper.ts → dist/lib/cacheHelper.js} +45 -105
- package/dist/lib/cacheKeys.d.ts +23 -0
- package/{lib/cacheKeys.ts → dist/lib/cacheKeys.js} +5 -10
- package/dist/lib/cipher.d.ts +153 -0
- package/{lib/cipher.ts → dist/lib/cipher.js} +23 -44
- package/dist/lib/connect.d.ts +91 -0
- package/{lib/connect.ts → dist/lib/connect.js} +47 -88
- package/dist/lib/dbDialect.d.ts +87 -0
- package/{lib/dbDialect.ts → dist/lib/dbDialect.js} +32 -112
- package/dist/lib/dbHelper.d.ts +204 -0
- package/{lib/dbHelper.ts → dist/lib/dbHelper.js} +83 -240
- package/dist/lib/dbUtils.d.ts +68 -0
- package/{lib/dbUtils.ts → dist/lib/dbUtils.js} +51 -125
- package/dist/lib/jwt.d.ts +13 -0
- package/{lib/jwt.ts → dist/lib/jwt.js} +11 -32
- package/dist/lib/logger.d.ts +32 -0
- package/{lib/logger.ts → dist/lib/logger.js} +202 -279
- package/dist/lib/redisHelper.d.ts +185 -0
- package/{lib/redisHelper.ts → dist/lib/redisHelper.js} +97 -141
- package/dist/lib/sqlBuilder.d.ts +160 -0
- package/{lib/sqlBuilder.ts → dist/lib/sqlBuilder.js} +132 -278
- package/dist/lib/sqlCheck.d.ts +23 -0
- package/{lib/sqlCheck.ts → dist/lib/sqlCheck.js} +24 -41
- package/dist/lib/validator.d.ts +45 -0
- package/{lib/validator.ts → dist/lib/validator.js} +44 -61
- package/dist/loader/loadApis.d.ts +12 -0
- package/{loader/loadApis.ts → dist/loader/loadApis.js} +9 -19
- package/dist/loader/loadHooks.d.ts +8 -0
- package/{loader/loadHooks.ts → dist/loader/loadHooks.js} +7 -21
- package/dist/loader/loadPlugins.d.ts +8 -0
- package/{loader/loadPlugins.ts → dist/loader/loadPlugins.js} +10 -22
- package/dist/main.d.ts +26 -0
- package/{main.ts → dist/main.js} +60 -99
- package/dist/paths.d.ts +93 -0
- package/{paths.ts → dist/paths.js} +6 -19
- package/dist/plugins/cache.d.ts +14 -0
- package/{plugins/cache.ts → dist/plugins/cache.js} +5 -12
- package/dist/plugins/cipher.d.ts +10 -0
- package/{plugins/cipher.ts → dist/plugins/cipher.js} +2 -6
- package/dist/plugins/config.d.ts +10 -0
- package/dist/plugins/config.js +6 -0
- package/dist/plugins/db.d.ts +14 -0
- package/{plugins/db.ts → dist/plugins/db.js} +9 -17
- package/dist/plugins/jwt.d.ts +10 -0
- package/dist/plugins/jwt.js +10 -0
- package/dist/plugins/logger.d.ts +28 -0
- package/{plugins/logger.ts → dist/plugins/logger.js} +3 -8
- package/dist/plugins/redis.d.ts +14 -0
- package/{plugins/redis.ts → dist/plugins/redis.js} +7 -12
- package/dist/plugins/tool.d.ts +79 -0
- package/{plugins/tool.ts → dist/plugins/tool.js} +7 -30
- package/dist/router/api.d.ts +14 -0
- package/dist/router/api.js +107 -0
- package/dist/router/static.d.ts +9 -0
- package/{router/static.ts → dist/router/static.js} +20 -34
- package/dist/scripts/ensureDist.d.ts +1 -0
- package/dist/scripts/ensureDist.js +80 -0
- package/dist/sync/syncApi.d.ts +3 -0
- package/{sync/syncApi.ts → dist/sync/syncApi.js} +34 -54
- package/dist/sync/syncCache.d.ts +2 -0
- package/{sync/syncCache.ts → dist/sync/syncCache.js} +1 -6
- package/dist/sync/syncDev.d.ts +6 -0
- package/{sync/syncDev.ts → dist/sync/syncDev.js} +29 -62
- package/dist/sync/syncMenu.d.ts +14 -0
- package/{sync/syncMenu.ts → dist/sync/syncMenu.js} +65 -125
- package/dist/sync/syncTable.d.ts +151 -0
- package/{sync/syncTable.ts → dist/sync/syncTable.js} +171 -378
- package/{types → dist/types}/api.d.ts +8 -47
- package/dist/types/api.js +4 -0
- package/{types → dist/types}/befly.d.ts +31 -222
- package/dist/types/befly.js +4 -0
- package/{types → dist/types}/cache.d.ts +7 -15
- package/dist/types/cache.js +4 -0
- package/dist/types/cipher.d.ts +27 -0
- package/dist/types/cipher.js +7 -0
- package/{types → dist/types}/common.d.ts +8 -33
- package/dist/types/common.js +5 -0
- package/{types → dist/types}/context.d.ts +2 -4
- package/dist/types/context.js +4 -0
- package/{types → dist/types}/crypto.d.ts +0 -3
- package/dist/types/crypto.js +4 -0
- package/dist/types/database.d.ts +138 -0
- package/dist/types/database.js +4 -0
- package/dist/types/hook.d.ts +15 -0
- package/dist/types/hook.js +6 -0
- package/dist/types/jwt.d.ts +75 -0
- package/dist/types/jwt.js +4 -0
- package/dist/types/logger.d.ts +47 -0
- package/dist/types/logger.js +6 -0
- package/dist/types/plugin.d.ts +14 -0
- package/dist/types/plugin.js +6 -0
- package/dist/types/redis.d.ts +71 -0
- package/dist/types/redis.js +4 -0
- package/{types/roleApisCache.ts → dist/types/roleApisCache.d.ts} +0 -2
- package/dist/types/roleApisCache.js +8 -0
- package/dist/types/sync.d.ts +92 -0
- package/dist/types/sync.js +4 -0
- package/dist/types/table.d.ts +34 -0
- package/dist/types/table.js +4 -0
- package/dist/types/validate.d.ts +67 -0
- package/dist/types/validate.js +4 -0
- package/dist/utils/arrayKeysToCamel.d.ts +13 -0
- package/{utils/arrayKeysToCamel.ts → dist/utils/arrayKeysToCamel.js} +5 -5
- package/dist/utils/calcPerfTime.d.ts +4 -0
- package/{utils/calcPerfTime.ts → dist/utils/calcPerfTime.js} +3 -3
- package/dist/utils/configTypes.d.ts +1 -0
- package/dist/utils/configTypes.js +1 -0
- package/dist/utils/convertBigIntFields.d.ts +11 -0
- package/{utils/convertBigIntFields.ts → dist/utils/convertBigIntFields.js} +5 -9
- package/dist/utils/cors.d.ts +8 -0
- package/{utils/cors.ts → dist/utils/cors.js} +1 -3
- package/dist/utils/disableMenusGlob.d.ts +13 -0
- package/{utils/disableMenusGlob.ts → dist/utils/disableMenusGlob.js} +9 -29
- package/dist/utils/fieldClear.d.ts +11 -0
- package/{utils/fieldClear.ts → dist/utils/fieldClear.js} +15 -33
- package/dist/utils/genShortId.d.ts +10 -0
- package/{utils/genShortId.ts → dist/utils/genShortId.js} +1 -1
- package/dist/utils/getClientIp.d.ts +6 -0
- package/{utils/getClientIp.ts → dist/utils/getClientIp.js} +1 -7
- package/dist/utils/importDefault.d.ts +1 -0
- package/dist/utils/importDefault.js +29 -0
- package/dist/utils/isDirentDirectory.d.ts +2 -0
- package/{utils/isDirentDirectory.ts → dist/utils/isDirentDirectory.js} +3 -8
- package/dist/utils/keysToCamel.d.ts +10 -0
- package/{utils/keysToCamel.ts → dist/utils/keysToCamel.js} +4 -5
- package/dist/utils/keysToSnake.d.ts +10 -0
- package/{utils/keysToSnake.ts → dist/utils/keysToSnake.js} +4 -5
- package/dist/utils/loadMenuConfigs.d.ts +5 -0
- package/{utils/loadMenuConfigs.ts → dist/utils/loadMenuConfigs.js} +24 -51
- package/dist/utils/pickFields.d.ts +4 -0
- package/{utils/pickFields.ts → dist/utils/pickFields.js} +2 -5
- package/dist/utils/process.d.ts +24 -0
- package/{utils/process.ts → dist/utils/process.js} +2 -18
- package/dist/utils/processFields.d.ts +4 -0
- package/{utils/processFields.ts → dist/utils/processFields.js} +5 -9
- package/dist/utils/regex.d.ts +145 -0
- package/{configs/presetRegexp.ts → dist/utils/regex.js} +8 -31
- package/dist/utils/response.d.ts +20 -0
- package/{utils/response.ts → dist/utils/response.js} +28 -49
- package/dist/utils/scanAddons.d.ts +17 -0
- package/{utils/scanAddons.ts → dist/utils/scanAddons.js} +6 -40
- package/dist/utils/scanConfig.d.ts +26 -0
- package/{utils/scanConfig.ts → dist/utils/scanConfig.js} +22 -59
- package/dist/utils/scanFiles.d.ts +30 -0
- package/{utils/scanFiles.ts → dist/utils/scanFiles.js} +26 -66
- package/dist/utils/scanSources.d.ts +10 -0
- package/dist/utils/scanSources.js +41 -0
- package/dist/utils/sortModules.d.ts +28 -0
- package/{utils/sortModules.ts → dist/utils/sortModules.js} +25 -65
- package/dist/utils/sqlLog.d.ts +14 -0
- package/{utils/sqlLog.ts → dist/utils/sqlLog.js} +2 -14
- package/package.json +14 -28
- package/.gitignore +0 -0
- package/bunfig.toml +0 -3
- package/docs/README.md +0 -98
- package/docs/api/api.md +0 -1921
- package/docs/guide/examples.md +0 -926
- package/docs/guide/quickstart.md +0 -354
- package/docs/hooks/auth.md +0 -38
- package/docs/hooks/cors.md +0 -28
- package/docs/hooks/hook.md +0 -838
- package/docs/hooks/parser.md +0 -19
- package/docs/hooks/rateLimit.md +0 -47
- package/docs/infra/redis.md +0 -628
- package/docs/plugins/cipher.md +0 -61
- package/docs/plugins/database.md +0 -189
- package/docs/plugins/plugin.md +0 -986
- package/docs/reference/addon.md +0 -510
- package/docs/reference/config.md +0 -573
- package/docs/reference/logger.md +0 -495
- package/docs/reference/sync.md +0 -478
- package/docs/reference/table.md +0 -763
- package/docs/reference/validator.md +0 -620
- package/lib/asyncContext.ts +0 -43
- package/plugins/config.ts +0 -13
- package/plugins/jwt.ts +0 -15
- package/router/api.ts +0 -130
- package/tsconfig.json +0 -8
- package/types/database.d.ts +0 -541
- package/types/hook.d.ts +0 -25
- package/types/jwt.d.ts +0 -118
- package/types/logger.d.ts +0 -65
- package/types/plugin.d.ts +0 -19
- package/types/redis.d.ts +0 -83
- package/types/sync.d.ts +0 -398
- package/types/table.d.ts +0 -216
- package/types/validate.d.ts +0 -69
- package/utils/configTypes.ts +0 -3
- package/utils/importDefault.ts +0 -21
- package/utils/scanSources.ts +0 -64
package/router/api.ts
DELETED
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* API路由处理器
|
|
3
|
-
* 处理 /api/* 路径的请求
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import type { ApiRoute } from "../types/api.ts";
|
|
7
|
-
import type { BeflyContext } from "../types/befly.ts";
|
|
8
|
-
// 类型导入
|
|
9
|
-
import type { RequestContext } from "../types/context.ts";
|
|
10
|
-
import type { Hook } from "../types/hook.ts";
|
|
11
|
-
|
|
12
|
-
import { withCtx } from "../lib/asyncContext.ts";
|
|
13
|
-
import { Logger } from "../lib/logger.ts";
|
|
14
|
-
// 相对导入
|
|
15
|
-
import { genShortId } from "../utils/genShortId.ts";
|
|
16
|
-
import { getClientIp } from "../utils/getClientIp.ts";
|
|
17
|
-
import { FinalResponse } from "../utils/response.ts";
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* API处理器工厂函数
|
|
21
|
-
* @param apis - API路由映射表
|
|
22
|
-
* @param hooks - 钩子列表
|
|
23
|
-
* @param context - 应用上下文
|
|
24
|
-
*/
|
|
25
|
-
export function apiHandler(apis: Map<string, ApiRoute>, hooks: Hook[], context: BeflyContext) {
|
|
26
|
-
return async (req: Request, server?: any): Promise<Response> => {
|
|
27
|
-
// 1. 生成请求 ID
|
|
28
|
-
const requestId = genShortId();
|
|
29
|
-
|
|
30
|
-
// 2. 创建请求上下文
|
|
31
|
-
const url = new URL(req.url);
|
|
32
|
-
// 只用接口路径做存在性判断与匹配:不要把 method 拼进 key
|
|
33
|
-
// 说明:apisMap 的 key 来源于 scanFiles/loadApis 生成的 routePath(例如 /api/core/xxx)
|
|
34
|
-
const apiPath = url.pathname || "/";
|
|
35
|
-
|
|
36
|
-
const clientIp = getClientIp(req, server);
|
|
37
|
-
|
|
38
|
-
const now = Date.now();
|
|
39
|
-
|
|
40
|
-
const ctx: RequestContext = {
|
|
41
|
-
method: req.method,
|
|
42
|
-
body: {},
|
|
43
|
-
user: {},
|
|
44
|
-
req: req,
|
|
45
|
-
now: now,
|
|
46
|
-
ip: clientIp,
|
|
47
|
-
headers: req.headers,
|
|
48
|
-
route: apiPath,
|
|
49
|
-
requestId: requestId,
|
|
50
|
-
corsHeaders: {
|
|
51
|
-
"X-Request-ID": requestId
|
|
52
|
-
},
|
|
53
|
-
api: apis.get(apiPath),
|
|
54
|
-
response: undefined,
|
|
55
|
-
result: undefined
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
return withCtx(
|
|
59
|
-
{
|
|
60
|
-
requestId: requestId,
|
|
61
|
-
method: req.method,
|
|
62
|
-
route: apiPath,
|
|
63
|
-
ip: clientIp,
|
|
64
|
-
now: now
|
|
65
|
-
},
|
|
66
|
-
async () => {
|
|
67
|
-
try {
|
|
68
|
-
// 4. 串联执行所有钩子
|
|
69
|
-
for (const hook of hooks) {
|
|
70
|
-
await hook.handler(context, ctx);
|
|
71
|
-
|
|
72
|
-
// 如果钩子已经设置了 response,停止执行
|
|
73
|
-
if (ctx.response) {
|
|
74
|
-
return ctx.response;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// hooks 全部通过后记录请求日志(拦截请求仅由 ErrorResponse 记录)
|
|
79
|
-
if (ctx.api && req.method !== "OPTIONS") {
|
|
80
|
-
const logData: Record<string, any> = {
|
|
81
|
-
event: "request",
|
|
82
|
-
apiName: ctx.api.name
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
if (ctx.body && Object.keys(ctx.body).length > 0) {
|
|
86
|
-
logData.body = ctx.body;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
Logger.info(logData, "request");
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// 5. 执行 API handler
|
|
93
|
-
if (!ctx.api) {
|
|
94
|
-
if (req.method !== "OPTIONS") {
|
|
95
|
-
ctx.response = Response.json(
|
|
96
|
-
{
|
|
97
|
-
code: 1,
|
|
98
|
-
msg: "接口不存在"
|
|
99
|
-
},
|
|
100
|
-
{
|
|
101
|
-
headers: ctx.corsHeaders
|
|
102
|
-
}
|
|
103
|
-
);
|
|
104
|
-
}
|
|
105
|
-
} else if (ctx.api.handler) {
|
|
106
|
-
const result = await ctx.api.handler(context, ctx);
|
|
107
|
-
|
|
108
|
-
if (result instanceof Response) {
|
|
109
|
-
ctx.response = result;
|
|
110
|
-
} else {
|
|
111
|
-
ctx.result = result;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
// 7. 返回响应(自动处理 response/result/日志)
|
|
116
|
-
return FinalResponse(ctx);
|
|
117
|
-
} catch (err: any) {
|
|
118
|
-
// 全局错误处理
|
|
119
|
-
const errorPath = ctx.api ? apiPath : req.url;
|
|
120
|
-
Logger.error({ err: err, path: errorPath }, "请求错误");
|
|
121
|
-
ctx.result = {
|
|
122
|
-
code: 1,
|
|
123
|
-
msg: "内部服务错误"
|
|
124
|
-
};
|
|
125
|
-
return FinalResponse(ctx);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
);
|
|
129
|
-
};
|
|
130
|
-
}
|
package/tsconfig.json
DELETED
package/types/database.d.ts
DELETED
|
@@ -1,541 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 数据库相关类型定义
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import type { SqlValue } from "./common";
|
|
6
|
-
import type { JoinOption } from "./common";
|
|
7
|
-
import type { DatabaseTables, TableName, TableType, TableInsertType, TableUpdateType, TypedWhereConditions } from "./table";
|
|
8
|
-
|
|
9
|
-
// 重新导出表类型工具
|
|
10
|
-
export type { DatabaseTables, TableName, TableType, TableInsertType, TableUpdateType, SystemFields, BaseTable, InsertType, UpdateType, SelectType, TypedWhereConditions } from "./table";
|
|
11
|
-
|
|
12
|
-
// ============================================
|
|
13
|
-
// SQL 查询相关类型
|
|
14
|
-
// ============================================
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* WHERE 条件操作符
|
|
18
|
-
*/
|
|
19
|
-
export type WhereOperator = "$eq" | "$ne" | "$not" | "$gt" | "$gte" | "$lt" | "$lte" | "$like" | "$notLike" | "$in" | "$notIn" | "$nin" | "$isNull" | "$isNotNull" | "$null" | "$notNull" | "$between" | "$notBetween";
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* WHERE 条件类型
|
|
23
|
-
*/
|
|
24
|
-
export type WhereConditions = Record<string, any>;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* 排序方向
|
|
28
|
-
*/
|
|
29
|
-
export type OrderDirection = "ASC" | "DESC";
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* 排序字段
|
|
33
|
-
*/
|
|
34
|
-
export type OrderByField = string | { field: string; direction: OrderDirection };
|
|
35
|
-
|
|
36
|
-
// ============================================
|
|
37
|
-
// 泛型查询选项(类型安全版本)
|
|
38
|
-
// ============================================
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* 泛型查询选项 - 支持类型推断
|
|
42
|
-
* @template K - 表名类型
|
|
43
|
-
*/
|
|
44
|
-
export interface TypedQueryOptions<K extends TableName> {
|
|
45
|
-
/** 表名 */
|
|
46
|
-
table: K;
|
|
47
|
-
/** 查询字段(表字段的子集) */
|
|
48
|
-
fields?: (keyof TableType<K>)[] | string[];
|
|
49
|
-
/** WHERE 条件(类型安全) */
|
|
50
|
-
where?: TypedWhereConditions<TableType<K>> | WhereConditions;
|
|
51
|
-
/** 排序(格式:["字段#ASC", "字段#DESC"]) */
|
|
52
|
-
orderBy?: string[];
|
|
53
|
-
/** 页码(从 1 开始) */
|
|
54
|
-
page?: number;
|
|
55
|
-
/** 每页数量 */
|
|
56
|
-
limit?: number;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* 泛型插入选项 - 支持类型推断
|
|
61
|
-
* @template K - 表名类型
|
|
62
|
-
*/
|
|
63
|
-
export interface TypedInsertOptions<K extends TableName> {
|
|
64
|
-
/** 表名 */
|
|
65
|
-
table: K;
|
|
66
|
-
/** 插入数据(ID、时间戳、state 会自动生成) */
|
|
67
|
-
data: TableInsertType<K> | Record<string, any>;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* 泛型更新选项 - 支持类型推断
|
|
72
|
-
* @template K - 表名类型
|
|
73
|
-
*/
|
|
74
|
-
export interface TypedUpdateOptions<K extends TableName> {
|
|
75
|
-
/** 表名 */
|
|
76
|
-
table: K;
|
|
77
|
-
/** 更新数据(updated_at 会自动更新) */
|
|
78
|
-
data: TableUpdateType<K> | Record<string, any>;
|
|
79
|
-
/** WHERE 条件 */
|
|
80
|
-
where: TypedWhereConditions<TableType<K>> | WhereConditions;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* 泛型删除选项 - 支持类型推断
|
|
85
|
-
* @template K - 表名类型
|
|
86
|
-
*/
|
|
87
|
-
export interface TypedDeleteOptions<K extends TableName> {
|
|
88
|
-
/** 表名 */
|
|
89
|
-
table: K;
|
|
90
|
-
/** WHERE 条件 */
|
|
91
|
-
where: TypedWhereConditions<TableType<K>> | WhereConditions;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// ============================================
|
|
95
|
-
// 兼容旧版查询选项(非类型安全版本)
|
|
96
|
-
// ============================================
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* 查询选项(兼容旧版,不进行类型检查)
|
|
100
|
-
*/
|
|
101
|
-
export interface QueryOptions {
|
|
102
|
-
/** 表名(可带别名,如 'order o') */
|
|
103
|
-
table: string;
|
|
104
|
-
/** 查询字段(联查时需带表别名,如 'o.id', 'u.username') */
|
|
105
|
-
fields?: string[];
|
|
106
|
-
/** WHERE 条件(联查时字段需带表别名,如 { 'o.state': 1 }) */
|
|
107
|
-
where?: WhereConditions;
|
|
108
|
-
/** 多表联查选项 */
|
|
109
|
-
joins?: JoinOption[];
|
|
110
|
-
/** 排序(格式:["字段#ASC", "字段#DESC"]) */
|
|
111
|
-
orderBy?: string[];
|
|
112
|
-
/** 页码(从 1 开始) */
|
|
113
|
-
page?: number;
|
|
114
|
-
/** 每页数量 */
|
|
115
|
-
limit?: number;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* 插入选项(兼容旧版)
|
|
120
|
-
*/
|
|
121
|
-
export interface InsertOptions {
|
|
122
|
-
/** 表名 */
|
|
123
|
-
table: string;
|
|
124
|
-
/** 插入数据(ID、时间戳、state 会自动生成) */
|
|
125
|
-
data: Record<string, any>;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* 更新选项(兼容旧版)
|
|
130
|
-
*/
|
|
131
|
-
export interface UpdateOptions {
|
|
132
|
-
/** 表名 */
|
|
133
|
-
table: string;
|
|
134
|
-
/** 更新数据(updated_at 会自动更新) */
|
|
135
|
-
data: Record<string, any>;
|
|
136
|
-
/** WHERE 条件 */
|
|
137
|
-
where: WhereConditions;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* 删除选项(兼容旧版)
|
|
142
|
-
*/
|
|
143
|
-
export interface DeleteOptions {
|
|
144
|
-
/** 表名 */
|
|
145
|
-
table: string;
|
|
146
|
-
/** WHERE 条件 */
|
|
147
|
-
where: WhereConditions;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* 列表查询结果
|
|
152
|
-
*/
|
|
153
|
-
export interface ListResult<T = any> {
|
|
154
|
-
/** 数据列表 */
|
|
155
|
-
lists: T[];
|
|
156
|
-
/** 总条数 */
|
|
157
|
-
total: number;
|
|
158
|
-
/** 当前页码 */
|
|
159
|
-
page: number;
|
|
160
|
-
/** 每页数量 */
|
|
161
|
-
limit: number;
|
|
162
|
-
/** 总页数 */
|
|
163
|
-
pages: number;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* 全部查询结果
|
|
168
|
-
*/
|
|
169
|
-
export interface AllResult<T = any> {
|
|
170
|
-
/** 数据列表 */
|
|
171
|
-
lists: T[];
|
|
172
|
-
/** 总条数 */
|
|
173
|
-
total: number;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* 事务回调函数
|
|
178
|
-
*/
|
|
179
|
-
export type TransactionCallback<T = any> = (trans: DbHelper) => Promise<T>;
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* SQL 查询结果
|
|
183
|
-
*/
|
|
184
|
-
export interface SqlQuery {
|
|
185
|
-
sql: string;
|
|
186
|
-
params: SqlValue[];
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* SQL 信息(用于 DbHelper 统一返回结构)
|
|
191
|
-
*/
|
|
192
|
-
export interface SqlInfo extends SqlQuery {
|
|
193
|
-
/** 执行耗时(毫秒) */
|
|
194
|
-
duration: number;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* 列表/全量查询常见的两段 SQL 调试信息
|
|
199
|
-
* - count: 计数 SQL
|
|
200
|
-
* - data: 数据 SQL(在 total=0 等情况下可能不会执行)
|
|
201
|
-
*/
|
|
202
|
-
export interface ListSql {
|
|
203
|
-
count: SqlInfo;
|
|
204
|
-
data?: SqlInfo;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* DbHelper 统一返回结构:全部用 data 承载原返回值,并附带 sql
|
|
209
|
-
*/
|
|
210
|
-
export interface DbResult<TData = any, TSql = SqlInfo> {
|
|
211
|
-
data: TData;
|
|
212
|
-
sql: TSql;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
/**
|
|
216
|
-
* 插入数据类型
|
|
217
|
-
*/
|
|
218
|
-
export type InsertData = Record<string, SqlValue> | Record<string, SqlValue>[];
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* 更新数据类型
|
|
222
|
-
*/
|
|
223
|
-
export type UpdateData = Record<string, SqlValue>;
|
|
224
|
-
|
|
225
|
-
/**
|
|
226
|
-
* 数据库类型
|
|
227
|
-
*/
|
|
228
|
-
export type DbType = "mysql" | "postgresql" | "sqlite";
|
|
229
|
-
|
|
230
|
-
/**
|
|
231
|
-
* 列信息
|
|
232
|
-
*/
|
|
233
|
-
export interface ColumnInfo {
|
|
234
|
-
/** 列名 */
|
|
235
|
-
name: string;
|
|
236
|
-
/** 数据类型 */
|
|
237
|
-
type: string;
|
|
238
|
-
/** 是否可空 */
|
|
239
|
-
nullable: boolean;
|
|
240
|
-
/** 默认值 */
|
|
241
|
-
default: any;
|
|
242
|
-
/** 长度 */
|
|
243
|
-
length?: number;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
/**
|
|
247
|
-
* 索引信息
|
|
248
|
-
*/
|
|
249
|
-
export interface IndexInfo {
|
|
250
|
-
/** 索引名 */
|
|
251
|
-
name: string;
|
|
252
|
-
/** 列名 */
|
|
253
|
-
column: string;
|
|
254
|
-
/** 是否唯一 */
|
|
255
|
-
unique: boolean;
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
/**
|
|
259
|
-
* 同步统计
|
|
260
|
-
*/
|
|
261
|
-
export interface SyncStats {
|
|
262
|
-
/** 创建的表数 */
|
|
263
|
-
created: number;
|
|
264
|
-
/** 修改的表数 */
|
|
265
|
-
modified: number;
|
|
266
|
-
/** 创建的索引数 */
|
|
267
|
-
indexesCreated: number;
|
|
268
|
-
/** 删除的索引数 */
|
|
269
|
-
indexesDropped: number;
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
/**
|
|
273
|
-
* DbHelper 接口(前向声明)
|
|
274
|
-
* 支持两种使用方式:
|
|
275
|
-
* 1. 类型安全模式:使用 TypedQueryOptions 等泛型接口,获得完整类型推断
|
|
276
|
-
* 2. 兼容模式:使用 QueryOptions 等非泛型接口,行为与之前一致
|
|
277
|
-
*/
|
|
278
|
-
export interface DbHelper {
|
|
279
|
-
// ============================================
|
|
280
|
-
// 类型安全方法(推荐)
|
|
281
|
-
// ============================================
|
|
282
|
-
|
|
283
|
-
/**
|
|
284
|
-
* 查询记录数(类型安全版本)
|
|
285
|
-
* @template K - 表名类型
|
|
286
|
-
*/
|
|
287
|
-
getCount<K extends TableName>(options: Omit<TypedQueryOptions<K>, "fields" | "page" | "limit" | "orderBy">): Promise<DbResult<number>>;
|
|
288
|
-
|
|
289
|
-
/**
|
|
290
|
-
* 查询单条数据(类型安全版本)
|
|
291
|
-
* @template K - 表名类型
|
|
292
|
-
* @returns 返回类型自动推断为对应表的记录类型
|
|
293
|
-
*/
|
|
294
|
-
getOne<K extends TableName>(options: TypedQueryOptions<K>): Promise<DbResult<TableType<K> | null>>;
|
|
295
|
-
|
|
296
|
-
/**
|
|
297
|
-
* 查询列表(类型安全版本)
|
|
298
|
-
* @template K - 表名类型
|
|
299
|
-
* @returns 返回类型自动推断为对应表的记录列表
|
|
300
|
-
*/
|
|
301
|
-
getList<K extends TableName>(options: TypedQueryOptions<K>): Promise<DbResult<ListResult<TableType<K>>, ListSql>>;
|
|
302
|
-
|
|
303
|
-
/**
|
|
304
|
-
* 查询所有数据(类型安全版本)
|
|
305
|
-
* @template K - 表名类型
|
|
306
|
-
* @returns 返回类型自动推断为对应表的记录数组
|
|
307
|
-
*/
|
|
308
|
-
getAll<K extends TableName>(options: Omit<TypedQueryOptions<K>, "page" | "limit">): Promise<DbResult<AllResult<TableType<K>>, ListSql>>;
|
|
309
|
-
|
|
310
|
-
/**
|
|
311
|
-
* 插入数据(类型安全版本)
|
|
312
|
-
* @template K - 表名类型
|
|
313
|
-
*/
|
|
314
|
-
insData<K extends TableName>(options: TypedInsertOptions<K>): Promise<DbResult<number>>;
|
|
315
|
-
|
|
316
|
-
/**
|
|
317
|
-
* 更新数据(类型安全版本)
|
|
318
|
-
* @template K - 表名类型
|
|
319
|
-
*/
|
|
320
|
-
updData<K extends TableName>(options: TypedUpdateOptions<K>): Promise<DbResult<number>>;
|
|
321
|
-
|
|
322
|
-
/**
|
|
323
|
-
* 删除数据(类型安全版本)
|
|
324
|
-
* @template K - 表名类型
|
|
325
|
-
*/
|
|
326
|
-
delData<K extends TableName>(options: TypedDeleteOptions<K>): Promise<DbResult<number>>;
|
|
327
|
-
|
|
328
|
-
// ============================================
|
|
329
|
-
// 兼容旧版方法(手动指定返回类型)
|
|
330
|
-
// ============================================
|
|
331
|
-
|
|
332
|
-
/**
|
|
333
|
-
* 查询记录数(兼容版本)
|
|
334
|
-
*/
|
|
335
|
-
getCount(options: Omit<QueryOptions, "fields" | "page" | "limit" | "orderBy">): Promise<DbResult<number>>;
|
|
336
|
-
|
|
337
|
-
/**
|
|
338
|
-
* 查询单条数据(兼容版本,需手动指定泛型)
|
|
339
|
-
* @template T - 返回类型
|
|
340
|
-
*/
|
|
341
|
-
getOne<T = any>(options: QueryOptions): Promise<DbResult<T | null>>;
|
|
342
|
-
|
|
343
|
-
/**
|
|
344
|
-
* 查询列表(兼容版本,需手动指定泛型)
|
|
345
|
-
* @template T - 列表项类型
|
|
346
|
-
*/
|
|
347
|
-
getList<T = any>(options: QueryOptions): Promise<DbResult<ListResult<T>, ListSql>>;
|
|
348
|
-
|
|
349
|
-
/**
|
|
350
|
-
* 查询所有数据(兼容版本,需手动指定泛型)
|
|
351
|
-
* @template T - 返回类型
|
|
352
|
-
*/
|
|
353
|
-
getAll<T = any>(options: Omit<QueryOptions, "page" | "limit">): Promise<DbResult<AllResult<T>, ListSql>>;
|
|
354
|
-
|
|
355
|
-
/**
|
|
356
|
-
* 插入数据(兼容版本)
|
|
357
|
-
*/
|
|
358
|
-
insData(options: InsertOptions): Promise<DbResult<number>>;
|
|
359
|
-
|
|
360
|
-
/**
|
|
361
|
-
* 更新数据(兼容版本)
|
|
362
|
-
*/
|
|
363
|
-
updData(options: UpdateOptions): Promise<DbResult<number>>;
|
|
364
|
-
|
|
365
|
-
/**
|
|
366
|
-
* 删除数据(兼容版本)
|
|
367
|
-
*/
|
|
368
|
-
delData(options: DeleteOptions): Promise<DbResult<number>>;
|
|
369
|
-
|
|
370
|
-
// ============================================
|
|
371
|
-
// 通用方法
|
|
372
|
-
// ============================================
|
|
373
|
-
|
|
374
|
-
/**
|
|
375
|
-
* 执行事务
|
|
376
|
-
* @template T - 事务返回类型
|
|
377
|
-
*/
|
|
378
|
-
trans<T = any>(callback: TransactionCallback<T>): Promise<T>;
|
|
379
|
-
|
|
380
|
-
/**
|
|
381
|
-
* 执行原始 SQL
|
|
382
|
-
*/
|
|
383
|
-
query(sql: string, params?: any[]): Promise<DbResult<any>>;
|
|
384
|
-
|
|
385
|
-
/**
|
|
386
|
-
* 检查数据是否存在
|
|
387
|
-
*/
|
|
388
|
-
exists(options: Omit<QueryOptions, "fields" | "orderBy" | "page" | "limit">): Promise<DbResult<boolean>>;
|
|
389
|
-
|
|
390
|
-
/**
|
|
391
|
-
* 检查表是否存在
|
|
392
|
-
*/
|
|
393
|
-
tableExists(tableName: string): Promise<DbResult<boolean>>;
|
|
394
|
-
|
|
395
|
-
/**
|
|
396
|
-
* 批量插入数据
|
|
397
|
-
*/
|
|
398
|
-
insBatch(table: string, dataList: Record<string, any>[]): Promise<DbResult<number[]>>;
|
|
399
|
-
|
|
400
|
-
/**
|
|
401
|
-
* 禁用数据(设置 state=2)
|
|
402
|
-
*/
|
|
403
|
-
disableData(options: Omit<DeleteOptions, "hard">): Promise<DbResult<number>>;
|
|
404
|
-
|
|
405
|
-
/**
|
|
406
|
-
* 启用数据(设置 state=1)
|
|
407
|
-
*/
|
|
408
|
-
enableData(options: Omit<DeleteOptions, "hard">): Promise<DbResult<number>>;
|
|
409
|
-
|
|
410
|
-
/**
|
|
411
|
-
* 硬删除数据(物理删除)
|
|
412
|
-
*/
|
|
413
|
-
delForce(options: Omit<DeleteOptions, "hard">): Promise<DbResult<number>>;
|
|
414
|
-
|
|
415
|
-
/**
|
|
416
|
-
* 自增字段
|
|
417
|
-
*/
|
|
418
|
-
increment(table: string, field: string, where: WhereConditions, value?: number): Promise<DbResult<number>>;
|
|
419
|
-
|
|
420
|
-
/**
|
|
421
|
-
* 自减字段
|
|
422
|
-
*/
|
|
423
|
-
decrement(table: string, field: string, where: WhereConditions, value?: number): Promise<DbResult<number>>;
|
|
424
|
-
|
|
425
|
-
/**
|
|
426
|
-
* 查询单个字段值
|
|
427
|
-
*/
|
|
428
|
-
getFieldValue<T = any>(options: Omit<QueryOptions, "fields"> & { field: string }): Promise<DbResult<T | null>>;
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
/**
|
|
432
|
-
* SQL 客户端选项
|
|
433
|
-
*/
|
|
434
|
-
export interface SqlClientOptions {
|
|
435
|
-
/** 最大连接数 */
|
|
436
|
-
max?: number;
|
|
437
|
-
/** 是否使用 BigInt */
|
|
438
|
-
bigint?: boolean;
|
|
439
|
-
/** 连接超时时间(毫秒),默认 30000ms */
|
|
440
|
-
connectionTimeout?: number;
|
|
441
|
-
/** 其他自定义选项 */
|
|
442
|
-
[key: string]: any;
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
// ========== 数据库同步相关类型 ==========
|
|
446
|
-
|
|
447
|
-
/**
|
|
448
|
-
* 列信息接口(用于数据库同步)
|
|
449
|
-
*/
|
|
450
|
-
export interface ColumnInfo {
|
|
451
|
-
/** 字段类型(如 string, number 等) */
|
|
452
|
-
type: string;
|
|
453
|
-
/** 数据库列类型(如 VARCHAR, INT 等) */
|
|
454
|
-
columnType: string;
|
|
455
|
-
/** 字段长度 */
|
|
456
|
-
length: number | null;
|
|
457
|
-
/** 是否可空 */
|
|
458
|
-
nullable: boolean;
|
|
459
|
-
/** 默认值 */
|
|
460
|
-
defaultValue: any;
|
|
461
|
-
/** 字段注释 */
|
|
462
|
-
comment: string | null;
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
/**
|
|
466
|
-
* 索引信息接口
|
|
467
|
-
*/
|
|
468
|
-
export interface IndexInfo {
|
|
469
|
-
/** 索引名称到字段数组的映射 */
|
|
470
|
-
[indexName: string]: string[];
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
/**
|
|
474
|
-
* 字段变更类型
|
|
475
|
-
*/
|
|
476
|
-
export interface FieldChange {
|
|
477
|
-
/** 变更类型 */
|
|
478
|
-
type: "length" | "comment" | "datatype" | "default";
|
|
479
|
-
/** 当前值 */
|
|
480
|
-
current: any;
|
|
481
|
-
/** 新值 */
|
|
482
|
-
new: any;
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
/**
|
|
486
|
-
* 索引操作接口
|
|
487
|
-
*/
|
|
488
|
-
export interface IndexAction {
|
|
489
|
-
/** 操作类型:create(创建)或 drop(删除) */
|
|
490
|
-
action: "create" | "drop";
|
|
491
|
-
/** 索引名称 */
|
|
492
|
-
indexName: string;
|
|
493
|
-
/** 字段名称 */
|
|
494
|
-
fieldName: string;
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
/**
|
|
498
|
-
* 表同步计划接口
|
|
499
|
-
*/
|
|
500
|
-
export interface TablePlan {
|
|
501
|
-
/** 是否有变更 */
|
|
502
|
-
changed: boolean;
|
|
503
|
-
/** 添加字段的 SQL 子句 */
|
|
504
|
-
addClauses: string[];
|
|
505
|
-
/** 修改字段的 SQL 子句 */
|
|
506
|
-
modifyClauses: string[];
|
|
507
|
-
/** 默认值变更的 SQL 子句 */
|
|
508
|
-
defaultClauses: string[];
|
|
509
|
-
/** 索引操作列表 */
|
|
510
|
-
indexActions: IndexAction[];
|
|
511
|
-
/** 注释变更的 SQL 子句(可选) */
|
|
512
|
-
commentActions?: string[];
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
/**
|
|
516
|
-
* 全局统计计数接口
|
|
517
|
-
*/
|
|
518
|
-
export interface GlobalCount {
|
|
519
|
-
/** 已处理表数量 */
|
|
520
|
-
processedTables: number;
|
|
521
|
-
/** 创建表数量 */
|
|
522
|
-
createdTables: number;
|
|
523
|
-
/** 修改表数量 */
|
|
524
|
-
modifiedTables: number;
|
|
525
|
-
/** 新增字段数量 */
|
|
526
|
-
addFields: number;
|
|
527
|
-
/** 类型变更数量 */
|
|
528
|
-
typeChanges: number;
|
|
529
|
-
/** 最大值变更数量 */
|
|
530
|
-
maxChanges: number;
|
|
531
|
-
/** 最小值变更数量 */
|
|
532
|
-
minChanges: number;
|
|
533
|
-
/** 默认值变更数量 */
|
|
534
|
-
defaultChanges: number;
|
|
535
|
-
/** 名称变更数量 */
|
|
536
|
-
nameChanges: number;
|
|
537
|
-
/** 索引创建数量 */
|
|
538
|
-
indexCreate: number;
|
|
539
|
-
/** 索引删除数量 */
|
|
540
|
-
indexDrop: number;
|
|
541
|
-
}
|
package/types/hook.d.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Befly 钩子系统类型定义
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import type { BeflyContext } from "./befly.ts";
|
|
6
|
-
import type { RequestContext } from "./context.ts";
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* 钩子处理函数类型(串联模式,无 next 参数)
|
|
10
|
-
*/
|
|
11
|
-
export type HookHandler = (befly: BeflyContext, ctx: RequestContext) => Promise<void> | void;
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* 钩子配置类型
|
|
15
|
-
*/
|
|
16
|
-
export interface Hook {
|
|
17
|
-
/** 钩子名称(运行时动态添加,由文件名生成) */
|
|
18
|
-
name?: string;
|
|
19
|
-
|
|
20
|
-
/** 依赖的钩子列表(在这些钩子之后执行) */
|
|
21
|
-
deps: string[];
|
|
22
|
-
|
|
23
|
-
/** 钩子处理函数 */
|
|
24
|
-
handler: HookHandler;
|
|
25
|
-
}
|