Browse Source

refactor: bulk data apis

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5239/head
Pranav C 2 years ago
parent
commit
8c9b85693c
  1. 91
      packages/nocodb/src/lib/controllers/dataController/bulkData.ts
  2. 102
      packages/nocodb/src/lib/services/dataService/bulkData.ts
  3. 31
      packages/nocodb/src/lib/services/dataService/helpers.ts
  4. 28
      packages/nocodb/src/lib/services/dataService/index.ts
  5. 1
      packages/nocodb/src/lib/services/index.ts

91
packages/nocodb/src/lib/controllers/dataController/bulkData.ts

@ -0,0 +1,91 @@
import { Request, Response, Router } from 'express';
import ncMetaAclMw from '../../meta/helpers/ncMetaAclMw';
import apiMetrics from '../../meta/helpers/apiMetrics';
import { bulkDataService } from '../../services';
async function bulkDataInsert(req: Request, res: Response) {
res.json(
await bulkDataService.bulkDataInsert({
body: req.body,
cookie: req,
projectName: req.params.projectName,
tableName: req.params.tableName,
})
);
}
async function bulkDataUpdate(req: Request, res: Response) {
res.json(
await bulkDataService.bulkDataUpdate({
body: req.body,
cookie: req,
projectName: req.params.projectName,
tableName: req.params.tableName,
})
);
}
// todo: Integrate with filterArrJson bulkDataUpdateAll
async function bulkDataUpdateAll(req: Request, res: Response) {
res.json(
await bulkDataService.bulkDataUpdateAll({
body: req.body,
cookie: req,
projectName: req.params.projectName,
tableName: req.params.tableName,
query: req.query,
})
);
}
async function bulkDataDelete(req: Request, res: Response) {
res.json(
await bulkDataService.bulkDataDelete({
body: req.body,
cookie: req,
projectName: req.params.projectName,
tableName: req.params.tableName,
})
);
}
// todo: Integrate with filterArrJson bulkDataDeleteAll
async function bulkDataDeleteAll(req: Request, res: Response) {
res.json(
await bulkDataService.bulkDataDeleteAll({
// cookie: req,
projectName: req.params.projectName,
tableName: req.params.tableName,
query: req.query,
})
);
}
const router = Router({ mergeParams: true });
router.post(
'/api/v1/db/data/bulk/:orgs/:projectName/:tableName',
apiMetrics,
ncMetaAclMw(bulkDataInsert, 'bulkDataInsert')
);
router.patch(
'/api/v1/db/data/bulk/:orgs/:projectName/:tableName',
apiMetrics,
ncMetaAclMw(bulkDataUpdate, 'bulkDataUpdate')
);
router.patch(
'/api/v1/db/data/bulk/:orgs/:projectName/:tableName/all',
apiMetrics,
ncMetaAclMw(bulkDataUpdateAll, 'bulkDataUpdateAll')
);
router.delete(
'/api/v1/db/data/bulk/:orgs/:projectName/:tableName',
apiMetrics,
ncMetaAclMw(bulkDataDelete, 'bulkDataDelete')
);
router.delete(
'/api/v1/db/data/bulk/:orgs/:projectName/:tableName/all',
apiMetrics,
ncMetaAclMw(bulkDataDeleteAll, 'bulkDataDeleteAll')
);
export default router;

102
packages/nocodb/src/lib/services/dataService/bulkData.ts

@ -0,0 +1,102 @@
import { BaseModelSqlv2 } from '../../db/sql-data-mapper/lib/sql/BaseModelSqlv2';
import { Base, Model } from '../../models';
import NcConnectionMgrv2 from '../../utils/common/NcConnectionMgrv2';
import { getViewAndModelByAliasOrId, PathParams } from './helpers';
type BulkOperation =
| 'bulkInsert'
| 'bulkUpdate'
| 'bulkUpdateAll'
| 'bulkDelete'
| 'bulkDeleteAll';
export async function getModelViewBase(param: PathParams) {
const { model, view } = await getViewAndModelByAliasOrId(param);
const base = await Base.get(model.base_id);
return { model, view, base };
}
export async function executeBulkOperation<T extends BulkOperation>(
param: PathParams & {
operation: T;
options: Parameters<typeof BaseModelSqlv2.prototype[T]>;
}
) {
const { model, view, base } = await getModelViewBase(param);
const baseModel = await Model.getBaseModelSQL({
id: model.id,
viewId: view?.id,
dbDriver: NcConnectionMgrv2.get(base),
});
return await baseModel[param.operation].apply(null, param.options);
}
// todo: Integrate with filterArrJson bulkDataUpdateAll
export async function bulkDataInsert(
param: PathParams & {
body: any;
cookie: any;
}
) {
return await executeBulkOperation({
...param,
operation: 'bulkInsert',
options: [param.body, { cookie: param.cookie }],
});
}
// todo: Integrate with filterArrJson bulkDataUpdateAll
export async function bulkDataUpdate(
param: PathParams & {
body: any;
cookie: any;
}
) {
return await executeBulkOperation({
...param,
operation: 'bulkUpdate',
options: [param.body, { cookie: param.cookie }],
});
}
// todo: Integrate with filterArrJson bulkDataUpdateAll
export async function bulkDataUpdateAll(
param: PathParams & {
body: any;
cookie: any;
query: any;
}
) {
return await executeBulkOperation({
...param,
operation: 'bulkUpdateAll',
options: [param.query, param.body, { cookie: param.cookie }],
});
}
export async function bulkDataDelete(
param: PathParams & {
body: any;
cookie: any;
}
) {
return await executeBulkOperation({
...param,
operation: 'bulkDelete',
options: [param.body, { cookie: param.cookie }],
});
}
// todo: Integrate with filterArrJson bulkDataDeleteAll
export async function bulkDataDeleteAll(
param: PathParams & {
query: any;
}
) {
return await executeBulkOperation({
...param,
operation: 'bulkDeleteAll',
options: [param.query],
});
}

31
packages/nocodb/src/lib/services/dataService/helpers.ts

@ -0,0 +1,31 @@
import { NcError } from '../../meta/helpers/catchError'
import { Model, View } from '../../models'
import Project from '../../models/Project'
export interface PathParams {
projectName: string;
tableName: string;
viewName?: string;
}
export async function getViewAndModelByAliasOrId(param: {
projectName: string;
tableName: string;
viewName?: string;
}) {
const project = await Project.getWithInfoByTitleOrId(param.projectName);
const model = await Model.getByAliasOrId({
project_id: project.id,
aliasOrId: param.tableName,
});
const view =
param.viewName &&
(await View.getByTitleOrId({
titleOrId: param.viewName,
fk_model_id: model.id,
}));
if (!model) NcError.notFound('Table not found');
return { model, view };
}

28
packages/nocodb/src/lib/services/dataService/index.ts

@ -3,14 +3,8 @@ import getAst from '../../db/sql-data-mapper/lib/sql/helpers/getAst';
import { NcError } from '../../meta/helpers/catchError';
import { PagedResponseImpl } from '../../meta/helpers/PagedResponse';
import { Base, Model, View } from '../../models';
import Project from '../../models/Project';
import NcConnectionMgrv2 from '../../utils/common/NcConnectionMgrv2';
interface PathParams {
projectName: string;
tableName: string;
viewName: string;
}
import { getViewAndModelByAliasOrId, PathParams } from './helpers'
export async function dataList(param: PathParams & { query: any }) {
const { model, view } = await getViewAndModelByAliasOrId(param);
@ -328,26 +322,6 @@ export async function getGroupedDataList(param: {
return data;
}
export async function getViewAndModelByAliasOrId(param: {
projectName: string;
tableName: string;
viewName?: string;
}) {
const project = await Project.getWithInfoByTitleOrId(param.projectName);
const model = await Model.getByAliasOrId({
project_id: project.id,
aliasOrId: param.tableName,
});
const view =
param.viewName &&
(await View.getByTitleOrId({
titleOrId: param.viewName,
fk_model_id: model.id,
}));
if (!model) NcError.notFound('Table not found');
return { model, view };
}
export async function dataListByViewId(param: { viewId: string; query: any }) {
const view = await View.get(param.viewId);

1
packages/nocodb/src/lib/services/index.ts

@ -27,3 +27,4 @@ export * as projectUserService from './projectUserService';
export * as attachmentService from './attachmentService';
export * as hookFilterService from './hookFilterService';
export * as dataService from './dataService';
export * as bulkDataService from './dataService/bulkData';

Loading…
Cancel
Save