befly-tpl 3.9.40 → 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/package.json +5 -4
- package/tests/fields-exclude.test.ts +136 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "befly-tpl",
|
|
3
|
-
"version": "3.9.
|
|
3
|
+
"version": "3.9.41",
|
|
4
4
|
"description": "Befly 3.0 TypeScript Template",
|
|
5
5
|
"private": false,
|
|
6
6
|
"publishConfig": {
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
"files": [
|
|
16
16
|
"apis",
|
|
17
17
|
"tables",
|
|
18
|
+
"tests",
|
|
18
19
|
"main.ts",
|
|
19
20
|
".npmrc",
|
|
20
21
|
"bunfig.toml",
|
|
@@ -28,11 +29,11 @@
|
|
|
28
29
|
],
|
|
29
30
|
"type": "module",
|
|
30
31
|
"dependencies": {
|
|
31
|
-
"@befly-addon/admin": "1.0.
|
|
32
|
-
"befly": "3.8.
|
|
32
|
+
"@befly-addon/admin": "1.0.46",
|
|
33
|
+
"befly": "3.8.21"
|
|
33
34
|
},
|
|
34
35
|
"engines": {
|
|
35
36
|
"bun": ">=1.3.0"
|
|
36
37
|
},
|
|
37
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "ac90b47c807b2c016ac2331b56814e335a82b291"
|
|
38
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();
|