Browse Source

feat: improved logic for bulkDelete

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

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

@ -3904,7 +3904,9 @@ class BaseModelSqlv2 {
const deleted = []; const deleted = [];
const res = []; 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); const pkValues = this._extractPksValues(d);
if (!pkValues) { if (!pkValues) {
// throw or skip if no pk provided // throw or skip if no pk provided
@ -3914,17 +3916,41 @@ class BaseModelSqlv2 {
continue; continue;
} }
const deletedRecord = await this.readByPk(pkValues); pkAndData.push({ pk: pkValues, data: d });
if (!deletedRecord) {
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 // throw or skip if no record found
if (throwExceptionIfNotExist) { if (throwExceptionIfNotExist) {
NcError.recordNotFound(JSON.stringify(pkValues)); NcError.recordNotFound(JSON.stringify(pk));
} }
continue; continue;
} }
deleted.push(deletedRecord);
res.push(d); deleted.push(oldRecord);
res.push(data);
}
}
}
} }
const execQueries: (( const execQueries: ((

Loading…
Cancel
Save