From 9ce245b947ed463eba0177626759d943cc3e4cd7 Mon Sep 17 00:00:00 2001 From: mertmit Date: Mon, 2 Oct 2023 11:05:05 +0000 Subject: [PATCH 1/2] fix: new data apis --- packages/nocodb/src/db/BaseModelSqlv2.ts | 23 ++++++++++--------- .../nocodb/src/services/data-table.service.ts | 14 ++++++++--- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/packages/nocodb/src/db/BaseModelSqlv2.ts b/packages/nocodb/src/db/BaseModelSqlv2.ts index 194355ecfc..c5e5908b6c 100644 --- a/packages/nocodb/src/db/BaseModelSqlv2.ts +++ b/packages/nocodb/src/db/BaseModelSqlv2.ts @@ -2882,17 +2882,22 @@ class BaseModelSqlv2 { const newData = []; const updatePkValues = []; const toBeUpdated = []; - const res = []; for (const d of updateDatas) { if (!raw) await this.validate(d); const pkValues = await this._extractPksValues(d); if (!pkValues) { - // pk not specified - bypass - continue; + if (!pkValues) { + if (throwExceptionIfNotExist) + NcError.unprocessableEntity( + `Record with pk ${JSON.stringify(pkValues)} not found`, + ); + else { + continue; + } + } } if (!raw) prevData.push(await this.readByPk(pkValues)); const wherePk = await this._wherePk(pkValues); - res.push(wherePk); toBeUpdated.push({ d, wherePk }); updatePkValues.push(pkValues); } @@ -2907,12 +2912,8 @@ class BaseModelSqlv2 { if (!raw) { for (const pkValues of updatePkValues) { - const oldRecord = await this.readByPk(pkValues); - if (!oldRecord && throwExceptionIfNotExist) - NcError.unprocessableEntity( - `Record with pk ${JSON.stringify(pkValues)} not found`, - ); - newData.push(oldRecord); + const updatedRecord = await this.readByPk(pkValues); + newData.push(updatedRecord); } } @@ -2930,7 +2931,7 @@ class BaseModelSqlv2 { } } - return res; + return newData; } catch (e) { if (transaction) await transaction.rollback(); throw e; diff --git a/packages/nocodb/src/services/data-table.service.ts b/packages/nocodb/src/services/data-table.service.ts index d7c1592982..9316fffc0f 100644 --- a/packages/nocodb/src/services/data-table.service.ts +++ b/packages/nocodb/src/services/data-table.service.ts @@ -212,7 +212,7 @@ export class DataTableService { const result = (Array.isArray(body) ? body : [body]).map((row) => { return pkColumns.reduce((acc, col) => { - acc[col.title] = row[col.title]; + acc[col.title] = row[col.title] ?? row[col.column_name]; return acc; }, {}); }); @@ -238,13 +238,21 @@ export class DataTableService { for (const row of rows) { let pk; // if only one primary key then extract the value - if (model.primaryKeys.length === 1) pk = row[model.primaryKey.title]; + if (model.primaryKeys.length === 1) + pk = row[model.primaryKey.title] ?? row[model.primaryKey.column_name]; // if composite primary key then join the values with ___ - else pk = model.primaryKeys.map((pk) => row[pk.title]).join('___'); + else + pk = model.primaryKeys + .map((pk) => row[pk.title] ?? row[pk.column_name]) + .join('___'); // if duplicate then throw error if (keys.has(pk)) { NcError.unprocessableEntity('Duplicate row with id ' + pk); } + + if (pk === undefined || pk === null) { + NcError.unprocessableEntity('Primary key is required'); + } keys.add(pk); } } From 307f143d9a247f0c64ba44515d79ee99977a7160 Mon Sep 17 00:00:00 2001 From: mertmit Date: Mon, 2 Oct 2023 11:05:05 +0000 Subject: [PATCH 2/2] 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); }