From b36b3cdb186641f7dcedd7371b96776c85a64b2e Mon Sep 17 00:00:00 2001 From: Wing-Kam Wong Date: Tue, 11 Apr 2023 13:25:32 +0800 Subject: [PATCH] feat: add old-datas.service.ts --- .../datas/old-datas/old-datas.service.ts | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 packages/nocodb-nest/src/modules/datas/old-datas/old-datas.service.ts diff --git a/packages/nocodb-nest/src/modules/datas/old-datas/old-datas.service.ts b/packages/nocodb-nest/src/modules/datas/old-datas/old-datas.service.ts new file mode 100644 index 0000000000..e0d2640d62 --- /dev/null +++ b/packages/nocodb-nest/src/modules/datas/old-datas/old-datas.service.ts @@ -0,0 +1,93 @@ +import { Injectable } from '@nestjs/common'; +import { getViewAndModelByAliasOrId, PathParams } from '../helpers'; +import getAst from '../../../helpers/getAst'; +import { NcError } from '../../../helpers/catchError'; +import { + Base, + Column, + LinkToAnotherRecordColumn, + LookupColumn, + Model, + Project, + View, +} from '../../../models'; +import NcConnectionMgrv2 from '../../../utils/common/NcConnectionMgrv2'; +import { nocoExecute } from 'nc-help'; + +@Injectable() +export class OldDatasService { + async getDataList(param: PathParams & { query: any }) { + const { model, view } = await this.getViewAndModelFromRequest(param); + const base = await Base.get(model.base_id); + + const baseModel = await Model.getBaseModelSQL({ + id: model.id, + viewId: view?.id, + dbDriver: await NcConnectionMgrv2.get(base), + }); + + const { ast } = await getAst({ + query: param.query, + model, + view, + }); + + const listArgs: any = { ...param.query }; + try { + listArgs.filterArr = JSON.parse(listArgs.filterArrJson); + } catch (e) {} + try { + listArgs.sortArr = JSON.parse(listArgs.sortArrJson); + } catch (e) {} + + return await nocoExecute(ast, await baseModel.list(listArgs), {}, listArgs); + } + + async getDataCount(param: PathParams & { query: any }) { + const { model, view } = await this.getViewAndModelFromRequest(param); + const base = await Base.get(model.base_id); + + const baseModel = await Model.getBaseModelSQL({ + id: model.id, + viewId: view?.id, + dbDriver: await NcConnectionMgrv2.get(base), + }); + + const listArgs: any = { ...param.query }; + try { + listArgs.filterArr = JSON.parse(listArgs.filterArrJson); + } catch (e) {} + + return await baseModel.count(listArgs); + } + + async dataInsert(param: PathParams & { body: unknown }) { + const { model, view } = await this.getViewAndModelFromRequest(param); + + const base = await Base.get(model.base_id); + + const baseModel = await Model.getBaseModelSQL({ + id: model.id, + viewId: view?.id, + dbDriver: await NcConnectionMgrv2.get(base), + }); + + return await baseModel.insert(param.body, null, param); + } + + async getViewAndModelFromRequest(req) { + const project = await Project.getWithInfo(req.params.projectId); + const model = await Model.getByAliasOrId({ + project_id: project.id, + aliasOrId: req.params.tableName, + }); + 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 }; + } +}