befly 3.9.14 → 3.9.21
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/checks/checkTable.ts +18 -0
- package/package.json +2 -2
- package/sync/syncDb/table.ts +2 -1
package/checks/checkTable.ts
CHANGED
|
@@ -37,6 +37,11 @@ const RESERVED_FIELDS = ['id', 'created_at', 'updated_at', 'deleted_at', 'state'
|
|
|
37
37
|
*/
|
|
38
38
|
const FIELD_TYPES = ['string', 'number', 'text', 'array_string', 'array_text'] as const;
|
|
39
39
|
|
|
40
|
+
/**
|
|
41
|
+
* 允许的字段属性列表
|
|
42
|
+
*/
|
|
43
|
+
const ALLOWED_FIELD_PROPERTIES = ['name', 'type', 'min', 'max', 'default', 'detail', 'index', 'unique', 'nullable', 'unsigned', 'regexp'] as const;
|
|
44
|
+
|
|
40
45
|
/**
|
|
41
46
|
* 小驼峰命名正则
|
|
42
47
|
* 可选:以下划线开头(用于特殊文件,如通用字段定义)
|
|
@@ -133,6 +138,14 @@ export async function checkTable(): Promise<void> {
|
|
|
133
138
|
// 直接使用字段对象
|
|
134
139
|
const field = fieldDef as FieldDefinition;
|
|
135
140
|
|
|
141
|
+
// 检查是否存在非法属性
|
|
142
|
+
const fieldKeys = Object.keys(field);
|
|
143
|
+
const illegalProps = fieldKeys.filter((key) => !ALLOWED_FIELD_PROPERTIES.includes(key as any));
|
|
144
|
+
if (illegalProps.length > 0) {
|
|
145
|
+
Logger.warn(`${item.typeName}表 ${fileName} 文件 ${colKey} 包含非法属性: ${illegalProps.join(', ')},` + `允许的属性为: ${ALLOWED_FIELD_PROPERTIES.join(', ')}`);
|
|
146
|
+
hasError = true;
|
|
147
|
+
}
|
|
148
|
+
|
|
136
149
|
// 检查必填字段:name, type
|
|
137
150
|
if (!field.name || typeof field.name !== 'string') {
|
|
138
151
|
Logger.warn(`${item.typeName}表 ${fileName} 文件 ${colKey} 缺少必填字段 name 或类型错误`);
|
|
@@ -193,6 +206,11 @@ export async function checkTable(): Promise<void> {
|
|
|
193
206
|
hasError = true;
|
|
194
207
|
}
|
|
195
208
|
|
|
209
|
+
// 检查 unique 和 index 冲突(警告但不阻断)
|
|
210
|
+
if (field.unique && field.index) {
|
|
211
|
+
Logger.warn(`${item.typeName}表 ${fileName} 文件 ${colKey} 同时设置了 unique=true 和 index=true,` + `unique 约束会自动创建唯一索引,index=true 将被忽略以避免重复索引`);
|
|
212
|
+
}
|
|
213
|
+
|
|
196
214
|
// 约束:当最小值与最大值均为数字时,要求最小值 <= 最大值
|
|
197
215
|
if (fieldMin !== undefined && fieldMax !== undefined && fieldMin !== null && fieldMax !== null) {
|
|
198
216
|
if (fieldMin > fieldMax) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "befly",
|
|
3
|
-
"version": "3.9.
|
|
3
|
+
"version": "3.9.21",
|
|
4
4
|
"description": "Befly - 为 Bun 专属打造的 TypeScript API 接口框架核心引擎",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"private": false,
|
|
@@ -74,7 +74,7 @@
|
|
|
74
74
|
"pino": "^10.1.0",
|
|
75
75
|
"pino-roll": "^4.0.0"
|
|
76
76
|
},
|
|
77
|
-
"gitHead": "
|
|
77
|
+
"gitHead": "d0825d638bbb779137fb8860049bcc1b3200a5dc",
|
|
78
78
|
"devDependencies": {
|
|
79
79
|
"typescript": "^5.9.3"
|
|
80
80
|
}
|
package/sync/syncDb/table.ts
CHANGED
|
@@ -161,7 +161,8 @@ export async function modifyTable(sql: SQL, tableName: string, fields: Record<st
|
|
|
161
161
|
const dbFieldName = snakeCase(fieldKey);
|
|
162
162
|
|
|
163
163
|
const indexName = `idx_${dbFieldName}`;
|
|
164
|
-
|
|
164
|
+
// 如果字段有 unique 约束,跳过创建普通索引(unique 会自动创建唯一索引)
|
|
165
|
+
if (fieldDef.index && !fieldDef.unique && !existingIndexes[indexName]) {
|
|
165
166
|
indexActions.push({ action: 'create', indexName: indexName, fieldName: dbFieldName });
|
|
166
167
|
changed = true;
|
|
167
168
|
} else if (!fieldDef.index && existingIndexes[indexName] && existingIndexes[indexName].length === 1) {
|