befly 3.22.9 → 3.23.1

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/api.js CHANGED
@@ -1,3 +1,5 @@
1
+ import { buildPathSyncLists } from "./syncUtil.js";
2
+
1
3
  const API_TABLE_NAME = "beflyApi";
2
4
 
3
5
  const getApiParentPath = (apiPath) => {
@@ -22,64 +24,45 @@ export async function syncApi(ctx, apis) {
22
24
  where: { state$gte: 0 }
23
25
  });
24
26
 
25
- // 给接口根据接口路径进行映射
26
- const dbLists = allDbApis.data.lists || [];
27
- const existingApiMap = new Map();
28
- for (const item of dbLists) {
29
- if (!existingApiMap.has(item.path)) {
30
- existingApiMap.set(item.path, item);
31
- }
32
- }
33
-
34
- const insList = [];
35
- const updList = [];
36
- const scannedPathSet = new Set();
27
+ const apiDefs = [];
37
28
  for (const api of apis) {
38
- scannedPathSet.add(api.apiPath);
39
-
40
- const auth = api.auth === false ? "否" : Array.isArray(api.auth) ? api.auth.join(",") : "是";
41
- const parentPath = getApiParentPath(api.apiPath);
42
- const existing = existingApiMap.get(api.apiPath);
43
-
44
- if (existing) {
45
- updList.push({
46
- id: existing.id,
47
- data: {
48
- name: api.name,
49
- path: api.apiPath,
50
- method: api.method,
51
- parentPath: parentPath,
52
- auth: auth
53
- }
54
- });
55
- continue;
56
- }
57
-
58
- insList.push({
29
+ apiDefs.push({
59
30
  name: api.name,
60
31
  path: api.apiPath,
61
32
  method: api.method,
62
- parentPath: parentPath,
63
- auth: auth
33
+ parentPath: getApiParentPath(api.apiPath),
34
+ auth: api.auth === false ? "否" : Array.isArray(api.auth) ? api.auth.join(",") : "是"
64
35
  });
65
36
  }
66
37
 
67
- const delIdList = [];
68
- for (const item of dbLists) {
69
- if (!scannedPathSet.has(item.path)) {
70
- delIdList.push(item.id);
71
- }
72
- }
38
+ const syncLists = buildPathSyncLists(
39
+ allDbApis.data.lists || [],
40
+ apiDefs,
41
+ (def) => ({
42
+ name: def.name,
43
+ path: def.path,
44
+ method: def.method,
45
+ parentPath: def.parentPath,
46
+ auth: def.auth
47
+ }),
48
+ (def) => ({
49
+ name: def.name,
50
+ path: def.path,
51
+ method: def.method,
52
+ parentPath: def.parentPath,
53
+ auth: def.auth
54
+ })
55
+ );
73
56
 
74
- if (updList.length > 0) {
75
- await ctx.mysql.updBatch(API_TABLE_NAME, updList);
57
+ if (syncLists.updList.length > 0) {
58
+ await ctx.mysql.updBatch(API_TABLE_NAME, syncLists.updList);
76
59
  }
77
60
 
78
- if (insList.length > 0) {
79
- await ctx.mysql.insBatch(API_TABLE_NAME, insList);
61
+ if (syncLists.insList.length > 0) {
62
+ await ctx.mysql.insBatch(API_TABLE_NAME, syncLists.insList);
80
63
  }
81
64
 
82
- if (delIdList.length > 0) {
83
- await ctx.mysql.delForceBatch(API_TABLE_NAME, delIdList);
65
+ if (syncLists.delIds.length > 0) {
66
+ await ctx.mysql.delForceBatch(API_TABLE_NAME, syncLists.delIds);
84
67
  }
85
68
  }
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)。