From a9d9012876979b9c1406276fe93c1c088b948910 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Tue, 30 May 2023 11:30:47 +0530 Subject: [PATCH] refactor: update/delete - return with array of object containing id Signed-off-by: Pranav C --- .../nocodb/src/services/data-table.service.ts | 54 +++++-------------- 1 file changed, 14 insertions(+), 40 deletions(-) diff --git a/packages/nocodb/src/services/data-table.service.ts b/packages/nocodb/src/services/data-table.service.ts index 72dab7cc9f..d3ca4aada6 100644 --- a/packages/nocodb/src/services/data-table.service.ts +++ b/packages/nocodb/src/services/data-table.service.ts @@ -92,19 +92,12 @@ export class DataTableService { dbDriver: await NcConnectionMgrv2.get(base), }); - // return await baseModel.updateByPk( - // param.rowId, - // param.body, - // null, - // param.cookie, - // ); - const res = await baseModel.bulkUpdate( Array.isArray(param.body) ? param.body : [param.body], { cookie: param.cookie }, ); - return Array.isArray(param.body) ? res : res[0]; + return this.extractIdObj(param.body) } async dataDelete(param: { @@ -122,20 +115,13 @@ export class DataTableService { viewId: view?.id, dbDriver: await NcConnectionMgrv2.get(base), }); - // - // // todo: Should have error http status code - // const message = await baseModel.hasLTARData(param.rowId, model); - // if (message.length) { - // return { message }; - // } - // return await baseModel.delByPk(param.rowId, null, param.cookie); - const res = await baseModel.bulkUpdate( + await baseModel.bulkUpdate( Array.isArray(param.body) ? param.body : [param.body], { cookie: param.cookie }, ); - return Array.isArray(param.body) ? res : res[0]; + return this.extractIdObj(param.body); } async dataCount(param: { @@ -191,28 +177,16 @@ export class DataTableService { return { model, view }; } - private async extractPks({ model, rows }: { rows: any[]; model?: Model }) { - return await Promise.all( - rows.map(async (row) => { - // if not object then return the value - if (typeof row !== 'object') return row; - - let pk; - - // if model is passed then use the model to get the pk columns and extract the pk values - if (model) { - pk = await model.getColumns().then((cols) => - cols - .filter((col) => col.pk) - .map((col) => row[col.title]) - .join('___'), - ); - } else { - // if model is not passed then get all the values and join them - pk = Object.values(row).join('___'); - } - return pk; - }), - ); + private async extractIdObj({ model, body }: { body: Record | Record[]; model: Model }) { + const pkColumns = await model.getColumns().then((cols) => cols.filter((col) => col.pk)); + + const result = (Array.isArray(body) ? body : [body]).map((row) => { + return pkColumns.reduce((acc, col) => { + acc[col.title] = row[col.title]; + return acc; + }) + }) + + return Array.isArray(body) ? result : result[0]; } }