Browse Source

fix: for single record operation instead of bulk hook use normal hook

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/6470/head
Pranav C 1 year ago
parent
commit
c50b558756
  1. 53
      packages/nocodb/src/db/BaseModelSqlv2.ts
  2. 18
      packages/nocodb/src/services/data-table.service.ts
  3. 1366
      packages/nocodb/tests/unit/rest/tests/newDataApis.test.ts

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

@ -2478,6 +2478,7 @@ class BaseModelSqlv2 {
skip_hooks = false,
raw = false,
insertOneByOneAsFallback = false,
isSingleRecordInsertion = false,
}: {
chunkSize?: number;
cookie?: any;
@ -2485,6 +2486,7 @@ class BaseModelSqlv2 {
skip_hooks?: boolean;
raw?: boolean;
insertOneByOneAsFallback?: boolean;
isSingleRecordInsertion?: boolean;
} = {},
) {
let trx;
@ -2671,8 +2673,14 @@ class BaseModelSqlv2 {
await trx.commit();
if (!raw && !skip_hooks)
await this.afterBulkInsert(insertDatas, this.dbDriver, cookie);
if (!raw && !skip_hooks) {
if (isSingleRecordInsertion) {
const insertData = await this.readByPk(response[0]);
await this.afterInsert(insertData, this.dbDriver, cookie);
} else {
await this.afterBulkInsert(insertDatas, this.dbDriver, cookie);
}
}
return response;
} catch (e) {
@ -2688,7 +2696,13 @@ class BaseModelSqlv2 {
cookie,
raw = false,
throwExceptionIfNotExist = false,
}: { cookie?: any; raw?: boolean; throwExceptionIfNotExist?: boolean } = {},
isSingleRecordUpdation = false,
}: {
cookie?: any;
raw?: boolean;
throwExceptionIfNotExist?: boolean;
isSingleRecordUpdation?: boolean;
} = {},
) {
let transaction;
try {
@ -2740,8 +2754,19 @@ class BaseModelSqlv2 {
}
}
if (!raw)
await this.afterBulkUpdate(prevData, newData, this.dbDriver, cookie);
if (!raw) {
if (isSingleRecordUpdation) {
await this.afterUpdate(
prevData[0],
newData[0],
trx,
cookie,
datas[0],
);
} else {
await this.afterBulkUpdate(prevData, newData, this.dbDriver, cookie);
}
}
return res;
} catch (e) {
@ -2816,7 +2841,12 @@ class BaseModelSqlv2 {
{
cookie,
throwExceptionIfNotExist = false,
}: { cookie?: any; throwExceptionIfNotExist?: boolean } = {},
isSingleRecordDeletion = false,
}: {
cookie?: any;
throwExceptionIfNotExist?: boolean;
isSingleRecordDeletion?: boolean;
} = {},
) {
let transaction;
try {
@ -2918,7 +2948,11 @@ class BaseModelSqlv2 {
await transaction.commit();
await this.afterBulkDelete(deleted, this.dbDriver, cookie);
if (isSingleRecordDeletion) {
await this.afterDelete(deleted[0], trx, cookie);
} else {
await this.afterBulkDelete(deleted, this.dbDriver, cookie);
}
return res;
} catch (e) {
@ -4618,6 +4652,11 @@ function applyPaginate(
}
export function _wherePk(primaryKeys: Column[], id: unknown | unknown[]) {
// if id object is provided use as it is
if (id && typeof id === 'object') {
return id;
}
const ids = Array.isArray(id) ? id : (id + '').split('___');
const where = {};
for (let i = 0; i < primaryKeys.length; ++i) {

18
packages/nocodb/src/services/data-table.service.ts

@ -73,7 +73,11 @@ export class DataTableService {
// if array then do bulk insert
const result = await baseModel.bulkInsert(
Array.isArray(param.body) ? param.body : [param.body],
{ cookie: param.cookie, insertOneByOneAsFallback: true },
{
cookie: param.cookie,
insertOneByOneAsFallback: true,
isSingleRecordInsertion: !Array.isArray(param.body),
},
);
return Array.isArray(param.body) ? result : result[0];
@ -101,7 +105,11 @@ export class DataTableService {
await baseModel.bulkUpdate(
Array.isArray(param.body) ? param.body : [param.body],
{ cookie: param.cookie, throwExceptionIfNotExist: true },
{
cookie: param.cookie,
throwExceptionIfNotExist: true,
isSingleRecordUpdation: !Array.isArray(param.body),
},
);
return this.extractIdObj({ body: param.body, model });
@ -128,7 +136,11 @@ export class DataTableService {
await baseModel.bulkDelete(
Array.isArray(param.body) ? param.body : [param.body],
{ cookie: param.cookie, throwExceptionIfNotExist: true },
{
cookie: param.cookie,
throwExceptionIfNotExist: true,
isSingleRecordDeletion: !Array.isArray(param.body),
},
);
return this.extractIdObj({ body: param.body, model });

1366
packages/nocodb/tests/unit/rest/tests/newDataApis.test.ts

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save