mirror of https://github.com/nocodb/nocodb
Pranav C
2 years ago
5 changed files with 226 additions and 27 deletions
@ -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; |
@ -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], |
||||
}); |
||||
} |
@ -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 }; |
||||
} |
Loading…
Reference in new issue