Browse Source

fix: bulk Apis

Signed-off-by: Wing-Kam Wong <wingkwong.code@gmail.com>
pull/1679/head
Wing-Kam Wong 3 years ago
parent
commit
e3606c249e
  1. 17
      packages/nocodb-sdk/src/lib/Api.ts
  2. 44
      packages/nocodb/src/lib/noco/meta/api/dataApis/bulkDataAliasApis.ts
  3. 14
      scripts/sdk/swagger.json

17
packages/nocodb-sdk/src/lib/Api.ts

@ -2267,6 +2267,7 @@ export class Api<
*
* @tags DB Table Row
* @name BulkCreate
* @summary Bulk insert table rows
* @request POST:/api/v1/db/data/bulk/{orgs}/{projectName}/{tableName}
* @response `200` `any` OK
*/
@ -2291,6 +2292,7 @@ export class Api<
*
* @tags DB Table Row
* @name BulkUpdate
* @summary Bulk update all table rows by IDs
* @request PATCH:/api/v1/db/data/bulk/{orgs}/{projectName}/{tableName}
* @response `200` `any` OK
*/
@ -2315,6 +2317,7 @@ export class Api<
*
* @tags DB Table Row
* @name BulkDelete
* @summary Bulk delete all table rows by IDs
* @request DELETE:/api/v1/db/data/bulk/{orgs}/{projectName}/{tableName}
* @response `200` `any` OK
*/
@ -2338,13 +2341,12 @@ export class Api<
* No description
*
* @tags DB Table Row
* @name Create2
* @name BulkUpdateAll
* @summary Bulk update all table rows with conditions
* @request PATCH:/api/v1/db/data/bulk/{orgs}/{projectName}/{tableName}/all
* @originalName create
* @duplicate
* @response `200` `any` OK
*/
create2: (
bulkUpdateAll: (
orgs: string,
projectName: string,
tableName: string,
@ -2364,13 +2366,12 @@ export class Api<
* No description
*
* @tags DB Table Row
* @name Create3
* @name BulkDeleteAll
* @summary Bulk delete all table rows with conditions
* @request DELETE:/api/v1/db/data/bulk/{orgs}/{projectName}/{tableName}/all
* @originalName create
* @duplicate
* @response `200` `any` OK
*/
create3: (
bulkDeleteAll: (
orgs: string,
projectName: string,
tableName: string,

44
packages/nocodb/src/lib/noco/meta/api/dataApis/bulkDataAliasApis.ts

@ -2,13 +2,11 @@ import { Request, Response, Router } from 'express';
import Model from '../../../../noco-models/Model';
import Base from '../../../../noco-models/Base';
import NcConnectionMgrv2 from '../../../common/NcConnectionMgrv2';
import View from '../../../../noco-models/View';
import ncMetaAclMw from '../../helpers/ncMetaAclMw';
import Project from '../../../../noco-models/Project';
import { NcError } from '../../helpers/catchError';
import { getViewAndModelFromRequestByAliasOrId } from './helpers';
async function bulkDataInsert(req: Request, res: Response) {
const { model, view } = await getViewAndModelFromRequest(req);
const { model, view } = await getViewAndModelFromRequestByAliasOrId(req);
const base = await Base.get(model.base_id);
@ -22,7 +20,7 @@ async function bulkDataInsert(req: Request, res: Response) {
}
async function bulkDataUpdate(req: Request, res: Response) {
const { model, view } = await getViewAndModelFromRequest(req);
const { model, view } = await getViewAndModelFromRequestByAliasOrId(req);
const base = await Base.get(model.base_id);
const baseModel = await Model.getBaseModelSQL({
@ -35,7 +33,7 @@ async function bulkDataUpdate(req: Request, res: Response) {
}
async function bulkDataUpdateAll(req: Request, res: Response) {
const { model, view } = await getViewAndModelFromRequest(req);
const { model, view } = await getViewAndModelFromRequestByAliasOrId(req);
const base = await Base.get(model.base_id);
const baseModel = await Model.getBaseModelSQL({
@ -48,7 +46,7 @@ async function bulkDataUpdateAll(req: Request, res: Response) {
}
async function bulkDataDelete(req: Request, res: Response) {
const { model, view } = await getViewAndModelFromRequest(req);
const { model, view } = await getViewAndModelFromRequestByAliasOrId(req);
const base = await Base.get(model.base_id);
const baseModel = await Model.getBaseModelSQL({
id: model.id,
@ -60,7 +58,7 @@ async function bulkDataDelete(req: Request, res: Response) {
}
async function bulkDataDeleteAll(req: Request, res: Response) {
const { model, view } = await getViewAndModelFromRequest(req);
const { model, view } = await getViewAndModelFromRequestByAliasOrId(req);
const base = await Base.get(model.base_id);
const baseModel = await Model.getBaseModelSQL({
id: model.id,
@ -68,46 +66,28 @@ async function bulkDataDeleteAll(req: Request, res: Response) {
dbDriver: NcConnectionMgrv2.get(base)
});
res.json(await baseModel.bulkDeleteAll(req.body));
res.json(await baseModel.bulkDeleteAll(req.query));
}
async function getViewAndModelFromRequest(req) {
const project = await Project.getWithInfoByTitle(req.params.projectName);
const model = await Model.getByAliasOrId({
project_id: project.id,
base_id: project.bases?.[0]?.id,
aliasOrId: req.params.tableAlias
});
const view =
req.params.viewName &&
(await View.getByTitleOrId({
titleOrId: req.params.viewName,
fk_model_id: model.id
}));
if (!model) NcError.notFound('Table not found');
return { model, view };
}
const router = Router({ mergeParams: true });
router.post(
'/api/v1/db/data/bulk/:orgs/:projectName/:tableAlias',
'/api/v1/db/data/bulk/:orgs/:projectName/:tableName',
ncMetaAclMw(bulkDataInsert, 'bulkDataInsert')
);
router.patch(
'/api/v1/db/data/bulk/:orgs/:projectName/:tableAlias',
'/api/v1/db/data/bulk/:orgs/:projectName/:tableName',
ncMetaAclMw(bulkDataUpdate, 'bulkDataUpdate')
);
router.patch(
'/api/v1/db/data/bulk/:orgs/:projectName/:tableAlias/all',
'/api/v1/db/data/bulk/:orgs/:projectName/:tableName/all',
ncMetaAclMw(bulkDataUpdateAll, 'bulkDataUpdateAll')
);
router.delete(
'/api/v1/db/data/bulk/:orgs/:projectName/:tableAlias',
'/api/v1/db/data/bulk/:orgs/:projectName/:tableName',
ncMetaAclMw(bulkDataDelete, 'bulkDataDelete')
);
router.delete(
'/api/v1/db/data/bulk/:orgs/:projectName/:tableAlias/all',
'/api/v1/db/data/bulk/:orgs/:projectName/:tableName/all',
ncMetaAclMw(bulkDataDeleteAll, 'bulkDataDeleteAll')
);

14
scripts/sdk/swagger.json

@ -2769,7 +2769,7 @@
}
],
"post": {
"summary": "",
"summary": "Bulk insert table rows",
"operationId": "db-table-row-bulk-create",
"responses": {
"200": {
@ -2793,7 +2793,7 @@
}
},
"patch": {
"summary": "",
"summary": "Bulk update all table rows by IDs",
"operationId": "db-table-row-bulk-update",
"responses": {
"200": {
@ -2817,7 +2817,7 @@
}
},
"delete": {
"summary": "",
"summary": "Bulk delete all table rows by IDs",
"operationId": "db-table-row-bulk-delete",
"responses": {
"200": {
@ -2869,8 +2869,8 @@
}
],
"patch": {
"summary": "",
"operationId": "db-table-row-create",
"summary": "Bulk update all table rows with conditions",
"operationId": "db-table-row-bulk-update-all",
"responses": {
"200": {
"description": "OK",
@ -2893,8 +2893,8 @@
}
},
"delete": {
"summary": "",
"operationId": "db-table-row-create",
"summary": "Bulk delete all table rows with conditions",
"operationId": "db-table-row-bulk-delete-all",
"responses": {
"200": {
"description": "OK",

Loading…
Cancel
Save