Browse Source

feat: throw exception if user passed a non-existing rowId

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5901/head
Pranav C 2 years ago
parent
commit
49f868e45d
  1. 32
      packages/nocodb/src/db/BaseModelSqlv2.ts
  2. 4
      packages/nocodb/src/services/data-table.service.ts

32
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);
}

4
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 });

Loading…
Cancel
Save