From 49f868e45d61b7233c8e023ef01ba3f9d856c3a6 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Tue, 30 May 2023 16:31:52 +0530 Subject: [PATCH] feat: throw exception if user passed a non-existing rowId Signed-off-by: Pranav C --- packages/nocodb/src/db/BaseModelSqlv2.ts | 32 ++++++++++++++++--- .../nocodb/src/services/data-table.service.ts | 4 +-- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/packages/nocodb/src/db/BaseModelSqlv2.ts b/packages/nocodb/src/db/BaseModelSqlv2.ts index 973e1c9eb0..c26d67a876 100644 --- a/packages/nocodb/src/db/BaseModelSqlv2.ts +++ b/packages/nocodb/src/db/BaseModelSqlv2.ts @@ -14,7 +14,7 @@ import Validator from 'validator'; import { customAlphabet } from 'nanoid'; import DOMPurify from 'isomorphic-dompurify'; import { v4 as uuidv4 } from 'uuid'; -import { extractLimitAndOffset } from '../helpers' +import { extractLimitAndOffset } from '../helpers'; import { NcError } from '../helpers/catchError'; import getAst from '../helpers/getAst'; @@ -2173,7 +2173,11 @@ class BaseModelSqlv2 { async bulkUpdate( datas: any[], - { cookie, raw = false }: { cookie?: any; raw?: boolean } = {}, + { + cookie, + raw = false, + throwExceptionIfNotExist = false, + }: { cookie?: any; raw?: boolean; throwExceptionIfNotExist?: boolean } = {}, ) { let transaction; try { @@ -2212,7 +2216,12 @@ class BaseModelSqlv2 { if (!raw) { for (const pkValues of updatePkValues) { - newData.push(await this.readByPk(pkValues)); + const oldRecord = await this.readByPk(pkValues); + if (!oldRecord && throwExceptionIfNotExist) + NcError.unprocessableEntity( + `Record with pk ${JSON.stringify(pkValues)} not found`, + ); + newData.push(oldRecord); } } @@ -2275,7 +2284,13 @@ class BaseModelSqlv2 { } } - async bulkDelete(ids: any[], { cookie }: { cookie?: any } = {}) { + async bulkDelete( + ids: any[], + { + cookie, + throwExceptionIfNotExist = false, + }: { cookie?: any; throwExceptionIfNotExist?: boolean } = {}, + ) { let transaction; try { const deleteIds = await Promise.all( @@ -2290,7 +2305,14 @@ class BaseModelSqlv2 { // pk not specified - bypass continue; } - deleted.push(await this.readByPk(pkValues)); + + const oldRecord = await this.readByPk(pkValues); + if (!oldRecord && throwExceptionIfNotExist) + NcError.unprocessableEntity( + `Record with pk ${JSON.stringify(pkValues)} not found`, + ); + deleted.push(oldRecord); + res.push(d); } diff --git a/packages/nocodb/src/services/data-table.service.ts b/packages/nocodb/src/services/data-table.service.ts index 9251af6004..ad68fef149 100644 --- a/packages/nocodb/src/services/data-table.service.ts +++ b/packages/nocodb/src/services/data-table.service.ts @@ -94,7 +94,7 @@ export class DataTableService { const res = await baseModel.bulkUpdate( Array.isArray(param.body) ? param.body : [param.body], - { cookie: param.cookie }, + { cookie: param.cookie, throwExceptionIfNotExist: true }, ); return this.extractIdObj({ body: param.body, model }); @@ -118,7 +118,7 @@ export class DataTableService { await baseModel.bulkUpdate( Array.isArray(param.body) ? param.body : [param.body], - { cookie: param.cookie }, + { cookie: param.cookie, throwExceptionIfNotExist: true }, ); return this.extractIdObj({ body: param.body, model });