befly 3.22.8 → 3.23.0

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/sync/menu.js CHANGED
@@ -1,3 +1,5 @@
1
+ import { buildPathSyncLists } from "./syncUtil.js";
2
+
1
3
  const MENU_TABLE_NAME = "beflyMenu";
2
4
 
3
5
  export function flattenMenusToDefMap(menus) {
@@ -47,55 +49,33 @@ export async function syncMenu(ctx, menus) {
47
49
  where: { state$gte: 0 }
48
50
  });
49
51
 
50
- const existingList = allExistingMenus.data.lists || [];
51
-
52
- const existingMenuMap = new Map();
53
- const delIdSet = new Set();
54
- for (const record of existingList) {
55
- if (!existingMenuMap.has(record.path)) {
56
- existingMenuMap.set(record.path, record);
57
- delIdSet.add(record.id);
58
- }
59
- }
60
-
61
- // 2) 一次性算出 insert/update(仅依赖 path diff,不使用 pid,不预生成 id)
62
- const updList = [];
63
- const insList = [];
64
-
65
- for (const def of menuDefMap.values()) {
66
- const existing = existingMenuMap.get(def.path);
67
- if (existing) {
68
- delIdSet.delete(existing.id);
69
- updList.push({
70
- id: existing.id,
71
- data: {
72
- name: def.name,
73
- path: def.path,
74
- parentPath: def.parentPath,
75
- sort: def.sort
76
- }
77
- });
78
- } else {
79
- insList.push({
80
- name: def.name,
81
- path: def.path,
82
- parentPath: def.parentPath,
83
- sort: def.sort
84
- });
85
- }
86
- }
52
+ const syncLists = buildPathSyncLists(
53
+ allExistingMenus.data.lists || [],
54
+ Array.from(menuDefMap.values()),
55
+ (def) => ({
56
+ name: def.name,
57
+ path: def.path,
58
+ parentPath: def.parentPath,
59
+ sort: def.sort
60
+ }),
61
+ (def) => ({
62
+ name: def.name,
63
+ path: def.path,
64
+ parentPath: def.parentPath,
65
+ sort: def.sort
66
+ })
67
+ );
87
68
 
88
- if (updList.length > 0) {
89
- await ctx.mysql.updBatch(MENU_TABLE_NAME, updList);
69
+ if (syncLists.updList.length > 0) {
70
+ await ctx.mysql.updBatch(MENU_TABLE_NAME, syncLists.updList);
90
71
  }
91
72
 
92
- if (insList.length > 0) {
93
- await ctx.mysql.insBatch(MENU_TABLE_NAME, insList);
73
+ if (syncLists.insList.length > 0) {
74
+ await ctx.mysql.insBatch(MENU_TABLE_NAME, syncLists.insList);
94
75
  }
95
76
 
96
77
  // 3) 删除差集(DB - 配置)
97
- const delIds = Array.from(delIdSet);
98
- if (delIds.length > 0) {
99
- await ctx.mysql.delForceBatch(MENU_TABLE_NAME, delIds);
78
+ if (syncLists.delIds.length > 0) {
79
+ await ctx.mysql.delForceBatch(MENU_TABLE_NAME, syncLists.delIds);
100
80
  }
101
81
  }
@@ -0,0 +1,35 @@
1
+ export function buildPathSyncLists(existingList, defList, buildUpdateData, buildInsertData) {
2
+ const existingMap = new Map();
3
+ const delIdSet = new Set();
4
+
5
+ for (const record of existingList) {
6
+ if (!existingMap.has(record.path)) {
7
+ existingMap.set(record.path, record);
8
+ delIdSet.add(record.id);
9
+ }
10
+ }
11
+
12
+ const updList = [];
13
+ const insList = [];
14
+
15
+ for (const def of defList) {
16
+ const existing = existingMap.get(def.path);
17
+
18
+ if (existing) {
19
+ delIdSet.delete(existing.id);
20
+ updList.push({
21
+ id: existing.id,
22
+ data: buildUpdateData(def, existing)
23
+ });
24
+ continue;
25
+ }
26
+
27
+ insList.push(buildInsertData(def));
28
+ }
29
+
30
+ return {
31
+ updList: updList,
32
+ insList: insList,
33
+ delIds: Array.from(delIdSet)
34
+ };
35
+ }
package/utils/util.js CHANGED
@@ -170,6 +170,19 @@ export function camelCase(input) {
170
170
  return [first].concat(rest).join("");
171
171
  }
172
172
 
173
+ /**
174
+ * 字段名专用小驼峰转换。
175
+ * - 只要原始值包含下划线,就直接保留原字段名。
176
+ * - 其余场景继续走普通小驼峰转换。
177
+ */
178
+ export function camelCaseKeepUnderscore(input) {
179
+ if (input.includes("_")) {
180
+ return input;
181
+ }
182
+
183
+ return camelCase(input);
184
+ }
185
+
173
186
  /**
174
187
  * 把字符串转为 snake_case。
175
188
  * - 主要用于表名/字段名(例如 userId -> user_id)。