befly-tpl 3.9.39 → 3.9.41

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 CHANGED
@@ -55,7 +55,6 @@ bun run main.ts
55
55
 
56
56
  ```typescript
57
57
  // apis/user/hello.ts
58
- import { Yes } from 'befly';
59
58
  import type { ApiRoute } from 'befly';
60
59
 
61
60
  export default {
@@ -63,9 +62,12 @@ export default {
63
62
  auth: false, // 公开接口
64
63
  fields: {},
65
64
  handler: async (befly, ctx) => {
66
- return Yes('Hello, Befly!', {
67
- timestamp: Date.now()
68
- });
65
+ return {
66
+ msg: 'Hello, Befly!',
67
+ data: {
68
+ timestamp: Date.now()
69
+ }
70
+ };
69
71
  }
70
72
  } as ApiRoute;
71
73
  ```
@@ -77,7 +79,6 @@ export default {
77
79
  ### TypeScript 全面支持
78
80
 
79
81
  ```typescript
80
- import { Yes } from 'befly';
81
82
  import type { ApiRoute, BeflyContext } from 'befly';
82
83
  import type { User } from './types/models';
83
84
 
@@ -97,7 +98,7 @@ export default {
97
98
  where: { id }
98
99
  });
99
100
 
100
- return Yes('查询成功', user);
101
+ return { msg: '查询成功', data: user };
101
102
  }
102
103
  } as ApiRoute;
103
104
  ```
package/apis/test/hi.ts CHANGED
@@ -1,9 +1,7 @@
1
- import { Yes } from 'befly';
2
-
3
1
  export default {
4
2
  name: '测试接口',
5
3
  handler: async (befly, ctx) => {
6
4
  // 返回成功信息
7
- return Yes('测试成功');
5
+ return '测试成功';
8
6
  }
9
7
  };
package/main.ts CHANGED
@@ -1,4 +1,10 @@
1
1
  import { Befly } from 'befly';
2
2
 
3
- const app = new Befly();
4
- await app.listen();
3
+ export const app = new Befly({
4
+ cors: {
5
+ origin: process.env.CORS_ALLOWED_ORIGIN,
6
+ methods: process.env.CORS_ALLOWED_METHODS
7
+ }
8
+ });
9
+
10
+ await app.start();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "befly-tpl",
3
- "version": "3.9.39",
3
+ "version": "3.9.41",
4
4
  "description": "Befly 3.0 TypeScript Template",
5
5
  "private": false,
6
6
  "publishConfig": {
@@ -10,17 +10,15 @@
10
10
  "scripts": {
11
11
  "dev": "NODE_ENV=development bun run main.ts",
12
12
  "start": "NODE_ENV=production bun run main.ts",
13
- "pm2": "bun --bun pm2 start pm2.config.cjs",
14
- "sync:api:dev": "NODE_ENV=development bunx befly sync:api",
15
- "sync:api:prod": "NODE_ENV=production bunx befly sync:api"
13
+ "pm2": "bun --bun pm2 start pm2.config.cjs"
16
14
  },
17
15
  "files": [
18
16
  "apis",
19
17
  "tables",
18
+ "tests",
20
19
  "main.ts",
21
20
  ".npmrc",
22
21
  "bunfig.toml",
23
- "env.ts",
24
22
  "LICENSE",
25
23
  "main.ts",
26
24
  "menu.json",
@@ -31,12 +29,11 @@
31
29
  ],
32
30
  "type": "module",
33
31
  "dependencies": {
34
- "@befly-addon/admin": "1.0.44",
35
- "befly": "3.8.19",
36
- "befly-cli": "3.9.24"
32
+ "@befly-addon/admin": "1.0.46",
33
+ "befly": "3.8.21"
37
34
  },
38
35
  "engines": {
39
36
  "bun": ">=1.3.0"
40
37
  },
41
- "gitHead": "0619a7cfdd889be9fdd1b187c3d6522288707df0"
38
+ "gitHead": "ac90b47c807b2c016ac2331b56814e335a82b291"
42
39
  }
@@ -0,0 +1,136 @@
1
+ /**
2
+ * 测试字段排除功能
3
+ * 验证 fields 的3种写法:空数组、全部包含、全部排除
4
+ */
5
+
6
+ import { Befly } from 'befly';
7
+
8
+ async function testFieldsExclude() {
9
+ // 强制启用数据库
10
+ process.env.DATABASE_ENABLE = '1';
11
+
12
+ // 创建 Befly 实例
13
+ const befly = new Befly();
14
+
15
+ // 启动服务器(这会初始化所有插件包括数据库)
16
+ const server = await befly.start();
17
+
18
+ // 获取 db 实例
19
+ const db = befly.appContext.db;
20
+ const redis = befly.appContext.redis;
21
+
22
+ console.log('\n========== 测试开始 ==========\n');
23
+
24
+ try {
25
+ // 测试1:空数组(查询所有字段)
26
+ console.log('【测试1】空数组 - 查询所有字段');
27
+ const result1 = await db.getOne({
28
+ table: 'addon_admin_user',
29
+ fields: [],
30
+ where: { id: 1 }
31
+ });
32
+ console.log('结果字段数:', Object.keys(result1 || {}).length);
33
+ console.log('字段列表:', Object.keys(result1 || {}).join(', '));
34
+
35
+ // 测试2:指定包含字段
36
+ console.log('\n【测试2】指定包含字段 - 只查询 id, name, email');
37
+ const result2 = await db.getOne({
38
+ table: 'addon_admin_user',
39
+ fields: ['id', 'name', 'email'],
40
+ where: { id: 1 }
41
+ });
42
+ console.log('结果字段数:', Object.keys(result2 || {}).length);
43
+ console.log('字段列表:', Object.keys(result2 || {}).join(', '));
44
+
45
+ // 测试3:排除字段(排除敏感字段)
46
+ console.log('\n【测试3】排除字段 - 排除 password, salt');
47
+ const result3 = await db.getOne({
48
+ table: 'addon_admin_user',
49
+ fields: ['!password', '!salt'],
50
+ where: { id: 1 }
51
+ });
52
+ console.log('结果字段数:', Object.keys(result3 || {}).length);
53
+ console.log('字段列表:', Object.keys(result3 || {}).join(', '));
54
+ console.log('是否包含 password:', 'password' in (result3 || {}));
55
+ console.log('是否包含 salt:', 'salt' in (result3 || {}));
56
+
57
+ // 测试4:getList 支持排除字段
58
+ console.log('\n【测试4】getList - 排除字段');
59
+ const result4 = await db.getList({
60
+ table: 'addon_admin_user',
61
+ fields: ['!password', '!salt'],
62
+ page: 1,
63
+ limit: 2
64
+ });
65
+ console.log('查询到记录数:', result4.lists.length);
66
+ if (result4.lists.length > 0) {
67
+ console.log('第一条记录字段:', Object.keys(result4.lists[0]).join(', '));
68
+ console.log('是否包含 password:', 'password' in result4.lists[0]);
69
+ }
70
+
71
+ // 测试5:getAll 支持排除字段
72
+ console.log('\n【测试5】getAll - 排除字段');
73
+ const result5 = await db.getAll({
74
+ table: 'addon_admin_user',
75
+ fields: ['!password', '!salt']
76
+ });
77
+ console.log('查询到记录数:', result5.length);
78
+ if (result5.length > 0) {
79
+ console.log('第一条记录字段:', Object.keys(result5[0]).join(', '));
80
+ console.log('是否包含 password:', 'password' in result5[0]);
81
+ }
82
+
83
+ // 测试6:混用检测(应该报错)
84
+ console.log('\n【测试6】混用检测 - 应该抛出错误');
85
+ try {
86
+ await db.getOne({
87
+ table: 'addon_admin_user',
88
+ fields: ['id', '!password'],
89
+ where: { id: 1 }
90
+ });
91
+ console.log('❌ 没有抛出错误(不符合预期)');
92
+ } catch (error: any) {
93
+ console.log('✅ 成功捕获错误:', error.message);
94
+ }
95
+
96
+ // 测试7:星号检测(应该报错)
97
+ console.log('\n【测试7】星号检测 - 应该抛出错误');
98
+ try {
99
+ await db.getOne({
100
+ table: 'addon_admin_user',
101
+ fields: ['*'],
102
+ where: { id: 1 }
103
+ });
104
+ console.log('❌ 没有抛出错误(不符合预期)');
105
+ } catch (error: any) {
106
+ console.log('✅ 成功捕获错误:', error.message);
107
+ }
108
+
109
+ // 测试8:缓存性能测试
110
+ console.log('\n【测试8】缓存性能测试 - 多次查询相同表');
111
+ const start = Date.now();
112
+ for (let i = 0; i < 5; i++) {
113
+ await db.getOne({
114
+ table: 'addon_admin_user',
115
+ fields: ['!password', '!salt'],
116
+ where: { id: 1 }
117
+ });
118
+ }
119
+ const duration = Date.now() - start;
120
+ console.log(`5次查询总耗时: ${duration}ms`);
121
+ console.log(`平均耗时: ${(duration / 5).toFixed(2)}ms`);
122
+
123
+ console.log('\n========== 测试完成 ==========\n');
124
+ } catch (error: any) {
125
+ console.error('\n❌ 测试失败:', error.message);
126
+ console.error(error.stack);
127
+ } finally {
128
+ // 关闭服务器和连接
129
+ server.stop(true);
130
+ await db.close();
131
+ await redis.close();
132
+ process.exit(0);
133
+ }
134
+ }
135
+
136
+ testFieldsExclude();
package/env.ts DELETED
@@ -1,12 +0,0 @@
1
- /**
2
- * 项目环境变量配置
3
- * 这里的配置会覆盖 core/env.ts 中的默认配置
4
- */
5
-
6
- import type { EnvConfig } from 'befly/types/env.js';
7
-
8
- /**
9
- * 项目自定义配置
10
- * 只需要配置需要覆盖的字段
11
- */
12
- export const Env: Partial<EnvConfig> = {};