From ced48a282c4114706d82fb62357a634cb7042ae4 Mon Sep 17 00:00:00 2001 From: Pranav C Date: Sat, 4 Mar 2023 11:17:53 +0530 Subject: [PATCH] refactor: move nested data(LTAR) apis logic to service Signed-off-by: Pranav C --- .../dataApis/dataAliasNestedController.ts | 248 +++------------- .../dataService/dataAliasNestedService.ts | 280 ++++++++++++++++++ packages/nocodb/src/lib/services/index.ts | 1 + 3 files changed, 328 insertions(+), 201 deletions(-) create mode 100644 packages/nocodb/src/lib/services/dataService/dataAliasNestedService.ts diff --git a/packages/nocodb/src/lib/controllers/dataApis/dataAliasNestedController.ts b/packages/nocodb/src/lib/controllers/dataApis/dataAliasNestedController.ts index 3398bdffa3..9fc5169ee3 100644 --- a/packages/nocodb/src/lib/controllers/dataApis/dataAliasNestedController.ts +++ b/packages/nocodb/src/lib/controllers/dataApis/dataAliasNestedController.ts @@ -1,223 +1,79 @@ import { Request, Response, Router } from 'express'; -import Model from '../../models/Model'; -import Base from '../../models/Base'; -import { PagedResponseImpl } from '../../meta/helpers/PagedResponse'; import ncMetaAclMw from '../../meta/helpers/ncMetaAclMw'; -import NcConnectionMgrv2 from '../../utils/common/NcConnectionMgrv2' -import { - getColumnByIdOrName, - getViewAndModelFromRequestByAliasOrId, -} from './helpers'; -import { NcError } from '../../meta/helpers/catchError'; import apiMetrics from '../../meta/helpers/apiMetrics'; +import { dataAliasNestedService } from '../../services'; // todo: handle case where the given column is not ltar -export async function mmList(req: Request, res: Response, next) { - const { model, view } = await getViewAndModelFromRequestByAliasOrId(req); - - if (!model) return next(new Error('Table not found')); - - const base = await Base.get(model.base_id); - - const baseModel = await Model.getBaseModelSQL({ - id: model.id, - viewId: view?.id, - dbDriver: NcConnectionMgrv2.get(base), - }); - - const column = await getColumnByIdOrName(req.params.columnName, model); - - const data = await baseModel.mmList( - { - colId: column.id, - parentId: req.params.rowId, - }, - req.query as any - ); - const count: any = await baseModel.mmListCount({ - colId: column.id, - parentId: req.params.rowId, - }); - +export async function mmList(req: Request, res: Response) { res.json( - new PagedResponseImpl(data, { - count, - ...req.query, + await dataAliasNestedService.mmList({ + query: req.query, + columnName: req.params.columnName, + rowId: req.params.rowId, + projectName: req.params.projectName, + tableName: req.params.tableName, }) ); } -export async function mmExcludedList(req: Request, res: Response, next) { - const { model, view } = await getViewAndModelFromRequestByAliasOrId(req); - if (!model) return next(new Error('Table not found')); - - const base = await Base.get(model.base_id); - - const baseModel = await Model.getBaseModelSQL({ - id: model.id, - viewId: view?.id, - dbDriver: NcConnectionMgrv2.get(base), - }); - const column = await getColumnByIdOrName(req.params.columnName, model); - - const data = await baseModel.getMmChildrenExcludedList( - { - colId: column.id, - pid: req.params.rowId, - }, - req.query - ); - - const count = await baseModel.getMmChildrenExcludedListCount( - { - colId: column.id, - pid: req.params.rowId, - }, - req.query - ); - +export async function mmExcludedList(req: Request, res: Response) { res.json( - new PagedResponseImpl(data, { - count, - ...req.query, + await dataAliasNestedService.mmExcludedList({ + query: req.query, + columnName: req.params.columnName, + rowId: req.params.rowId, + projectName: req.params.projectName, + tableName: req.params.tableName, }) ); } -export async function hmExcludedList(req: Request, res: Response, next) { - const { model, view } = await getViewAndModelFromRequestByAliasOrId(req); - - if (!model) return next(new Error('Table not found')); - - const base = await Base.get(model.base_id); - - const baseModel = await Model.getBaseModelSQL({ - id: model.id, - viewId: view?.id, - dbDriver: NcConnectionMgrv2.get(base), - }); - - const column = await getColumnByIdOrName(req.params.columnName, model); - - const data = await baseModel.getHmChildrenExcludedList( - { - colId: column.id, - pid: req.params.rowId, - }, - req.query - ); - - const count = await baseModel.getHmChildrenExcludedListCount( - { - colId: column.id, - pid: req.params.rowId, - }, - req.query - ); - +export async function hmExcludedList(req: Request, res: Response) { res.json( - new PagedResponseImpl(data, { - count, - ...req.query, + await dataAliasNestedService.hmExcludedList({ + query: req.query, + columnName: req.params.columnName, + rowId: req.params.rowId, + projectName: req.params.projectName, + tableName: req.params.tableName, }) ); } -export async function btExcludedList(req: Request, res: Response, next) { - const { model, view } = await getViewAndModelFromRequestByAliasOrId(req); - if (!model) return next(new Error('Table not found')); - - const base = await Base.get(model.base_id); - - const baseModel = await Model.getBaseModelSQL({ - id: model.id, - viewId: view?.id, - dbDriver: NcConnectionMgrv2.get(base), - }); - - const column = await getColumnByIdOrName(req.params.columnName, model); - - const data = await baseModel.getBtChildrenExcludedList( - { - colId: column.id, - cid: req.params.rowId, - }, - req.query - ); - - const count = await baseModel.getBtChildrenExcludedListCount( - { - colId: column.id, - cid: req.params.rowId, - }, - req.query - ); - +export async function btExcludedList(req: Request, res: Response) { res.json( - new PagedResponseImpl(data, { - count, - ...req.query, + await dataAliasNestedService.btExcludedList({ + query: req.query, + columnName: req.params.columnName, + rowId: req.params.rowId, + projectName: req.params.projectName, + tableName: req.params.tableName, }) ); } // todo: handle case where the given column is not ltar -export async function hmList(req: Request, res: Response, next) { - const { model, view } = await getViewAndModelFromRequestByAliasOrId(req); - if (!model) return next(new Error('Table not found')); - - const base = await Base.get(model.base_id); - - const baseModel = await Model.getBaseModelSQL({ - id: model.id, - viewId: view?.id, - dbDriver: NcConnectionMgrv2.get(base), - }); - - const column = await getColumnByIdOrName(req.params.columnName, model); - - const data = await baseModel.hmList( - { - colId: column.id, - id: req.params.rowId, - }, - req.query - ); - - const count = await baseModel.hmListCount({ - colId: column.id, - id: req.params.rowId, - }); - +export async function hmList(req: Request, res: Response) { res.json( - new PagedResponseImpl(data, { - count, - ...req.query, - } as any) + await dataAliasNestedService.hmList({ + query: req.query, + columnName: req.params.columnName, + rowId: req.params.rowId, + projectName: req.params.projectName, + tableName: req.params.tableName, + }) ); } //@ts-ignore async function relationDataRemove(req, res) { - const { model, view } = await getViewAndModelFromRequestByAliasOrId(req); - - if (!model) NcError.notFound('Table not found'); - - const base = await Base.get(model.base_id); - - const baseModel = await Model.getBaseModelSQL({ - id: model.id, - viewId: view?.id, - dbDriver: NcConnectionMgrv2.get(base), - }); - - const column = await getColumnByIdOrName(req.params.columnName, model); - - await baseModel.removeChild({ - colId: column.id, - childId: req.params.refRowId, + await dataAliasNestedService.relationDataRemove({ + columnName: req.params.columnName, rowId: req.params.rowId, + projectName: req.params.projectName, + tableName: req.params.tableName, cookie: req, + refRowId: req.params.refRowId, }); res.json({ msg: 'success' }); @@ -226,23 +82,13 @@ async function relationDataRemove(req, res) { //@ts-ignore // todo: Give proper error message when reference row is already related and handle duplicate ref row id in hm async function relationDataAdd(req, res) { - const { model, view } = await getViewAndModelFromRequestByAliasOrId(req); - if (!model) NcError.notFound('Table not found'); - - const base = await Base.get(model.base_id); - - const baseModel = await Model.getBaseModelSQL({ - id: model.id, - viewId: view?.id, - dbDriver: NcConnectionMgrv2.get(base), - }); - - const column = await getColumnByIdOrName(req.params.columnName, model); - await baseModel.addChild({ - colId: column.id, - childId: req.params.refRowId, + await dataAliasNestedService.relationDataAdd({ + columnName: req.params.columnName, rowId: req.params.rowId, + projectName: req.params.projectName, + tableName: req.params.tableName, cookie: req, + refRowId: req.params.refRowId, }); res.json({ msg: 'success' }); diff --git a/packages/nocodb/src/lib/services/dataService/dataAliasNestedService.ts b/packages/nocodb/src/lib/services/dataService/dataAliasNestedService.ts new file mode 100644 index 0000000000..20959d15fa --- /dev/null +++ b/packages/nocodb/src/lib/services/dataService/dataAliasNestedService.ts @@ -0,0 +1,280 @@ +import { PagedResponseImpl } from '../../meta/helpers/PagedResponse'; +import { Base, Model } from '../../models'; +import NcConnectionMgrv2 from '../../utils/common/NcConnectionMgrv2'; +import { + getColumnByIdOrName, + getViewAndModelByAliasOrId, + PathParams, +} from './helpers'; +import { NcError } from '../../meta/helpers/catchError'; + +// todo: handle case where the given column is not ltar +export async function mmList( + param: PathParams & { + query: any; + columnName: string; + rowId: string; + } +) { + const { model, view } = await getViewAndModelByAliasOrId(param); + + if (!model) NcError.notFound('Table not found'); + + const base = await Base.get(model.base_id); + + const baseModel = await Model.getBaseModelSQL({ + id: model.id, + viewId: view?.id, + dbDriver: NcConnectionMgrv2.get(base), + }); + + const column = await getColumnByIdOrName(param.columnName, model); + + const data = await baseModel.mmList( + { + colId: column.id, + parentId: param.rowId, + }, + param.query as any + ); + const count: any = await baseModel.mmListCount({ + colId: column.id, + parentId: param.rowId, + }); + + return new PagedResponseImpl(data, { + count, + ...param.query, + }); +} + +export async function mmExcludedList( + param: PathParams & { + query: any; + columnName: string; + rowId: string; + } +) { + const { model, view } = await getViewAndModelByAliasOrId(param); + if (!model) NcError.notFound('Table not found'); + + const base = await Base.get(model.base_id); + + const baseModel = await Model.getBaseModelSQL({ + id: model.id, + viewId: view?.id, + dbDriver: NcConnectionMgrv2.get(base), + }); + const column = await getColumnByIdOrName(param.columnName, model); + + const data = await baseModel.getMmChildrenExcludedList( + { + colId: column.id, + pid: param.rowId, + }, + param.query + ); + + const count = await baseModel.getMmChildrenExcludedListCount( + { + colId: column.id, + pid: param.rowId, + }, + param.query + ); + + return new PagedResponseImpl(data, { + count, + ...param.query, + }); +} + +export async function hmExcludedList( + param: PathParams & { + query: any; + columnName: string; + rowId: string; + } +) { + const { model, view } = await getViewAndModelByAliasOrId(param); + + if (!model) NcError.notFound('Table not found'); + + const base = await Base.get(model.base_id); + + const baseModel = await Model.getBaseModelSQL({ + id: model.id, + viewId: view?.id, + dbDriver: NcConnectionMgrv2.get(base), + }); + + const column = await getColumnByIdOrName(param.columnName, model); + + const data = await baseModel.getHmChildrenExcludedList( + { + colId: column.id, + pid: param.rowId, + }, + param.query + ); + + const count = await baseModel.getHmChildrenExcludedListCount( + { + colId: column.id, + pid: param.rowId, + }, + param.query + ); + + return new PagedResponseImpl(data, { + count, + ...param.query, + }); +} + +export async function btExcludedList( + param: PathParams & { + query: any; + columnName: string; + rowId: string; + } +) { + const { model, view } = await getViewAndModelByAliasOrId(param); + if (!model) NcError.notFound('Table not found'); + + const base = await Base.get(model.base_id); + + const baseModel = await Model.getBaseModelSQL({ + id: model.id, + viewId: view?.id, + dbDriver: NcConnectionMgrv2.get(base), + }); + + const column = await getColumnByIdOrName(param.columnName, model); + + const data = await baseModel.getBtChildrenExcludedList( + { + colId: column.id, + cid: param.rowId, + }, + param.query + ); + + const count = await baseModel.getBtChildrenExcludedListCount( + { + colId: column.id, + cid: param.rowId, + }, + param.query + ); + + return new PagedResponseImpl(data, { + count, + ...param.query, + }); +} + +// todo: handle case where the given column is not ltar +export async function hmList( + param: PathParams & { + query: any; + columnName: string; + rowId: string; + } +) { + const { model, view } = await getViewAndModelByAliasOrId(param); + + if (!model) NcError.notFound('Table not found'); + + const base = await Base.get(model.base_id); + + const baseModel = await Model.getBaseModelSQL({ + id: model.id, + viewId: view?.id, + dbDriver: NcConnectionMgrv2.get(base), + }); + + const column = await getColumnByIdOrName(param.columnName, model); + + const data = await baseModel.hmList( + { + colId: column.id, + id: param.rowId, + }, + param.query + ); + + const count = await baseModel.hmListCount({ + colId: column.id, + id: param.rowId, + }); + + return new PagedResponseImpl(data, { + count, + ...param.query, + } as any); +} + +//@ts-ignore +export async function relationDataRemove( + param: PathParams & { + columnName: string; + rowId: string; + refRowId: string; + cookie: any; + } +) { + const { model, view } = await getViewAndModelByAliasOrId(param); + if (!model) NcError.notFound('Table not found'); + + const base = await Base.get(model.base_id); + + const baseModel = await Model.getBaseModelSQL({ + id: model.id, + viewId: view?.id, + dbDriver: NcConnectionMgrv2.get(base), + }); + + const column = await getColumnByIdOrName(param.columnName, model); + + await baseModel.removeChild({ + colId: column.id, + childId: param.refRowId, + rowId: param.rowId, + cookie: param.cookie, + }); + + return true; +} + +//@ts-ignore +// todo: Give proper error message when reference row is already related and handle duplicate ref row id in hm +export async function relationDataAdd( + param: PathParams & { + columnName: string; + rowId: string; + refRowId: string; + cookie: any; + } +) { + const { model, view } = await getViewAndModelByAliasOrId(param); + if (!model) NcError.notFound('Table not found'); + + const base = await Base.get(model.base_id); + + const baseModel = await Model.getBaseModelSQL({ + id: model.id, + viewId: view?.id, + dbDriver: NcConnectionMgrv2.get(base), + }); + + const column = await getColumnByIdOrName(param.columnName, model); + await baseModel.addChild({ + colId: column.id, + childId: param.refRowId, + rowId: param.rowId, + cookie: param.cookie, + }); + + return true; +} diff --git a/packages/nocodb/src/lib/services/index.ts b/packages/nocodb/src/lib/services/index.ts index f99398753c..c58049aa62 100644 --- a/packages/nocodb/src/lib/services/index.ts +++ b/packages/nocodb/src/lib/services/index.ts @@ -27,6 +27,7 @@ export * as attachmentService from './attachmentService'; export * as hookFilterService from './hookFilterService'; export * as dataService from './dataService'; export * as bulkDataService from './dataService/bulkData'; +export * as dataAliasNestedService from './dataService/dataAliasNestedService'; export * as cacheService from './cacheService'; export * as auditService from './auditService'; export * as swaggerService from './swaggerService';