Browse Source

feat: if array of values passed then do bulk insert

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5901/head
Pranav C 2 years ago
parent
commit
bd9620c073
  1. 4
      packages/noco-docs/content/en/developer-resources/rest-apis.md
  2. 9
      packages/nocodb/src/controllers/data-table.controller.ts
  3. 159
      packages/nocodb/src/schema/swagger.json
  4. 45
      packages/nocodb/src/services/data-table.service.ts

4
packages/noco-docs/content/en/developer-resources/rest-apis.md

@ -77,8 +77,8 @@ Currently, the default value for {orgs} is <b>noco</b>. Users will be able to ch
| Data | Get | dbTableRow | tableRowList | /api/v1/base/{baseId}/tables/{tableId} | | Data | Get | dbTableRow | tableRowList | /api/v1/base/{baseId}/tables/{tableId} |
| Data | Post | dbTableRow | tableRowCreate | /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 | Get | dbTableRow | tableRowRead | /api/v1/base/tables/{tableId}/rows/{rowId} |
| Data | Patch | dbTableRow | tableRowUpdate | /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/{rowId} | | Data | Delete| dbTableRow | tableRowDelete | /api/v1/base/tables/{tableId}/rows |
| Data | Get | dbTableRow | tableRowCount | /api/v1/base/{baseId}/tables/{tableId}/count | | Data | Get | dbTableRow | tableRowCount | /api/v1/base/{baseId}/tables/{tableId}/count |
### Meta APIs ### Meta APIs

9
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') @Acl('dataUpdate')
async dataUpdate( async dataUpdate(
@Request() req, @Request() req,
@ -98,11 +98,11 @@ export class DataTableController {
body: req.body, body: req.body,
cookie: req, cookie: req,
viewId, viewId,
rowId: rowId, // rowId: rowId,
}); });
} }
@Delete(['/api/v1/base/tables/:modelId/rows/:rowId']) @Delete(['/api/v1/base/tables/:modelId/rows'])
@Acl('dataDelete') @Acl('dataDelete')
async dataDelete( async dataDelete(
@Request() req, @Request() req,
@ -114,7 +114,8 @@ export class DataTableController {
modelId: modelId, modelId: modelId,
cookie: req, cookie: req,
viewId, viewId,
rowId: rowId, body: req.body,
// rowId: rowId,
}); });
} }

159
packages/nocodb/src/schema/swagger.json

@ -14261,56 +14261,26 @@
} }
} }
} }
} },
},
"/api/v1/base/tables/{tableId}/rows/{rowId}": { "patch": {
"parameters": [ "summary": "Update Table View Row",
{ "operationId": "table-row-update",
"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": { "responses": {
"200": { "200": {
"description": "OK", "description": "OK",
"content": { "content": {
"application/json": { "application/json": {
"schema": { "schema": {
"type": "object", "type": "object"
"properties": {}
}, },
"examples": { "examples": {
"Example 1": { "Example 1": {
"value": { "value": {
"Id": 1, "Id": 1,
"Title": "foo", "Title": "bar",
"CreatedAt": "2023-03-11T09:11:47.437Z", "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" "$ref": "#/components/responses/BadRequest"
} }
}, },
"description": "Get the target Table View Row",
"tags": ["DB Table 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": [ "parameters": [
{ {
"$ref": "#/components/parameters/xc-auth" "$ref": "#/components/parameters/xc-auth"
} }
] ]
}, },
"patch": { "delete": {
"summary": "Update Table View Row", "summary": "Delete Table View Row",
"operationId": "table-row-update", "operationId": "table-row-delete",
"responses": { "responses": {
"200": { "200": {
"description": "OK", "description": "OK",
"content": { "content": {
"application/json": { "application/json": {
"schema": { "schema": {
"type": "object" "type": "number"
}, },
"examples": { "examples": {
"Example 1": { "Example 1": {
"value": { "value": 1
"Id": 1,
"Title": "bar",
"CreatedAt": "2023-03-11T09:11:47.437Z",
"UpdatedAt": "2023-03-11T09:20:21.133Z"
}
} }
} }
} }
@ -14357,44 +14349,91 @@
"$ref": "#/components/responses/BadRequest" "$ref": "#/components/responses/BadRequest"
} }
}, },
"tags": ["DB Table Row"],
"requestBody": { "requestBody": {
"content": { "content": {
"application/json": { "application/json": {
"schema": { "schema": {
"type": "object" "oneOf": [
{
"type": "object"
},
{
"type": "array",
"items": {
"type": "object"
}
}
]
}, },
"examples": { "examples": {
"Example 1": { "Example 1": {
"value": { "value": {
"Title": "bar" "Id": 1
} }
} }
} }
} }
} }
}, },
"description": "Update the target Table View Row", "tags": ["DB Table Row"],
"description": "Delete the target Table View Row",
"parameters": [ "parameters": [
{ {
"$ref": "#/components/parameters/xc-auth" "$ref": "#/components/parameters/xc-auth"
} }
] ]
}, }
"delete": { },
"summary": "Delete Table View Row", "/api/v1/base/tables/{tableId}/rows/{rowId}": {
"operationId": "table-row-delete", "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": { "responses": {
"200": { "200": {
"description": "OK", "description": "OK",
"content": { "content": {
"application/json": { "application/json": {
"schema": { "schema": {
"type": "number" "type": "object",
"properties": {}
}, },
"examples": { "examples": {
"Example 1": { "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" "$ref": "#/components/responses/BadRequest"
} }
}, },
"description": "Get the target Table View Row",
"tags": ["DB Table Row"], "tags": ["DB Table Row"],
"description": "Delete the target Table View Row",
"parameters": [ "parameters": [
{ {
"$ref": "#/components/parameters/xc-auth" "$ref": "#/components/parameters/xc-auth"

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

@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common';
import { NcError } from '../helpers/catchError'; import { NcError } from '../helpers/catchError';
import { Base, Model, View } from '../models'; import { Base, Model, View } from '../models';
import NcConnectionMgrv2 from '../utils/common/NcConnectionMgrv2'; import NcConnectionMgrv2 from '../utils/common/NcConnectionMgrv2';
import projectAcl from '../utils/projectAcl';
import { DatasService } from './datas.service'; import { DatasService } from './datas.service';
@Injectable() @Injectable()
@ -77,7 +78,7 @@ export class DataTableService {
projectId?: string; projectId?: string;
modelId: string; modelId: string;
viewId?: string; viewId?: string;
rowId: string; // rowId: string;
body: any; body: any;
cookie: any; cookie: any;
}) { }) {
@ -91,35 +92,51 @@ export class DataTableService {
dbDriver: await NcConnectionMgrv2.get(base), dbDriver: await NcConnectionMgrv2.get(base),
}); });
return await baseModel.updateByPk( // return await baseModel.updateByPk(
param.rowId, // param.rowId,
param.body, // param.body,
null, // null,
param.cookie, // 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: { async dataDelete(param: {
projectId?: string; projectId?: string;
modelId: string; modelId: string;
viewId?: string; viewId?: string;
rowId: string; // rowId: string;
cookie: any; cookie: any;
body: any;
}) { }) {
const { model, view } = await this.getModelAndView(param); const { model, view } = await this.getModelAndView(param);
const base = await Base.get(model.base_id); const base = await Base.get(model.base_id);
const baseModel = await Model.getBaseModelSQL({ const baseModel = await Model.getBaseModelSQL({
id: model.id, id: model.id,
viewId: view?.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 return Array.isArray(param.body) ? res : res[0];
const message = await baseModel.hasLTARData(param.rowId, model);
if (message.length) {
return { message };
}
return await baseModel.delByPk(param.rowId, null, param.cookie);
} }
async dataCount(param: { async dataCount(param: {

Loading…
Cancel
Save