From bd9620c073dc07a5dbe1c2e26d0d5dcd18214c32 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Mon, 29 May 2023 14:58:15 +0530 Subject: [PATCH] feat: if array of values passed then do bulk insert Signed-off-by: Pranav C --- .../en/developer-resources/rest-apis.md | 4 +- .../src/controllers/data-table.controller.ts | 9 +- packages/nocodb/src/schema/swagger.json | 159 +++++++++++------- .../nocodb/src/services/data-table.service.ts | 45 +++-- 4 files changed, 137 insertions(+), 80 deletions(-) diff --git a/packages/noco-docs/content/en/developer-resources/rest-apis.md b/packages/noco-docs/content/en/developer-resources/rest-apis.md index d93daa069e..3652646fd8 100644 --- a/packages/noco-docs/content/en/developer-resources/rest-apis.md +++ b/packages/noco-docs/content/en/developer-resources/rest-apis.md @@ -77,8 +77,8 @@ Currently, the default value for {orgs} is noco. Users will be able to ch | Data | Get | dbTableRow | tableRowList | /api/v1/base/{baseId}/tables/{tableId} | | Data | Post | dbTableRow | tableRowCreate | /api/v1/base/{baseId}/tables/{tableId} | | Data | Get | dbTableRow | tableRowRead | /api/v1/base/tables/{tableId}/rows/{rowId} | -| Data | Patch | dbTableRow | tableRowUpdate | /api/v1/base/tables/{tableId}/rows/{rowId} | -| Data | Delete| dbTableRow | tableRowDelete | /api/v1/base/tables/{tableId}/rows/{rowId} | +| Data | Patch | dbTableRow | tableRowUpdate | /api/v1/base/tables/{tableId}/rows | +| Data | Delete| dbTableRow | tableRowDelete | /api/v1/base/tables/{tableId}/rows | | Data | Get | dbTableRow | tableRowCount | /api/v1/base/{baseId}/tables/{tableId}/count | ### Meta APIs diff --git a/packages/nocodb/src/controllers/data-table.controller.ts b/packages/nocodb/src/controllers/data-table.controller.ts index 3073423c30..15944d6982 100644 --- a/packages/nocodb/src/controllers/data-table.controller.ts +++ b/packages/nocodb/src/controllers/data-table.controller.ts @@ -85,7 +85,7 @@ export class DataTableController { }); } - @Patch(['/api/v1/base/tables/:modelId/rows/:rowId']) + @Patch(['/api/v1/base/tables/:modelId/rows']) @Acl('dataUpdate') async dataUpdate( @Request() req, @@ -98,11 +98,11 @@ export class DataTableController { body: req.body, cookie: req, viewId, - rowId: rowId, + // rowId: rowId, }); } - @Delete(['/api/v1/base/tables/:modelId/rows/:rowId']) + @Delete(['/api/v1/base/tables/:modelId/rows']) @Acl('dataDelete') async dataDelete( @Request() req, @@ -114,7 +114,8 @@ export class DataTableController { modelId: modelId, cookie: req, viewId, - rowId: rowId, + body: req.body, + // rowId: rowId, }); } diff --git a/packages/nocodb/src/schema/swagger.json b/packages/nocodb/src/schema/swagger.json index 912a9fa6d9..3f003ca5d3 100644 --- a/packages/nocodb/src/schema/swagger.json +++ b/packages/nocodb/src/schema/swagger.json @@ -14261,56 +14261,26 @@ } } } - } - }, - "/api/v1/base/tables/{tableId}/rows/{rowId}": { - "parameters": [ - { - "schema": { - "type": "string" - }, - "name": "tableId", - "in": "path", - "required": true, - "description": "Table Id" - }, - { - "schema": { - "type": "string" - }, - "name": "viewId", - "in": "query", - "required": true - }, - { - "schema": { - "example": "1" - }, - "name": "rowId", - "in": "path", - "required": true, - "description": "Unique Row ID" - } - ], - "get": { - "summary": "Get Table View Row", - "operationId": "db-view-row-read", + }, + + "patch": { + "summary": "Update Table View Row", + "operationId": "table-row-update", "responses": { "200": { "description": "OK", "content": { "application/json": { "schema": { - "type": "object", - "properties": {} + "type": "object" }, "examples": { "Example 1": { "value": { "Id": 1, - "Title": "foo", + "Title": "bar", "CreatedAt": "2023-03-11T09:11:47.437Z", - "UpdatedAt": "2023-03-11T09:11:47.784Z" + "UpdatedAt": "2023-03-11T09:20:21.133Z" } } } @@ -14321,33 +14291,55 @@ "$ref": "#/components/responses/BadRequest" } }, - "description": "Get the target Table View Row", "tags": ["DB Table Row"], + "requestBody": { + "content": { + "application/json": { + "schema": { + "oneOf": [ + { + "type": "object" + }, + { + "type": "array", + "items": { + "type": "object" + } + } + ] + }, + "examples": { + "Example 1": { + "value": { + "Id": 1, + "Title": "bar" + } + } + } + } + } + }, + "description": "Update the target Table View Row", "parameters": [ { "$ref": "#/components/parameters/xc-auth" } ] }, - "patch": { - "summary": "Update Table View Row", - "operationId": "table-row-update", + "delete": { + "summary": "Delete Table View Row", + "operationId": "table-row-delete", "responses": { "200": { "description": "OK", "content": { "application/json": { "schema": { - "type": "object" + "type": "number" }, "examples": { "Example 1": { - "value": { - "Id": 1, - "Title": "bar", - "CreatedAt": "2023-03-11T09:11:47.437Z", - "UpdatedAt": "2023-03-11T09:20:21.133Z" - } + "value": 1 } } } @@ -14357,44 +14349,91 @@ "$ref": "#/components/responses/BadRequest" } }, - "tags": ["DB Table Row"], + "requestBody": { "content": { "application/json": { "schema": { - "type": "object" + "oneOf": [ + { + "type": "object" + }, + { + "type": "array", + "items": { + "type": "object" + } + } + ] }, "examples": { "Example 1": { "value": { - "Title": "bar" + "Id": 1 } } } } } }, - "description": "Update the target Table View Row", + "tags": ["DB Table Row"], + "description": "Delete the target Table View Row", "parameters": [ { "$ref": "#/components/parameters/xc-auth" } ] - }, - "delete": { - "summary": "Delete Table View Row", - "operationId": "table-row-delete", + } + }, + "/api/v1/base/tables/{tableId}/rows/{rowId}": { + "parameters": [ + { + "schema": { + "type": "string" + }, + "name": "tableId", + "in": "path", + "required": true, + "description": "Table Id" + }, + { + "schema": { + "type": "string" + }, + "name": "viewId", + "in": "query", + "required": true + }, + { + "schema": { + "example": "1" + }, + "name": "rowId", + "in": "path", + "required": true, + "description": "Unique Row ID" + } + ], + "get": { + "summary": "Get Table View Row", + "operationId": "db-view-row-read", "responses": { "200": { "description": "OK", "content": { "application/json": { "schema": { - "type": "number" + "type": "object", + "properties": {} }, "examples": { "Example 1": { - "value": 1 + "value": { + "Id": 1, + "Title": "foo", + "CreatedAt": "2023-03-11T09:11:47.437Z", + "UpdatedAt": "2023-03-11T09:11:47.784Z" + } } } } @@ -14404,8 +14443,8 @@ "$ref": "#/components/responses/BadRequest" } }, + "description": "Get the target Table View Row", "tags": ["DB Table Row"], - "description": "Delete the target Table View Row", "parameters": [ { "$ref": "#/components/parameters/xc-auth" diff --git a/packages/nocodb/src/services/data-table.service.ts b/packages/nocodb/src/services/data-table.service.ts index b6a19d238b..b0a3bb72fe 100644 --- a/packages/nocodb/src/services/data-table.service.ts +++ b/packages/nocodb/src/services/data-table.service.ts @@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common'; import { NcError } from '../helpers/catchError'; import { Base, Model, View } from '../models'; import NcConnectionMgrv2 from '../utils/common/NcConnectionMgrv2'; +import projectAcl from '../utils/projectAcl'; import { DatasService } from './datas.service'; @Injectable() @@ -77,7 +78,7 @@ export class DataTableService { projectId?: string; modelId: string; viewId?: string; - rowId: string; + // rowId: string; body: any; cookie: any; }) { @@ -91,35 +92,51 @@ export class DataTableService { dbDriver: await NcConnectionMgrv2.get(base), }); - return await baseModel.updateByPk( - param.rowId, - param.body, - null, - param.cookie, + // 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]; } async dataDelete(param: { projectId?: string; modelId: string; viewId?: string; - rowId: string; + // rowId: string; cookie: any; + body: any; }) { const { model, view } = await this.getModelAndView(param); const base = await Base.get(model.base_id); const baseModel = await Model.getBaseModelSQL({ id: model.id, viewId: view?.id, - dbDriver: await NcConnectionMgrv2.get(base), + 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( + Array.isArray(param.body) ? param.body : [param.body], + { cookie: param.cookie }, + ); - // 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); + return Array.isArray(param.body) ? res : res[0]; } async dataCount(param: {