Browse Source

fix: refactor aliasMapper

pull/9764/head
DarkPhoenix2704 3 weeks ago
parent
commit
2693d5e051
  1. 63
      packages/nc-gui/composables/useViewGroupBy.ts
  2. 29
      packages/nc-gui/utils/aliasUtils.ts

63
packages/nc-gui/composables/useViewGroupBy.ts

@ -391,7 +391,7 @@ const [useProvideViewGroupBy, useViewGroupBy] = useInjectionState(
}
if (appInfo.value.ee) {
const aggregationMap = new Map<string, string>()
const aggregationAliasMapper = new AliasMapper()
const aggregation = Object.values(gridViewCols.value)
.map((f) => ({
@ -401,12 +401,9 @@ const [useProvideViewGroupBy, useViewGroupBy] = useInjectionState(
.filter((f) => f.type !== CommonAggregations.None)
const aggregationParams = (group.children ?? []).map((child) => {
const key = Math.random().toString(36).substring(7)
aggregationMap.set(key, child.key)
return {
where: calculateNestedWhere(child.nestedIn, where?.value),
alias: key,
alias: aggregationAliasMapper.generateAlias(child.key),
...(isUIAllowed('filterSync') ? {} : { filterArrJson: JSON.stringify(nestedFilters.value) }),
}
})
@ -427,24 +424,20 @@ const [useProvideViewGroupBy, useViewGroupBy] = useInjectionState(
aggregationParams,
)
Object.entries(aggResponse).forEach(([key, value]) => {
const originalKey = aggregationMap.get(key)
const child = (group?.children ?? []).find((c) => c.key.toString() === originalKey.toString())
await aggregationAliasMapper.process(aggResponse, (originalKey, value) => {
const child = (group?.children ?? []).find((c) => c.key.toString() === (originalKey as any).toString())
if (child) {
Object.assign(child.aggregations, value)
}
})
}
if (group?.children && group.nestedIn.length === groupBy.value.length - 1) {
const aliasMap = new Map<string, string>()
if (group?.children?.length && group.nestedIn.length === groupBy.value.length - 1) {
const aliasMapper = new AliasMapper()
const childViewFilters = group?.children?.map((childGroup) => {
const key = Math.random().toString(36).substring(7)
aliasMap.set(key, childGroup.key)
return {
alias: key,
alias: aliasMapper.generateAlias(childGroup.key),
where: calculateNestedWhere(childGroup.nestedIn, where?.value),
offset:
((childGroup.paginationData.page ?? 0) - 1) * (childGroup.paginationData.pageSize ?? groupByRecordLimit.value),
@ -466,9 +459,8 @@ const [useProvideViewGroupBy, useViewGroupBy] = useInjectionState(
)
: await fetchBulkListData({}, childViewFilters)
Object.entries(bulkData).forEach(([key, value]: { key: string; value: any }) => {
const originalKey = aliasMap.get(key)
const child = (group?.children ?? []).find((c) => c.key.toString() === originalKey.toString())
await aliasMapper.process(bulkData, (originalKey, value: any) => {
const child = (group?.children ?? []).find((c) => c.key.toString() === (originalKey as any).toString())
if (child) {
child.count = value.pageInfo.totalRows ?? 0
child.rows = formatData(value.list)
@ -478,18 +470,15 @@ const [useProvideViewGroupBy, useViewGroupBy] = useInjectionState(
}
}
if (group?.children && group.nestedIn.length < groupBy.value.length - 1) {
const aliasMap = new Map<string, string>()
if (group?.children?.length && group.nestedIn.length < groupBy.value.length - 1) {
const aliasMapper = new AliasMapper()
const childGroupFilters = group?.children?.map((childGroup) => {
const childGroupBy = groupBy.value[childGroup.nestedIn.length]
const childNestedWhere = calculateNestedWhere(childGroup.nestedIn, where?.value)
const key = Math.random().toString(36).substring(7)
aliasMap.set(key, childGroup.key)
return {
alias: key,
alias: aliasMapper.generateAlias(childGroup.key),
offset:
((childGroup.paginationData.page ?? 0) - 1) * (childGroup.paginationData.pageSize ?? groupByGroupLimit.value),
limit: childGroup.paginationData.pageSize ?? groupByGroupLimit.value,
@ -501,7 +490,7 @@ const [useProvideViewGroupBy, useViewGroupBy] = useInjectionState(
}
})
if (childGroupFilters.length > 0) {
if (childGroupFilters?.length > 0) {
const bulkGroupData = !isPublic
? await api.dbDataTableBulkGroupList.dbDataTableBulkGroupList(
meta.value.id,
@ -512,13 +501,12 @@ const [useProvideViewGroupBy, useViewGroupBy] = useInjectionState(
)
: await fetchBulkGroupData({}, childGroupFilters)
for (const [key, value] of Object.entries(bulkGroupData)) {
const originalKey = aliasMap.get(key)
const child = (group?.children ?? []).find((c) => c.key.toString() === originalKey.toString())!
Object.assign(child, await processGroupData(value, child))
}
await aliasMapper.process(bulkGroupData, async (originalKey, value) => {
const child = (group?.children ?? []).find((c) => c.key.toString() === originalKey.toString())
if (child) {
Object.assign(child, await processGroupData(value, child))
}
})
}
}
} catch (e) {
@ -582,17 +570,14 @@ const [useProvideViewGroupBy, useViewGroupBy] = useInjectionState(
filteredFields = filteredFields?.filter((x) => x.type !== CommonAggregations.None)
if (filteredFields && !filteredFields?.length) return
if ((filteredFields && !filteredFields?.length) || !group.children?.length) return
const aggregationMap = new Map<string, string>()
const aliasMapper = new AliasMapper()
const aggregationParams = (group.children ?? []).map((child) => {
const key = Math.random().toString(36).substring(7)
aggregationMap.set(key, child.key)
return {
where: calculateNestedWhere(child.nestedIn, where?.value),
alias: key,
alias: aliasMapper.generateAlias(child.key),
...(isUIAllowed('filterSync') ? {} : { filterArrJson: JSON.stringify(nestedFilters.value) }),
}
})
@ -613,9 +598,7 @@ const [useProvideViewGroupBy, useViewGroupBy] = useInjectionState(
aggregationParams,
)
Object.entries(response).forEach(([key, value]) => {
const originalKey = aggregationMap.get(key)
await aliasMapper.process(response, (originalKey, value) => {
const child = (group.children ?? []).find((c) => c.key.toString() === originalKey.toString())
if (child) {
Object.assign(child.aggregations, value)

29
packages/nc-gui/utils/aliasUtils.ts

@ -0,0 +1,29 @@
export class AliasMapper {
private aliasMap = new Map<string, unknown>()
generateAlias(key: unknown): string {
const alias = Math.random().toString(36).substring(2, 15)
this.aliasMap.set(alias, key)
return alias
}
getAlias(alias: string): unknown {
return this.aliasMap.get(alias)
}
clear() {
this.aliasMap.clear()
}
async process<T extends Record<string, unknown>>(
data: Record<string, T>,
processor: (key: unknown, value: T) => void | Promise<void>,
): Promise<void> {
for (const [alias, value] of Object.entries(data)) {
const originalKey = this.getAlias(alias)
if (originalKey !== undefined) {
await processor(originalKey, value)
}
}
}
}
Loading…
Cancel
Save