befly 3.9.12 → 3.9.14
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/docs/README.md +85 -0
- package/docs/addon.md +512 -0
- package/docs/api.md +1604 -0
- package/docs/cipher.md +580 -0
- package/docs/config.md +638 -0
- package/docs/database.md +147 -3
- package/docs/examples.md +892 -0
- package/docs/hook.md +754 -0
- package/docs/logger.md +495 -0
- package/docs/plugin.md +978 -0
- package/docs/quickstart.md +331 -0
- package/docs/sync.md +586 -0
- package/docs/table.md +765 -0
- package/docs/validator.md +618 -0
- package/loader/loadApis.ts +33 -36
- package/package.json +3 -3
- package/sync/syncDb/apply.ts +1 -1
- package/sync/syncDb/constants.ts +0 -11
- package/sync/syncDb/helpers.ts +0 -1
- package/sync/syncDb/table.ts +2 -2
- package/sync/syncDb/tableCreate.ts +3 -3
- package/tests/syncDb-constants.test.ts +1 -23
- package/tests/syncDb-helpers.test.ts +0 -1
- package/types/database.d.ts +0 -2
package/docs/database.md
CHANGED
|
@@ -9,6 +9,12 @@
|
|
|
9
9
|
- [核心概念](#核心概念)
|
|
10
10
|
- [DbHelper](#dbhelper)
|
|
11
11
|
- [自动转换](#自动转换)
|
|
12
|
+
- [自动过滤 null 和 undefined](#自动过滤-null-和-undefined)
|
|
13
|
+
- [写入时自动过滤](#写入时自动过滤)
|
|
14
|
+
- [更新时自动过滤](#更新时自动过滤)
|
|
15
|
+
- [Where 条件自动过滤](#where-条件自动过滤)
|
|
16
|
+
- [实际应用示例](#实际应用示例)
|
|
17
|
+
- [手动清理字段](#手动清理字段)
|
|
12
18
|
- [字段命名规范](#字段命名规范)
|
|
13
19
|
- [查询方法](#查询方法)
|
|
14
20
|
- [getOne - 查询单条](#getone---查询单条)
|
|
@@ -79,6 +85,126 @@ handler: async (befly, ctx) => {
|
|
|
79
85
|
- **字段名**:写入时小驼峰转下划线,查询时下划线转小驼峰
|
|
80
86
|
- **BIGINT 字段**:`id`、`*Id`、`*_id`、`*At`、`*_at` 自动转为 number
|
|
81
87
|
|
|
88
|
+
### 自动过滤 null 和 undefined
|
|
89
|
+
|
|
90
|
+
所有写入方法(`insData`、`insBatch`、`updData`)和条件查询(`where`)都会**自动过滤值为 `null` 或 `undefined` 的字段**。
|
|
91
|
+
|
|
92
|
+
这意味着:
|
|
93
|
+
|
|
94
|
+
- 传入 `undefined` 或 `null` 的字段会被忽略,不会写入数据库
|
|
95
|
+
- `where` 条件中值为 `undefined` 或 `null` 的条件会被忽略
|
|
96
|
+
- 这使得处理可选参数变得非常简单,无需手动判断
|
|
97
|
+
|
|
98
|
+
#### 写入时自动过滤
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
// 用户提交的数据可能部分字段为空
|
|
102
|
+
await befly.db.insData({
|
|
103
|
+
table: 'user',
|
|
104
|
+
data: {
|
|
105
|
+
username: 'john',
|
|
106
|
+
email: 'john@example.com',
|
|
107
|
+
phone: undefined, // ❌ 自动忽略,不会写入
|
|
108
|
+
avatar: null, // ❌ 自动忽略,不会写入
|
|
109
|
+
nickname: '' // ✅ 空字符串会写入(不是 null/undefined)
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
// 实际 SQL: INSERT INTO user (username, email, nickname, ...) VALUES ('john', 'john@example.com', '', ...)
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
#### 更新时自动过滤
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
// 只更新用户提交的字段
|
|
119
|
+
await befly.db.updData({
|
|
120
|
+
table: 'user',
|
|
121
|
+
data: {
|
|
122
|
+
nickname: ctx.body.nickname, // 如果用户传了值,会更新
|
|
123
|
+
avatar: ctx.body.avatar, // 如果为 undefined,自动忽略
|
|
124
|
+
bio: ctx.body.bio // 如果为 null,自动忽略
|
|
125
|
+
},
|
|
126
|
+
where: { id: ctx.user.id }
|
|
127
|
+
});
|
|
128
|
+
// 只有非 null/undefined 的字段会被更新
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
#### Where 条件自动过滤
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
// 条件筛选:用户可能只传部分筛选条件
|
|
135
|
+
const result = await befly.db.getList({
|
|
136
|
+
table: 'article',
|
|
137
|
+
where: {
|
|
138
|
+
categoryId: ctx.body.categoryId, // 如果未传,值为 undefined,自动忽略
|
|
139
|
+
status: ctx.body.status, // 如果未传,值为 undefined,自动忽略
|
|
140
|
+
authorId: ctx.body.authorId // 如果未传,值为 undefined,自动忽略
|
|
141
|
+
},
|
|
142
|
+
page: 1,
|
|
143
|
+
limit: 10
|
|
144
|
+
});
|
|
145
|
+
// 只有非 null/undefined 的条件会参与 WHERE 构建
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
#### 实际应用示例
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
// API: 用户列表(带可选筛选条件)
|
|
152
|
+
export default {
|
|
153
|
+
name: '用户列表',
|
|
154
|
+
fields: {
|
|
155
|
+
keyword: { name: '关键词', type: 'string', max: 50 },
|
|
156
|
+
roleId: { name: '角色ID', type: 'number' },
|
|
157
|
+
state: { name: '状态', type: 'number' }
|
|
158
|
+
},
|
|
159
|
+
handler: async (befly, ctx) => {
|
|
160
|
+
// 直接使用请求参数,无需判断是否存在
|
|
161
|
+
// null/undefined 的条件会被自动过滤
|
|
162
|
+
const result = await befly.db.getList({
|
|
163
|
+
table: 'user',
|
|
164
|
+
where: {
|
|
165
|
+
roleId: ctx.body.roleId, // 未传时为 undefined,自动忽略
|
|
166
|
+
state: ctx.body.state, // 未传时为 undefined,自动忽略
|
|
167
|
+
username$like: ctx.body.keyword ? `%${ctx.body.keyword}%` : undefined // 无关键词时忽略
|
|
168
|
+
},
|
|
169
|
+
page: ctx.body.page || 1,
|
|
170
|
+
limit: ctx.body.limit || 10
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
return befly.tool.Yes('查询成功', result);
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
#### 手动清理字段
|
|
179
|
+
|
|
180
|
+
如需手动清理数据,可以使用 `cleanFields` 方法:
|
|
181
|
+
|
|
182
|
+
```typescript
|
|
183
|
+
// 默认排除 null 和 undefined
|
|
184
|
+
const cleanData = befly.db.cleanFields({
|
|
185
|
+
name: 'John',
|
|
186
|
+
age: null,
|
|
187
|
+
email: undefined,
|
|
188
|
+
phone: ''
|
|
189
|
+
});
|
|
190
|
+
// 结果: { name: 'John', phone: '' }
|
|
191
|
+
|
|
192
|
+
// 自定义排除值(如同时排除空字符串)
|
|
193
|
+
const cleanData2 = befly.db.cleanFields(
|
|
194
|
+
{ name: 'John', phone: '', age: null },
|
|
195
|
+
[null, undefined, ''] // 排除这些值
|
|
196
|
+
);
|
|
197
|
+
// 结果: { name: 'John' }
|
|
198
|
+
|
|
199
|
+
// 保留特定字段的特定值(即使在排除列表中)
|
|
200
|
+
const cleanData3 = befly.db.cleanFields(
|
|
201
|
+
{ name: 'John', status: null, count: 0 },
|
|
202
|
+
[null, undefined], // 排除 null 和 undefined
|
|
203
|
+
{ status: null } // 但保留 status 字段的 null 值
|
|
204
|
+
);
|
|
205
|
+
// 结果: { name: 'John', status: null, count: 0 }
|
|
206
|
+
```
|
|
207
|
+
|
|
82
208
|
---
|
|
83
209
|
|
|
84
210
|
## 字段命名规范
|
|
@@ -889,14 +1015,32 @@ orderBy: ['sort#ASC', 'id#DESC'];
|
|
|
889
1015
|
|
|
890
1016
|
### 默认 State 过滤
|
|
891
1017
|
|
|
892
|
-
所有查询方法默认添加 `state > 0`
|
|
1018
|
+
所有查询方法默认添加 `state > 0` 条件,**仅过滤软删除的数据(state=0)**。
|
|
1019
|
+
|
|
1020
|
+
**过滤效果**:
|
|
1021
|
+
|
|
1022
|
+
| state 值 | 默认查询结果 |
|
|
1023
|
+
| -------- | ------------------- |
|
|
1024
|
+
| 0 | ❌ 被过滤(软删除) |
|
|
1025
|
+
| 1 | ✅ 可查询(正常) |
|
|
1026
|
+
| 2 | ✅ 可查询(禁用) |
|
|
1027
|
+
|
|
1028
|
+
> ⚠️ **注意**:禁用数据(state=2)默认**可以**查询到,如需过滤禁用数据,需显式指定 `state: 1`。
|
|
893
1029
|
|
|
894
1030
|
```typescript
|
|
895
|
-
//
|
|
1031
|
+
// 默认查询:state > 0,包含正常和禁用数据
|
|
896
1032
|
getOne({ table: 'user', where: { id: 1 } });
|
|
897
1033
|
// → WHERE id = 1 AND state > 0
|
|
898
1034
|
|
|
899
|
-
//
|
|
1035
|
+
// 只查询正常状态的数据
|
|
1036
|
+
getList({ table: 'user', where: { state: 1 } });
|
|
1037
|
+
// → WHERE state = 1
|
|
1038
|
+
|
|
1039
|
+
// 只查询禁用状态的数据
|
|
1040
|
+
getList({ table: 'user', where: { state: 2 } });
|
|
1041
|
+
// → WHERE state = 2
|
|
1042
|
+
|
|
1043
|
+
// 查询所有状态(包括软删除)
|
|
900
1044
|
getOne({ table: 'user', where: { id: 1, state$gte: 0 } });
|
|
901
1045
|
// → WHERE id = 1 AND state >= 0
|
|
902
1046
|
```
|