mirror of
https://github.com/nocodb/nocodb.git
synced 2026-04-30 15:26:48 +00:00
refactor: bulk delete useData fun
This commit is contained in:
@@ -165,40 +165,41 @@ export function useData(args: {
|
||||
const rowsToInsert = []
|
||||
|
||||
isPaginationLoading.value = true
|
||||
|
||||
// Todo: use isCreatedOrLastModifiedByCol insted of hardcoded values once upgrader is ready for oss
|
||||
const autoGeneratedKeys = clone(metaValue?.columns || [])
|
||||
.filter(
|
||||
(c) =>
|
||||
c.uidt !== UITypes.ID && (isCreatedOrLastModifiedTimeCol(c) || c.uidt === 'CreatedBy' || c.uidt === 'LastModifiedBy'),
|
||||
)
|
||||
.map((c) => c.title)
|
||||
|
||||
try {
|
||||
for (const currentRow of rows) {
|
||||
const { missingRequiredColumns, insertObj } = await populateInsertObject({
|
||||
meta: metaValue!,
|
||||
ltarState: {},
|
||||
getMeta,
|
||||
row: currentRow.row,
|
||||
undo,
|
||||
})
|
||||
rowsToInsert =
|
||||
(
|
||||
await Promise.all(
|
||||
rows.map(async (currentRow) => {
|
||||
const { missingRequiredColumns, insertObj } = await populateInsertObject({
|
||||
meta: metaValue!,
|
||||
ltarState: {},
|
||||
getMeta,
|
||||
row: currentRow.row,
|
||||
undo,
|
||||
})
|
||||
|
||||
// Todo: use isCreatedOrLastModifiedByCol insted of hardcoded values once upgrader is ready for oss
|
||||
const autoGeneratedKeys = clone(metaValue?.columns || [])
|
||||
.filter(
|
||||
(c) =>
|
||||
c.uidt !== UITypes.ID &&
|
||||
(isCreatedOrLastModifiedTimeCol(c) || c.uidt === 'CreatedBy' || c.uidt === 'LastModifiedBy'),
|
||||
if (missingRequiredColumns.size === 0) {
|
||||
autoGeneratedKeys.forEach((key) => delete insertObj[key])
|
||||
return insertObj
|
||||
}
|
||||
}),
|
||||
)
|
||||
.map((c) => c.title)
|
||||
|
||||
// delete auto generated keys
|
||||
for (const key of autoGeneratedKeys) {
|
||||
delete insertObj[key!]
|
||||
}
|
||||
|
||||
if (missingRequiredColumns.size) continue
|
||||
else rowsToInsert.push({ ...insertObj })
|
||||
}
|
||||
)?.filter(Boolean) ?? [] // Filter out undefined values (if any)
|
||||
|
||||
const bulkInsertedIds = await $api.dbDataTableRow.create(metaValue?.id as string, rowsToInsert, {
|
||||
viewId: viewMetaValue?.id as string,
|
||||
})
|
||||
|
||||
await callbacks?.syncCount?.()
|
||||
|
||||
return bulkInsertedIds
|
||||
} catch (error: any) {
|
||||
message.error(await extractSdkResponseErrorMsg(error))
|
||||
@@ -620,7 +621,7 @@ export function useData(args: {
|
||||
|
||||
async function deleteSelectedRows() {
|
||||
let row = formattedData.value.length
|
||||
const removedRowsData: { Id: string; row: Row; rowIndex: number }[] = []
|
||||
let removedRowsData: { Id: string; row: Row; rowIndex: number }[] = []
|
||||
|
||||
while (row--) {
|
||||
const { row: rowObj, rowMeta } = formattedData.value[row] as Record<string, any>
|
||||
@@ -628,10 +629,7 @@ export function useData(args: {
|
||||
continue
|
||||
}
|
||||
if (!rowMeta.new) {
|
||||
const id = meta?.value?.columns
|
||||
?.filter((c) => c.pk)
|
||||
.map((c) => rowObj[c.title as string])
|
||||
.join('___')
|
||||
const id = extractPkFromRow(rowObj, meta?.value?.columns as ColumnType[])
|
||||
|
||||
if (id) {
|
||||
removedRowsData.push({ Id: id, row: clone(formattedData.value[row]), rowIndex: row })
|
||||
@@ -640,21 +638,15 @@ export function useData(args: {
|
||||
}
|
||||
|
||||
try {
|
||||
const removedRowIds: { Id: string }[] = await bulkDeleteRows(
|
||||
removedRowsData.map((row) => {
|
||||
return {
|
||||
Id: row.Id,
|
||||
}
|
||||
}),
|
||||
)
|
||||
const removedRowIds: { Id: string }[] = await bulkDeleteRows(removedRowsData.map((row) => ({ Id: row.Id })))
|
||||
|
||||
if (Array.isArray(removedRowIds)) {
|
||||
const removedRowsMap: Map<string, string | number> = new Map(removedRowIds.map((row) => [row.Id as string, '']))
|
||||
const removedRowIdsSet = new Set(removedRowIds.map((row) => row.Id))
|
||||
|
||||
removedRowsData.filter((row) => removedRowsMap.has(row.Id))
|
||||
removedRowsData = removedRowsData.filter((row) => removedRowIdsSet.has(row.Id))
|
||||
|
||||
const rowIndexes = removedRowsData.map((row) => row.rowIndex)
|
||||
formattedData.value = formattedData.value.filter((_, index) => rowIndexes.includes(index))
|
||||
const rowIndexesSet = new Set(removedRowsData.map((row) => row.rowIndex))
|
||||
formattedData.value = formattedData.value.filter((_, index) => rowIndexesSet.has(index))
|
||||
}
|
||||
} catch (e: any) {
|
||||
return message.error(`${t('msg.error.deleteRowFailed')}: ${await extractSdkResponseErrorMsg(e)}`)
|
||||
@@ -663,13 +655,7 @@ export function useData(args: {
|
||||
addUndo({
|
||||
redo: {
|
||||
fn: async function redo(this: UndoRedoAction, removedRowsData: { Id: string; row: Row; rowIndex: number }[]) {
|
||||
const removedRowIds = await bulkDeleteRows(
|
||||
removedRowsData.map((row) => {
|
||||
return {
|
||||
Id: row.Id,
|
||||
}
|
||||
}),
|
||||
)
|
||||
const removedRowIds = await bulkDeleteRows(removedRowsData.map((row) => ({ Id: row.Id })))
|
||||
|
||||
if (Array.isArray(removedRowIds)) {
|
||||
for (const { row } of removedRowsData) {
|
||||
@@ -738,15 +724,14 @@ export function useData(args: {
|
||||
// plus one because we want to include the end row
|
||||
let row = start + 1
|
||||
|
||||
const removedRowsData: { Id: string; row: Row; rowIndex: number }[] = []
|
||||
let removedRowsData: { Id: string; row: Row; rowIndex: number }[] = []
|
||||
|
||||
while (row--) {
|
||||
try {
|
||||
const { row: rowObj, rowMeta } = formattedData.value[row] as Record<string, any>
|
||||
|
||||
if (!rowMeta.new) {
|
||||
const id = meta?.value?.columns
|
||||
?.filter((c) => c.pk)
|
||||
.map((c) => rowObj[c.title as string])
|
||||
.join('___')
|
||||
const id = extractPkFromRow(rowObj, meta?.value?.columns as ColumnType[])
|
||||
|
||||
if (id) {
|
||||
removedRowsData.push({ Id: id, row: clone(formattedData.value[row]), rowIndex: row })
|
||||
@@ -760,20 +745,15 @@ export function useData(args: {
|
||||
}
|
||||
|
||||
try {
|
||||
const removedRowIds: { Id: string }[] = await bulkDeleteRows(
|
||||
removedRowsData.map((row) => {
|
||||
return {
|
||||
Id: row.Id,
|
||||
}
|
||||
}),
|
||||
)
|
||||
const removedRowIds: { Id: string }[] = await bulkDeleteRows(removedRowsData.map((row) => ({ Id: row.Id })))
|
||||
|
||||
if (Array.isArray(removedRowIds)) {
|
||||
const removedRowsMap: Map<string, string | number> = new Map(removedRowIds.map((row) => [row.Id as string, '']))
|
||||
const removedRowIdsSet = new Set(removedRowIds.map((row) => row.Id))
|
||||
|
||||
removedRowsData.filter((row) => removedRowsMap.has(row.Id))
|
||||
removedRowsData = removedRowsData.filter((row) => removedRowIdsSet.has(row.Id))
|
||||
|
||||
const rowIndexes = removedRowsData.map((row) => row.rowIndex)
|
||||
formattedData.value = formattedData.value.filter((_, index) => rowIndexes.includes(index))
|
||||
const rowIndexesSet = new Set(removedRowsData.map((row) => row.rowIndex))
|
||||
formattedData.value = formattedData.value.filter((_, index) => rowIndexesSet.has(index))
|
||||
}
|
||||
} catch (e: any) {
|
||||
return message.error(`${t('msg.error.deleteRowFailed')}: ${await extractSdkResponseErrorMsg(e)}`)
|
||||
@@ -782,13 +762,7 @@ export function useData(args: {
|
||||
addUndo({
|
||||
redo: {
|
||||
fn: async function redo(this: UndoRedoAction, removedRowsData: { Id: string; row: Row; rowIndex: number }[]) {
|
||||
const removedRowIds = await bulkDeleteRows(
|
||||
removedRowsData.map((row) => {
|
||||
return {
|
||||
Id: row.Id,
|
||||
}
|
||||
}),
|
||||
)
|
||||
const removedRowIds = await bulkDeleteRows(removedRowsData.map((row) => ({ Id: row.Id })))
|
||||
|
||||
if (Array.isArray(removedRowIds)) {
|
||||
for (const { row } of removedRowsData) {
|
||||
@@ -893,5 +867,7 @@ export function useData(args: {
|
||||
bulkUpdateView,
|
||||
selectedAllRecords,
|
||||
removeRowIfNew,
|
||||
bulkDeleteRows,
|
||||
bulkInsertRows,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user