From 184bd512fe20fe0dfad29d462adcc0acb6068efb Mon Sep 17 00:00:00 2001 From: Pranav C Date: Fri, 20 Oct 2023 07:43:24 +0000 Subject: [PATCH] refactor: add ids object array and validate and generate where condition --- .../src/controllers/data-table.controller.ts | 4 ++-- packages/nocodb/src/db/BaseModelSqlv2.ts | 18 ++++++++++++++++-- .../nocodb/src/services/data-table.service.ts | 4 ++-- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/packages/nocodb/src/controllers/data-table.controller.ts b/packages/nocodb/src/controllers/data-table.controller.ts index 8f16ff789f..507e14d6e8 100644 --- a/packages/nocodb/src/controllers/data-table.controller.ts +++ b/packages/nocodb/src/controllers/data-table.controller.ts @@ -151,7 +151,7 @@ export class DataTableController { @Query('viewId') viewId: string, @Param('columnId') columnId: string, @Param('rowId') rowId: string, - @Body() refRowIds: string | string[] | number | number[], + @Body() refRowIds: string | string[] | number | number[] | Record, ) { return await this.dataTableService.nestedLink({ modelId, @@ -172,7 +172,7 @@ export class DataTableController { @Query('viewId') viewId: string, @Param('columnId') columnId: string, @Param('rowId') rowId: string, - @Body() refRowIds: string | string[] | number | number[], + @Body() refRowIds: string | string[] | number | number[] | Record, ) { return await this.dataTableService.nestedUnlink({ modelId, diff --git a/packages/nocodb/src/db/BaseModelSqlv2.ts b/packages/nocodb/src/db/BaseModelSqlv2.ts index e89caed1dd..6030145b77 100644 --- a/packages/nocodb/src/db/BaseModelSqlv2.ts +++ b/packages/nocodb/src/db/BaseModelSqlv2.ts @@ -4332,7 +4332,7 @@ class BaseModelSqlv2 { rowId, }: { cookie: any; - childIds: (string | number)[]; + childIds: (string | number | Record)[]; colId: string; rowId: string; }) { @@ -4973,13 +4973,27 @@ function applyPaginate( } export function _wherePk(primaryKeys: Column[], id: unknown | unknown[]) { + const where = {}; + // if id object is provided use as it is if (id && typeof id === 'object') { + // verify all pk columns are present in id object + for (const pk of primaryKeys) { + if (pk.title in id) { + where[pk.column_name] = id[pk.title]; + } else if (pk.column_name in id) { + where[pk.column_name] = id[pk.column_name]; + } else { + NcError.badRequest( + `Primary key column ${pk.title} not found in id object`, + ); + } + } + return id; } const ids = Array.isArray(id) ? id : (id + '').split('___'); - const where = {}; for (let i = 0; i < primaryKeys.length; ++i) { if (primaryKeys[i].dt === 'bytea') { // if column is bytea, then we need to encode the id to hex based on format diff --git a/packages/nocodb/src/services/data-table.service.ts b/packages/nocodb/src/services/data-table.service.ts index 118b5072f2..acdbba46b5 100644 --- a/packages/nocodb/src/services/data-table.service.ts +++ b/packages/nocodb/src/services/data-table.service.ts @@ -368,7 +368,7 @@ export class DataTableService { modelId: string; columnId: string; query: any; - refRowIds: string | string[] | number | number[]; + refRowIds: string | string[] | number | number[]| Record; rowId: string; }) { this.validateIds(param.refRowIds); @@ -403,7 +403,7 @@ export class DataTableService { modelId: string; columnId: string; query: any; - refRowIds: string | string[] | number | number[]; + refRowIds: string | string[] | number | number[]| Record; rowId: string; }) { this.validateIds(param.refRowIds);