Browse Source

feat: improved logic for bulkUpdate

pull/8315/head
mertmit 8 months ago
parent
commit
bd695fde58
  1. 56
      packages/nocodb/src/db/BaseModelSqlv2.ts

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

@ -319,6 +319,7 @@ class BaseModelSqlv2 {
sort?: string | string[]; sort?: string | string[];
fieldsSet?: Set<string>; fieldsSet?: Set<string>;
limitOverride?: number; limitOverride?: number;
pks?: string;
} = {}, } = {},
options: { options: {
ignoreViewFilterAndSort?: boolean; ignoreViewFilterAndSort?: boolean;
@ -3708,8 +3709,10 @@ class BaseModelSqlv2 {
const newData = []; const newData = [];
const updatePkValues = []; const updatePkValues = [];
const toBeUpdated = []; const toBeUpdated = [];
const toRead = [];
const readChunkSize = 100;
for (const d of updateDatas) { for (const d of updateDatas) {
const pkValues = await 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
if (throwExceptionIfNotExist) { if (throwExceptionIfNotExist) {
@ -3720,15 +3723,39 @@ class BaseModelSqlv2 {
if (!raw) { if (!raw) {
await this.prepareNocoData(d, false, cookie); await this.prepareNocoData(d, false, cookie);
const oldRecord = await this.readByPk(pkValues); toRead.push(pkValues);
if (!oldRecord) {
// throw or skip if no record found if (toRead.length >= readChunkSize) {
if (throwExceptionIfNotExist) { const tempToRead = toRead.splice(0, toRead.length);
NcError.recordNotFound(JSON.stringify(pkValues)); const oldRecords = await this.list(
{
pks: tempToRead.join(','),
},
{
limitOverride: tempToRead.length,
},
);
if (oldRecords.length === tempToRead.length) {
prevData.push(...oldRecords);
} else {
for (const record of oldRecords) {
const pkValues = this._extractPksValues(record);
const exists = tempToRead.find((d) => d === pkValues);
if (!exists) {
// throw or skip if no record found
if (throwExceptionIfNotExist) {
NcError.recordNotFound(JSON.stringify(pkValues));
}
continue;
}
prevData.push(record);
}
} }
continue;
} }
prevData.push(oldRecord);
} }
const wherePk = await this._wherePk(pkValues); const wherePk = await this._wherePk(pkValues);
toBeUpdated.push({ d, wherePk }); toBeUpdated.push({ d, wherePk });
@ -3744,9 +3771,12 @@ class BaseModelSqlv2 {
await transaction.commit(); await transaction.commit();
if (!raw) { if (!raw) {
for (const pkValues of updatePkValues) { while (updatePkValues.length) {
const updatedRecord = await this.readByPk(pkValues); const updatedRecords = await this.list({
newData.push(updatedRecord); pks: updatePkValues.splice(0, readChunkSize).join(','),
});
newData.push(...updatedRecords);
} }
} }
@ -3792,7 +3822,7 @@ class BaseModelSqlv2 {
await this.prepareNocoData(updateData, false, cookie); await this.prepareNocoData(updateData, false, cookie);
const pkValues = await this._extractPksValues(updateData); const pkValues = this._extractPksValues(updateData);
if (pkValues) { if (pkValues) {
// pk is specified - by pass // pk is specified - by pass
} else { } else {
@ -3875,7 +3905,7 @@ class BaseModelSqlv2 {
const deleted = []; const deleted = [];
const res = []; const res = [];
for (const d of deleteIds) { for (const d of deleteIds) {
const pkValues = await 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
if (throwExceptionIfNotExist) { if (throwExceptionIfNotExist) {

Loading…
Cancel
Save