From 307f143d9a247f0c64ba44515d79ee99977a7160 Mon Sep 17 00:00:00 2001 From: mertmit Date: Mon, 2 Oct 2023 11:05:05 +0000 Subject: [PATCH] fix: new data apis throwExceptionIfNotExist logic --- packages/nocodb/src/db/BaseModelSqlv2.ts | 44 +++++++++++++++++------- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/packages/nocodb/src/db/BaseModelSqlv2.ts b/packages/nocodb/src/db/BaseModelSqlv2.ts index c5e5908b6c..31ae0399fb 100644 --- a/packages/nocodb/src/db/BaseModelSqlv2.ts +++ b/packages/nocodb/src/db/BaseModelSqlv2.ts @@ -2886,17 +2886,27 @@ class BaseModelSqlv2 { if (!raw) await this.validate(d); const pkValues = await this._extractPksValues(d); if (!pkValues) { - if (!pkValues) { - if (throwExceptionIfNotExist) + // throw or skip if no pk provided + 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( `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); toBeUpdated.push({ d, wherePk }); updatePkValues.push(pkValues); @@ -3024,16 +3034,26 @@ class BaseModelSqlv2 { for (const d of deleteIds) { const pkValues = await this._extractPksValues(d); 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; } - const oldRecord = await this.readByPk(pkValues); - if (!oldRecord && throwExceptionIfNotExist) - NcError.unprocessableEntity( - `Record with pk ${JSON.stringify(pkValues)} not found`, - ); - deleted.push(oldRecord); + const deletedRecord = await this.readByPk(pkValues); + if (!deletedRecord) { + // throw or skip if no record found + if (throwExceptionIfNotExist) { + NcError.unprocessableEntity( + `Record with pk ${JSON.stringify(pkValues)} not found`, + ); + } + continue; + } + deleted.push(deletedRecord); res.push(d); }