befly 3.9.38 → 3.9.40
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 -38
- package/befly.config.ts +62 -40
- package/checks/checkApi.ts +16 -16
- package/checks/checkApp.ts +19 -25
- package/checks/checkTable.ts +42 -42
- package/docs/README.md +42 -35
- package/docs/{api.md → api/api.md} +223 -231
- package/docs/cipher.md +71 -69
- package/docs/database.md +143 -141
- package/docs/{examples.md → guide/examples.md} +181 -181
- package/docs/guide/quickstart.md +331 -0
- package/docs/hooks/auth.md +38 -0
- package/docs/hooks/cors.md +28 -0
- package/docs/{hook.md → hooks/hook.md} +140 -57
- package/docs/hooks/parser.md +19 -0
- package/docs/hooks/rateLimit.md +47 -0
- package/docs/{redis.md → infra/redis.md} +84 -93
- package/docs/plugins/cipher.md +61 -0
- package/docs/plugins/database.md +128 -0
- package/docs/{plugin.md → plugins/plugin.md} +83 -81
- package/docs/quickstart.md +26 -26
- package/docs/{addon.md → reference/addon.md} +46 -46
- package/docs/{config.md → reference/config.md} +32 -80
- package/docs/{logger.md → reference/logger.md} +52 -52
- package/docs/{sync.md → reference/sync.md} +32 -35
- package/docs/{table.md → reference/table.md} +1 -1
- package/docs/{validator.md → reference/validator.md} +57 -57
- package/hooks/auth.ts +8 -4
- package/hooks/cors.ts +13 -13
- package/hooks/parser.ts +37 -17
- package/hooks/permission.ts +26 -14
- package/hooks/rateLimit.ts +276 -0
- package/hooks/validator.ts +8 -8
- package/lib/asyncContext.ts +43 -0
- package/lib/cacheHelper.ts +212 -77
- package/lib/cacheKeys.ts +38 -0
- package/lib/cipher.ts +30 -30
- package/lib/connect.ts +28 -28
- package/lib/dbHelper.ts +183 -102
- package/lib/jwt.ts +16 -16
- package/lib/logger.ts +610 -19
- package/lib/redisHelper.ts +185 -44
- package/lib/sqlBuilder.ts +90 -91
- package/lib/validator.ts +59 -39
- package/loader/loadApis.ts +48 -44
- package/loader/loadHooks.ts +40 -14
- package/loader/loadPlugins.ts +16 -17
- package/main.ts +57 -47
- package/package.json +47 -45
- package/paths.ts +15 -14
- package/plugins/cache.ts +5 -4
- package/plugins/cipher.ts +3 -3
- package/plugins/config.ts +2 -2
- package/plugins/db.ts +9 -9
- package/plugins/jwt.ts +3 -3
- package/plugins/logger.ts +8 -12
- package/plugins/redis.ts +8 -8
- package/plugins/tool.ts +6 -6
- package/router/api.ts +85 -56
- package/router/static.ts +12 -12
- package/sync/syncAll.ts +12 -12
- package/sync/syncApi.ts +55 -52
- package/sync/syncDb/apply.ts +20 -19
- package/sync/syncDb/constants.ts +25 -23
- package/sync/syncDb/ddl.ts +35 -36
- package/sync/syncDb/helpers.ts +6 -9
- package/sync/syncDb/schema.ts +10 -9
- package/sync/syncDb/sqlite.ts +7 -8
- package/sync/syncDb/table.ts +37 -35
- package/sync/syncDb/tableCreate.ts +21 -20
- package/sync/syncDb/types.ts +23 -20
- package/sync/syncDb/version.ts +10 -10
- package/sync/syncDb.ts +43 -36
- package/sync/syncDev.ts +74 -65
- package/sync/syncMenu.ts +190 -55
- package/tests/api-integration-array-number.test.ts +282 -0
- package/tests/befly-config-env.test.ts +78 -0
- package/tests/cacheHelper.test.ts +135 -104
- package/tests/cacheKeys.test.ts +41 -0
- package/tests/cipher.test.ts +90 -89
- package/tests/dbHelper-advanced.test.ts +140 -134
- package/tests/dbHelper-all-array-types.test.ts +316 -0
- package/tests/dbHelper-array-serialization.test.ts +258 -0
- package/tests/dbHelper-columns.test.ts +56 -55
- package/tests/dbHelper-execute.test.ts +45 -44
- package/tests/dbHelper-joins.test.ts +124 -119
- package/tests/fields-redis-cache.test.ts +29 -27
- package/tests/fields-validate.test.ts +38 -38
- package/tests/getClientIp.test.ts +54 -0
- package/tests/integration.test.ts +69 -67
- package/tests/jwt.test.ts +27 -26
- package/tests/logger.test.ts +267 -34
- package/tests/rateLimit-hook.test.ts +477 -0
- package/tests/redisHelper.test.ts +187 -188
- package/tests/redisKeys.test.ts +6 -73
- package/tests/scanConfig.test.ts +144 -0
- package/tests/sqlBuilder-advanced.test.ts +217 -215
- package/tests/sqlBuilder.test.ts +92 -91
- package/tests/sync-connection.test.ts +29 -29
- package/tests/syncDb-apply.test.ts +97 -96
- package/tests/syncDb-array-number.test.ts +160 -0
- package/tests/syncDb-constants.test.ts +48 -47
- package/tests/syncDb-ddl.test.ts +99 -98
- package/tests/syncDb-helpers.test.ts +29 -28
- package/tests/syncDb-schema.test.ts +61 -60
- package/tests/syncDb-types.test.ts +60 -59
- package/tests/syncMenu-paths.test.ts +68 -0
- package/tests/util.test.ts +42 -41
- package/tests/validator-array-number.test.ts +310 -0
- package/tests/validator-default.test.ts +373 -0
- package/tests/validator.test.ts +271 -266
- package/tsconfig.json +4 -5
- package/types/api.d.ts +7 -12
- package/types/befly.d.ts +60 -13
- package/types/cache.d.ts +8 -4
- package/types/common.d.ts +17 -9
- package/types/context.d.ts +2 -2
- package/types/crypto.d.ts +23 -0
- package/types/database.d.ts +19 -19
- package/types/hook.d.ts +2 -2
- package/types/jwt.d.ts +118 -0
- package/types/logger.d.ts +30 -0
- package/types/plugin.d.ts +4 -4
- package/types/redis.d.ts +7 -3
- package/types/roleApisCache.ts +23 -0
- package/types/sync.d.ts +10 -10
- package/types/table.d.ts +50 -9
- package/types/validate.d.ts +69 -0
- package/utils/addonHelper.ts +90 -0
- package/utils/arrayKeysToCamel.ts +18 -0
- package/utils/calcPerfTime.ts +13 -0
- package/utils/configTypes.ts +3 -0
- package/utils/cors.ts +19 -0
- package/utils/fieldClear.ts +75 -0
- package/utils/genShortId.ts +12 -0
- package/utils/getClientIp.ts +45 -0
- package/utils/keysToCamel.ts +22 -0
- package/utils/keysToSnake.ts +22 -0
- package/utils/modules.ts +98 -0
- package/utils/pickFields.ts +19 -0
- package/utils/process.ts +56 -0
- package/utils/regex.ts +225 -0
- package/utils/response.ts +115 -0
- package/utils/route.ts +23 -0
- package/utils/scanConfig.ts +142 -0
- package/utils/scanFiles.ts +48 -0
- package/.prettierignore +0 -2
- package/.prettierrc +0 -12
- package/docs/1-/345/237/272/346/234/254/344/273/213/347/273/215.md +0 -35
- package/docs/2-/345/210/235/346/255/245/344/275/223/351/252/214.md +0 -64
- package/docs/3-/347/254/254/344/270/200/344/270/252/346/216/245/345/217/243.md +0 -46
- package/docs/4-/346/223/215/344/275/234/346/225/260/346/215/256/345/272/223.md +0 -172
- package/hooks/requestLogger.ts +0 -84
- package/types/index.ts +0 -24
- package/util.ts +0 -283
package/lib/connect.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
2
|
* 数据库连接管理器
|
|
3
3
|
* 统一管理 SQL 和 Redis 连接
|
|
4
4
|
* 配置从 beflyConfig 全局对象获取
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import { SQL, RedisClient } from
|
|
7
|
+
import { SQL, RedisClient } from "bun";
|
|
8
8
|
|
|
9
|
-
import { beflyConfig } from
|
|
10
|
-
import { Logger } from
|
|
9
|
+
import { beflyConfig } from "../befly.config.js";
|
|
10
|
+
import { Logger } from "./logger.js";
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* 数据库连接管理器
|
|
@@ -36,26 +36,26 @@ export class Connect {
|
|
|
36
36
|
const config = beflyConfig.db || {};
|
|
37
37
|
|
|
38
38
|
// 构建数据库连接字符串
|
|
39
|
-
const type = config.type ||
|
|
40
|
-
const host = config.host ||
|
|
39
|
+
const type = config.type || "mysql";
|
|
40
|
+
const host = config.host || "127.0.0.1";
|
|
41
41
|
const port = config.port || 3306;
|
|
42
|
-
const user = encodeURIComponent(config.username ||
|
|
43
|
-
const password = encodeURIComponent(config.password ||
|
|
44
|
-
const database = encodeURIComponent(config.database ||
|
|
42
|
+
const user = encodeURIComponent(config.username || "root");
|
|
43
|
+
const password = encodeURIComponent(config.password || "root");
|
|
44
|
+
const database = encodeURIComponent(config.database || "befly_demo");
|
|
45
45
|
|
|
46
46
|
let finalUrl: string;
|
|
47
|
-
if (type ===
|
|
47
|
+
if (type === "sqlite") {
|
|
48
48
|
finalUrl = database;
|
|
49
49
|
} else {
|
|
50
50
|
if (!host || !database) {
|
|
51
|
-
throw new Error(
|
|
51
|
+
throw new Error("数据库配置不完整,请检查配置参数");
|
|
52
52
|
}
|
|
53
53
|
finalUrl = `${type}://${user}:${password}@${host}:${port}/${database}`;
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
let sql: SQL;
|
|
57
57
|
|
|
58
|
-
if (type ===
|
|
58
|
+
if (type === "sqlite") {
|
|
59
59
|
sql = new SQL(finalUrl);
|
|
60
60
|
} else {
|
|
61
61
|
sql = new SQL({
|
|
@@ -69,11 +69,11 @@ export class Connect {
|
|
|
69
69
|
const timeout = 30000;
|
|
70
70
|
|
|
71
71
|
const healthCheckPromise = (async () => {
|
|
72
|
-
let version =
|
|
73
|
-
if (type ===
|
|
72
|
+
let version = "";
|
|
73
|
+
if (type === "sqlite") {
|
|
74
74
|
const v = await sql`SELECT sqlite_version() AS version`;
|
|
75
75
|
version = v?.[0]?.version;
|
|
76
|
-
} else if (type ===
|
|
76
|
+
} else if (type === "postgresql" || type === "postgres") {
|
|
77
77
|
const v = await sql`SELECT version() AS version`;
|
|
78
78
|
version = v?.[0]?.version;
|
|
79
79
|
} else {
|
|
@@ -89,17 +89,17 @@ export class Connect {
|
|
|
89
89
|
}, timeout);
|
|
90
90
|
});
|
|
91
91
|
|
|
92
|
-
|
|
92
|
+
await Promise.race([healthCheckPromise, timeoutPromise]);
|
|
93
93
|
|
|
94
94
|
this.sqlClient = sql;
|
|
95
95
|
this.sqlConnectedAt = Date.now();
|
|
96
96
|
this.sqlPoolMax = config.poolMax ?? 1;
|
|
97
97
|
return sql;
|
|
98
98
|
} catch (error: any) {
|
|
99
|
-
Logger.error({ err: error },
|
|
99
|
+
Logger.error({ err: error }, "[Connect] SQL 连接失败");
|
|
100
100
|
try {
|
|
101
101
|
await sql?.close();
|
|
102
|
-
} catch
|
|
102
|
+
} catch {}
|
|
103
103
|
|
|
104
104
|
throw error;
|
|
105
105
|
}
|
|
@@ -113,7 +113,7 @@ export class Connect {
|
|
|
113
113
|
try {
|
|
114
114
|
await this.sqlClient.close();
|
|
115
115
|
} catch (error: any) {
|
|
116
|
-
Logger.error({ err: error },
|
|
116
|
+
Logger.error({ err: error }, "[Connect] 关闭 SQL 连接时出错");
|
|
117
117
|
}
|
|
118
118
|
this.sqlClient = null;
|
|
119
119
|
this.sqlConnectedAt = null;
|
|
@@ -126,7 +126,7 @@ export class Connect {
|
|
|
126
126
|
*/
|
|
127
127
|
static getSql(): SQL {
|
|
128
128
|
if (!this.sqlClient) {
|
|
129
|
-
throw new Error(
|
|
129
|
+
throw new Error("SQL 客户端未连接,请先调用 Connect.connectSql()");
|
|
130
130
|
}
|
|
131
131
|
return this.sqlClient;
|
|
132
132
|
}
|
|
@@ -145,13 +145,13 @@ export class Connect {
|
|
|
145
145
|
|
|
146
146
|
try {
|
|
147
147
|
// 构建 Redis URL
|
|
148
|
-
const host = config.host ||
|
|
148
|
+
const host = config.host || "127.0.0.1";
|
|
149
149
|
const port = config.port || 6379;
|
|
150
|
-
const username = config.username ||
|
|
151
|
-
const password = config.password ||
|
|
150
|
+
const username = config.username || "";
|
|
151
|
+
const password = config.password || "";
|
|
152
152
|
const db = config.db || 0;
|
|
153
153
|
|
|
154
|
-
let auth =
|
|
154
|
+
let auth = "";
|
|
155
155
|
if (username && password) {
|
|
156
156
|
auth = `${username}:${password}@`;
|
|
157
157
|
} else if (password) {
|
|
@@ -175,7 +175,7 @@ export class Connect {
|
|
|
175
175
|
this.redisConnectedAt = Date.now();
|
|
176
176
|
return redis;
|
|
177
177
|
} catch (error: any) {
|
|
178
|
-
Logger.error({ err: error },
|
|
178
|
+
Logger.error({ err: error }, "[Connect] Redis 连接失败");
|
|
179
179
|
throw new Error(`Redis 连接失败: ${error.message}`);
|
|
180
180
|
}
|
|
181
181
|
}
|
|
@@ -189,7 +189,7 @@ export class Connect {
|
|
|
189
189
|
this.redisClient.close();
|
|
190
190
|
this.redisConnectedAt = null;
|
|
191
191
|
} catch (error: any) {
|
|
192
|
-
Logger.error({ err: error },
|
|
192
|
+
Logger.error({ err: error }, "[Connect] 关闭 Redis 连接时出错");
|
|
193
193
|
}
|
|
194
194
|
this.redisClient = null;
|
|
195
195
|
}
|
|
@@ -201,7 +201,7 @@ export class Connect {
|
|
|
201
201
|
*/
|
|
202
202
|
static getRedis(): RedisClient {
|
|
203
203
|
if (!this.redisClient) {
|
|
204
|
-
throw new Error(
|
|
204
|
+
throw new Error("Redis 客户端未连接,请先调用 Connect.connectRedis()");
|
|
205
205
|
}
|
|
206
206
|
return this.redisClient;
|
|
207
207
|
}
|
|
@@ -222,7 +222,7 @@ export class Connect {
|
|
|
222
222
|
// 连接 Redis
|
|
223
223
|
await this.connectRedis();
|
|
224
224
|
} catch (error: any) {
|
|
225
|
-
Logger.error({ err: error },
|
|
225
|
+
Logger.error({ err: error }, "数据库初始化失败");
|
|
226
226
|
await this.disconnect();
|
|
227
227
|
throw error;
|
|
228
228
|
}
|