20260905172534
This commit is contained in:
1 parent
1b01a31252
commit
7b00eeb88c
711 files changed
+2849
-959
No files matched your search
@@ -33,19 +33,35 @@ export function stripInternalKeys(value) {
|
||||
* 随后的 remapForeignKeys 才能据此修正跨表外键。派生字段的剥离统一延后到
|
||||
* 提交前由 sanitizeSaveReqs 处理,切勿在此处返回剥离副本。
|
||||
*/
|
||||
export function diffRows(current, previous) {
|
||||
const previousById = new Map(previous.map((item) => [String(item.b_id), item]))
|
||||
const currentById = new Map(current.map((item) => [String(item.b_id), item]))
|
||||
/**
|
||||
* 行集合差异比对(子表;单条记录字段级比对请用 diffRow)。
|
||||
* 比较时忽略派生字段(_* / v_*),避免字段面板的参与勾选等虚拟列造成「打开即脏」。
|
||||
*
|
||||
* 字符串业务键方案下行身份 = 主键列值组合(keyFields):
|
||||
* - 单列业务键,如 s_module / s_menu / s_i18n 传 ['b_id'];
|
||||
* - 复合业务键,如 s_field / s_field_view / s_field_edit 传 ['b_module_id','b_field']、
|
||||
* s_field_query 传 ['b_module_id','b_field','b_condition_no']。
|
||||
* 行内不再有雪花 id 可供回灌,delete 只携带主键列,insert 由主键列业务值本身定位。
|
||||
*
|
||||
* 注意:inserts / updates 返回的是 current 中的原对象引用(不拷贝),
|
||||
* 派生字段的剥离统一延后到提交前由 sanitizeSaveReqs 处理。
|
||||
*/
|
||||
export function diffRows(current, previous, options = {}) {
|
||||
const keyFields = options.keyFields || ['b_id']
|
||||
const keyOf = (item) =>
|
||||
keyFields.map((field) => String(item?.[field] ?? '')).join('\u0001')
|
||||
const previousByKey = new Map(previous.map((item) => [keyOf(item), item]))
|
||||
const currentByKey = new Map(current.map((item) => [keyOf(item), item]))
|
||||
return {
|
||||
inserts: current.filter((item) => !previousById.has(String(item.b_id))),
|
||||
inserts: current.filter((item) => !previousByKey.has(keyOf(item))),
|
||||
updates: current.filter((item) => {
|
||||
const old = previousById.get(String(item.b_id))
|
||||
const old = previousByKey.get(keyOf(item))
|
||||
if (!old) return false
|
||||
return JSON.stringify(stripInternalKeys(item)) !== JSON.stringify(stripInternalKeys(old))
|
||||
}),
|
||||
deletes: previous
|
||||
.filter((item) => !currentById.has(String(item.b_id)))
|
||||
.map((item) => ({ b_id: item.b_id })),
|
||||
.filter((item) => !currentByKey.has(keyOf(item)))
|
||||
.map((item) => Object.fromEntries(keyFields.map((field) => [field, item[field]]))),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,8 +108,9 @@ export function remapForeignKeys(rows, fields, mapping) {
|
||||
})
|
||||
}
|
||||
|
||||
export function tableChange(table, changes) {
|
||||
return { table, key_field: 'b_id', ...changes }
|
||||
export function tableChange(table, changes, keyFields) {
|
||||
const keys = keyFields && keyFields.length ? keyFields : ['b_id']
|
||||
return { table, key_field: keys.join(','), ...changes }
|
||||
}
|
||||
|
||||
export function hasChanges(request) {
|
||||
|
||||
Reference in new issue
Block a user