Browse Source

fix: new data apis throwExceptionIfNotExist logic

pull/6537/head
mertmit 1 year ago
parent
commit
307f143d9a
  1. 44
      packages/nocodb/src/db/BaseModelSqlv2.ts

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

@ -2886,17 +2886,27 @@ class BaseModelSqlv2 {
if (!raw) await this.validate(d); if (!raw) await this.validate(d);
const pkValues = await this._extractPksValues(d); const pkValues = await this._extractPksValues(d);
if (!pkValues) { if (!pkValues) {
if (!pkValues) { // throw or skip if no pk provided
if (throwExceptionIfNotExist) if (throwExceptionIfNotExist) {
NcError.unprocessableEntity(
`Record with pk ${JSON.stringify(pkValues)} not found`,
);
}
continue;
}
if (!raw) {
const oldRecord = await this.readByPk(pkValues);
if (!oldRecord) {
// throw or skip if no record found
if (throwExceptionIfNotExist) {
NcError.unprocessableEntity( NcError.unprocessableEntity(
`Record with pk ${JSON.stringify(pkValues)} not found`, `Record with pk ${JSON.stringify(pkValues)} not found`,
); );
else {
continue;
} }
continue;
} }
prevData.push(oldRecord);
} }
if (!raw) prevData.push(await this.readByPk(pkValues));
const wherePk = await this._wherePk(pkValues); const wherePk = await this._wherePk(pkValues);
toBeUpdated.push({ d, wherePk }); toBeUpdated.push({ d, wherePk });
updatePkValues.push(pkValues); updatePkValues.push(pkValues);
@ -3024,16 +3034,26 @@ class BaseModelSqlv2 {
for (const d of deleteIds) { for (const d of deleteIds) {
const pkValues = await this._extractPksValues(d); const pkValues = await this._extractPksValues(d);
if (!pkValues) { if (!pkValues) {
// pk not specified - bypass // throw or skip if no pk provided
if (throwExceptionIfNotExist) {
NcError.unprocessableEntity(
`Record with pk ${JSON.stringify(pkValues)} not found`,
);
}
continue; continue;
} }
const oldRecord = await this.readByPk(pkValues); const deletedRecord = await this.readByPk(pkValues);
if (!oldRecord && throwExceptionIfNotExist) if (!deletedRecord) {
NcError.unprocessableEntity( // throw or skip if no record found
`Record with pk ${JSON.stringify(pkValues)} not found`, if (throwExceptionIfNotExist) {
); NcError.unprocessableEntity(
deleted.push(oldRecord); `Record with pk ${JSON.stringify(pkValues)} not found`,
);
}
continue;
}
deleted.push(deletedRecord);
res.push(d); res.push(d);
} }

Loading…
Cancel
Save