Browse Source

feat: improved logic for bulkDelete

pull/8315/head
mertmit 2 months ago
parent
commit
b2fc4fb036
  1. 46
      packages/nocodb/src/db/BaseModelSqlv2.ts

46
packages/nocodb/src/db/BaseModelSqlv2.ts

@ -3904,7 +3904,9 @@ class BaseModelSqlv2 {
const deleted = [];
const res = [];
for (const d of deleteIds) {
const pkAndData: { pk: any; data: any }[] = [];
const readChunkSize = 100;
for (const [i, d] of deleteIds.entries()) {
const pkValues = this._extractPksValues(d);
if (!pkValues) {
// throw or skip if no pk provided
@ -3914,17 +3916,41 @@ class BaseModelSqlv2 {
continue;
}
const deletedRecord = await this.readByPk(pkValues);
if (!deletedRecord) {
// throw or skip if no record found
if (throwExceptionIfNotExist) {
NcError.recordNotFound(JSON.stringify(pkValues));
pkAndData.push({ pk: pkValues, data: d });
if (pkAndData.length >= readChunkSize || i === deleteIds.length - 1) {
const tempToRead = pkAndData.splice(0, pkAndData.length);
const oldRecords = await this.list(
{
pks: tempToRead.map((v) => v.pk).join(','),
},
{
limitOverride: tempToRead.length,
},
);
if (oldRecords.length === tempToRead.length) {
deleted.push(...oldRecords);
res.push(...tempToRead.map((v) => v.data));
} else {
for (const { pk, data } of tempToRead) {
const oldRecord = oldRecords.find(
(r) => this._extractPksValues(r) === pk,
);
if (!oldRecord) {
// throw or skip if no record found
if (throwExceptionIfNotExist) {
NcError.recordNotFound(JSON.stringify(pk));
}
continue;
}
deleted.push(oldRecord);
res.push(data);
}
}
continue;
}
deleted.push(deletedRecord);
res.push(d);
}
const execQueries: ((

Loading…
Cancel
Save