diff --git a/packages/nocodb/src/lib/controllers/dataApis/bulkDataAliasApis.ts b/packages/nocodb/src/lib/controllers/dataApis/bulkDataAliasApis.ts index bef5276be6..968ab16165 100644 --- a/packages/nocodb/src/lib/controllers/dataApis/bulkDataAliasApis.ts +++ b/packages/nocodb/src/lib/controllers/dataApis/bulkDataAliasApis.ts @@ -1,75 +1,69 @@ import { Request, Response, Router } from 'express'; -import { BaseModelSqlv2 } from '../../db/sql-data-mapper/lib/sql/BaseModelSqlv2'; -import Model from '../../models/Model'; -import Base from '../../models/Base'; -import NcConnectionMgrv2 from '../../utils/common/NcConnectionMgrv2'; +import { bulkDataService } from '../../services' import ncMetaAclMw from '../../meta/helpers/ncMetaAclMw'; -import { getViewAndModelFromRequestByAliasOrId } from './helpers'; import apiMetrics from '../../meta/helpers/apiMetrics'; -type BulkOperation = - | 'bulkInsert' - | 'bulkUpdate' - | 'bulkUpdateAll' - | 'bulkDelete' - | 'bulkDeleteAll'; -async function getModelViewBase(req: Request) { - const { model, view } = await getViewAndModelFromRequestByAliasOrId(req); - - const base = await Base.get(model.base_id); - return { model, view, base }; -} - -async function executeBulkOperation( - req: Request, - res: Response, - operation: T, - options: Parameters -) { - const { model, view, base } = await getModelViewBase(req); - const baseModel = await Model.getBaseModelSQL({ - id: model.id, - viewId: view?.id, - dbDriver: NcConnectionMgrv2.get(base), - }); - res.json(await baseModel[operation].apply(null, options)); -} async function bulkDataInsert(req: Request, res: Response) { - await executeBulkOperation(req, res, 'bulkInsert', [ - req.body, - { cookie: req }, - ]); + 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) { - await executeBulkOperation(req, res, 'bulkUpdate', [ - req.body, - { cookie: req }, - ]); + 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) { - await executeBulkOperation(req, res, 'bulkUpdateAll', [ - req.query, - req.body, - { cookie: req }, - ]); + 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) { - await executeBulkOperation(req, res, 'bulkDelete', [ - req.body, - { cookie: req }, - ]); + 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) { - await executeBulkOperation(req, res, 'bulkDeleteAll', [req.query]); + 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( diff --git a/packages/nocodb/src/lib/controllers/dataApis/dataAliasApis.ts b/packages/nocodb/src/lib/controllers/dataApis/dataAliasApis.ts index a1bd604261..cfa35fafe6 100644 --- a/packages/nocodb/src/lib/controllers/dataApis/dataAliasApis.ts +++ b/packages/nocodb/src/lib/controllers/dataApis/dataAliasApis.ts @@ -1,304 +1,131 @@ import { Request, Response, Router } from 'express'; -import Model from '../../models/Model'; -import { nocoExecute } from 'nc-help'; -import Base from '../../models/Base'; -import NcConnectionMgrv2 from '../../utils/common/NcConnectionMgrv2'; -import { NcError } from '../../meta/helpers/catchError'; -import { PagedResponseImpl } from '../../meta/helpers/PagedResponse'; -import View from '../../models/View'; +import { dataService } from '../../services' import ncMetaAclMw from '../../meta/helpers/ncMetaAclMw'; -import { getViewAndModelFromRequestByAliasOrId } from './helpers'; import apiMetrics from '../../meta/helpers/apiMetrics'; -import getAst from '../../db/sql-data-mapper/lib/sql/helpers/getAst'; import { parseHrtimeToSeconds } from '../../meta/api/helpers'; // todo: Handle the error case where view doesnt belong to model async function dataList(req: Request, res: Response) { const startTime = process.hrtime(); - const { model, view } = await getViewAndModelFromRequestByAliasOrId(req); - const responseData = await getDataList(model, view, req); + const responseData = await dataService.dataList({ + query: req.query, + projectName: req.params.projectName, + tableName: req.params.tableName, + viewName: req.params.viewName, + }); const elapsedSeconds = parseHrtimeToSeconds(process.hrtime(startTime)); res.setHeader('xc-db-response', elapsedSeconds); res.json(responseData); } async function dataFindOne(req: Request, res: Response) { - const { model, view } = await getViewAndModelFromRequestByAliasOrId(req); - res.json(await getFindOne(model, view, req)); + res.json( + await dataService.dataFindOne({ + query: req.query, + projectName: req.params.projectName, + tableName: req.params.tableName, + viewName: req.params.viewName, + }) + ); } async function dataGroupBy(req: Request, res: Response) { - const { model, view } = await getViewAndModelFromRequestByAliasOrId(req); - res.json(await getDataGroupBy(model, view, req)); + res.json( + await dataService.dataGroupBy({ + query: req.query, + projectName: req.params.projectName, + tableName: req.params.tableName, + viewName: req.params.viewName, + }) + ); } async function dataCount(req: Request, res: Response) { - const { model, view } = await getViewAndModelFromRequestByAliasOrId(req); - - const base = await Base.get(model.base_id); - - const baseModel = await Model.getBaseModelSQL({ - id: model.id, - viewId: view?.id, - dbDriver: NcConnectionMgrv2.get(base), + const countResult = await dataService.dataCount({ + query: req.query, + projectName: req.params.projectName, + tableName: req.params.tableName, + viewName: req.params.viewName, }); - const countArgs: any = { ...req.query }; - try { - countArgs.filterArr = JSON.parse(countArgs.filterArrJson); - } catch (e) {} - - const count = await baseModel.count(countArgs); - - res.json({ count }); + res.json(countResult); } -// todo: Handle the error case where view doesnt belong to model async function dataInsert(req: Request, res: Response) { - const { model, view } = await getViewAndModelFromRequestByAliasOrId(req); - - const base = await Base.get(model.base_id); - - const baseModel = await Model.getBaseModelSQL({ - id: model.id, - viewId: view?.id, - dbDriver: NcConnectionMgrv2.get(base), - }); - - res.json(await baseModel.insert(req.body, null, req)); + res.json( + await dataService.dataInsert({ + projectName: req.params.projectName, + tableName: req.params.tableName, + viewName: req.params.viewName, + body: req.body, + cookie: req, + }) + ); } async function dataUpdate(req: Request, res: Response) { - const { model, view } = await getViewAndModelFromRequestByAliasOrId(req); - const base = await Base.get(model.base_id); - - const baseModel = await Model.getBaseModelSQL({ - id: model.id, - viewId: view?.id, - dbDriver: NcConnectionMgrv2.get(base), - }); - - res.json(await baseModel.updateByPk(req.params.rowId, req.body, null, req)); + res.json( + await dataService.dataUpdate({ + projectName: req.params.projectName, + tableName: req.params.tableName, + viewName: req.params.viewName, + body: req.body, + cookie: req, + rowId: req.params.rowId, + }) + ); } async function dataDelete(req: Request, res: Response) { - const { model, view } = await getViewAndModelFromRequestByAliasOrId(req); - const base = await Base.get(model.base_id); - const baseModel = await Model.getBaseModelSQL({ - id: model.id, - viewId: view?.id, - dbDriver: NcConnectionMgrv2.get(base), - }); - - // todo: Should have error http status code - const message = await baseModel.hasLTARData(req.params.rowId, model); - if (message.length) { - res.json({ message }); - return; - } - res.json(await baseModel.delByPk(req.params.rowId, null, req)); -} - -async function getDataList(model, view: View, req) { - const base = await Base.get(model.base_id); - - const baseModel = await Model.getBaseModelSQL({ - id: model.id, - viewId: view?.id, - dbDriver: NcConnectionMgrv2.get(base), - }); - - const requestObj = await getAst({ model, query: req.query, view }); - - const listArgs: any = { ...req.query }; - try { - listArgs.filterArr = JSON.parse(listArgs.filterArrJson); - } catch (e) {} - try { - listArgs.sortArr = JSON.parse(listArgs.sortArrJson); - } catch (e) {} - - let data = []; - let count = 0; - try { - data = await nocoExecute( - requestObj, - await baseModel.list(listArgs), - {}, - listArgs - ); - count = await baseModel.count(listArgs); - } catch (e) { - console.log(e); - NcError.internalServerError( - 'Internal Server Error, check server log for more details' - ); - } - - return new PagedResponseImpl(data, { - ...req.query, - count, - }); -} - -async function getFindOne(model, view: View, req) { - const base = await Base.get(model.base_id); - - const baseModel = await Model.getBaseModelSQL({ - id: model.id, - viewId: view?.id, - dbDriver: NcConnectionMgrv2.get(base), - }); - - const args: any = { ...req.query }; - try { - args.filterArr = JSON.parse(args.filterArrJson); - } catch (e) {} - try { - args.sortArr = JSON.parse(args.sortArrJson); - } catch (e) {} - - const data = await baseModel.findOne(args); - return data - ? await nocoExecute( - await getAst({ model, query: args, view }), - data, - {}, - {} - ) - : {}; -} - -async function getDataGroupBy(model, view: View, req) { - const base = await Base.get(model.base_id); - - const baseModel = await Model.getBaseModelSQL({ - id: model.id, - viewId: view?.id, - dbDriver: NcConnectionMgrv2.get(base), - }); - - const listArgs: any = { ...req.query }; - const data = await baseModel.groupBy({ ...req.query }); - const count = await baseModel.count(listArgs); - - return new PagedResponseImpl(data, { - ...req.query, - count, - }); + res.json( + await dataService.dataDelete({ + projectName: req.params.projectName, + tableName: req.params.tableName, + viewName: req.params.viewName, + cookie: req, + rowId: req.params.rowId, + }) + ); } async function dataRead(req: Request, res: Response) { - const { model, view } = await getViewAndModelFromRequestByAliasOrId(req); - - const base = await Base.get(model.base_id); - - const baseModel = await Model.getBaseModelSQL({ - id: model.id, - viewId: view?.id, - dbDriver: NcConnectionMgrv2.get(base), - }); - - const row = await baseModel.readByPk(req.params.rowId); - - if (!row) { - NcError.notFound(); - } - res.json( - await nocoExecute( - await getAst({ model, query: req.query, view }), - row, - {}, - req.query - ) + await dataService.dataRead({ + projectName: req.params.projectName, + tableName: req.params.tableName, + viewName: req.params.viewName, + rowId: req.params.rowId, + query: req.query, + }) ); } async function dataExist(req: Request, res: Response) { - const { model, view } = await getViewAndModelFromRequestByAliasOrId(req); - - const base = await Base.get(model.base_id); - - const baseModel = await Model.getBaseModelSQL({ - id: model.id, - viewId: view?.id, - dbDriver: NcConnectionMgrv2.get(base), - }); - - res.json(await baseModel.exist(req.params.rowId)); + res.json( + await dataService.dataExist({ + projectName: req.params.projectName, + tableName: req.params.tableName, + viewName: req.params.viewName, + rowId: req.params.rowId, + query: req.query, + }) + ); } // todo: Handle the error case where view doesnt belong to model async function groupedDataList(req: Request, res: Response) { const startTime = process.hrtime(); - const { model, view } = await getViewAndModelFromRequestByAliasOrId(req); - const groupedData = await getGroupedDataList(model, view, req); + const groupedData = await dataService.groupedDataList({ + projectName: req.params.projectName, + tableName: req.params.tableName, + viewName: req.params.viewName, + query: req.query, + columnId: req.params.columnId, + }); const elapsedSeconds = parseHrtimeToSeconds(process.hrtime(startTime)); res.setHeader('xc-db-response', elapsedSeconds); res.json(groupedData); } - -async function getGroupedDataList(model, view: View, req) { - const base = await Base.get(model.base_id); - - const baseModel = await Model.getBaseModelSQL({ - id: model.id, - viewId: view?.id, - dbDriver: NcConnectionMgrv2.get(base), - }); - - const requestObj = await getAst({ model, query: req.query, view }); - - const listArgs: any = { ...req.query }; - try { - listArgs.filterArr = JSON.parse(listArgs.filterArrJson); - } catch (e) {} - try { - listArgs.sortArr = JSON.parse(listArgs.sortArrJson); - } catch (e) {} - try { - listArgs.options = JSON.parse(listArgs.optionsArrJson); - } catch (e) {} - - let data = []; - // let count = 0 - try { - const groupedData = await baseModel.groupedList({ - ...listArgs, - groupColumnId: req.params.columnId, - }); - data = await nocoExecute( - { key: 1, value: requestObj }, - groupedData, - {}, - listArgs - ); - const countArr = await baseModel.groupedListCount({ - ...listArgs, - groupColumnId: req.params.columnId, - }); - data = data.map((item) => { - // todo: use map to avoid loop - const count = - countArr.find((countItem: any) => countItem.key === item.key)?.count ?? - 0; - - item.value = new PagedResponseImpl(item.value, { - ...req.query, - count: count, - }); - return item; - }); - } catch (e) { - console.log(e); - NcError.internalServerError( - 'Internal Server Error, check server log for more details' - ); - } - return data; -} - const router = Router({ mergeParams: true }); // table data crud apis diff --git a/packages/nocodb/src/lib/controllers/dataApis/dataAliasExportApis.ts b/packages/nocodb/src/lib/controllers/dataApis/dataAliasExportApis.ts index c628e31814..d67f58f028 100644 --- a/packages/nocodb/src/lib/controllers/dataApis/dataAliasExportApis.ts +++ b/packages/nocodb/src/lib/controllers/dataApis/dataAliasExportApis.ts @@ -1,13 +1,13 @@ import { Request, Response, Router } from 'express'; import * as XLSX from 'xlsx'; +import apiMetrics from '../../meta/helpers/apiMetrics'; import ncMetaAclMw from '../../meta/helpers/ncMetaAclMw'; +import { View } from '../../models'; import { extractCsvData, extractXlsxData, - getViewAndModelFromRequestByAliasOrId, -} from './helpers'; -import apiMetrics from '../../meta/helpers/apiMetrics'; -import View from '../../models/View'; +} from '../../services/dataService/helpers'; +import { getViewAndModelFromRequestByAliasOrId } from './helpers' async function excelDataExport(req: Request, res: Response) { const { model, view } = await getViewAndModelFromRequestByAliasOrId(req); @@ -15,11 +15,7 @@ async function excelDataExport(req: Request, res: Response) { if (!targetView) { targetView = await View.getDefaultView(model.id); } - const { offset, elapsed, data } = await extractXlsxData({ - view: targetView, - query: req.query, - siteUrl: (req as any).ncSiteUrl, - }); + const { offset, elapsed, data } = await extractXlsxData(targetView, req); const wb = XLSX.utils.book_new(); XLSX.utils.book_append_sheet(wb, data, targetView.title); const buf = XLSX.write(wb, { type: 'base64', bookType: 'xlsx' }); diff --git a/packages/nocodb/src/lib/controllers/dataApis/dataAliasNestedApis.ts b/packages/nocodb/src/lib/controllers/dataApis/dataAliasNestedApis.ts index 5151af03b7..3398bdffa3 100644 --- a/packages/nocodb/src/lib/controllers/dataApis/dataAliasNestedApis.ts +++ b/packages/nocodb/src/lib/controllers/dataApis/dataAliasNestedApis.ts @@ -1,9 +1,9 @@ import { Request, Response, Router } from 'express'; import Model from '../../models/Model'; import Base from '../../models/Base'; -import NcConnectionMgrv2 from '../../utils/common/NcConnectionMgrv2'; import { PagedResponseImpl } from '../../meta/helpers/PagedResponse'; import ncMetaAclMw from '../../meta/helpers/ncMetaAclMw'; +import NcConnectionMgrv2 from '../../utils/common/NcConnectionMgrv2' import { getColumnByIdOrName, getViewAndModelFromRequestByAliasOrId, diff --git a/packages/nocodb/src/lib/controllers/dataApis/dataApis.ts b/packages/nocodb/src/lib/controllers/dataApis/dataApis.ts index 7c25607aab..fb5c35ab17 100644 --- a/packages/nocodb/src/lib/controllers/dataApis/dataApis.ts +++ b/packages/nocodb/src/lib/controllers/dataApis/dataApis.ts @@ -1,502 +1,116 @@ import { Request, Response, Router } from 'express'; -import Model from '../../models/Model'; -import { nocoExecute } from 'nc-help'; -import Base from '../../models/Base'; -import NcConnectionMgrv2 from '../../utils/common/NcConnectionMgrv2'; -import { PagedResponseImpl } from '../../meta/helpers/PagedResponse'; -import View from '../../models/View'; +import { dataService } from '../../services'; import ncMetaAclMw from '../../meta/helpers/ncMetaAclMw'; -import { NcError } from '../../meta/helpers/catchError'; import apiMetrics from '../../meta/helpers/apiMetrics'; -import getAst from '../../db/sql-data-mapper/lib/sql/helpers/getAst'; - -export async function dataList(req: Request, res: Response, next) { - const view = await View.get(req.params.viewId); - - const model = await Model.getByIdOrName({ - id: view?.fk_model_id || req.params.viewId, - }); - - if (!model) return next(new Error('Table not found')); - - res.json(await getDataList(model, view, req)); -} - -export async function mmList(req: Request, res: Response, next) { - const view = await View.get(req.params.viewId); - - const model = await Model.getByIdOrName({ - id: view?.fk_model_id || req.params.viewId, - }); - - 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 key = `${model.title}List`; - const requestObj: any = { - [key]: 1, - }; - - const data = ( - await nocoExecute( - requestObj, - { - [key]: async (args) => { - return await baseModel.mmList( - { - colId: req.params.colId, - parentId: req.params.rowId, - }, - args - ); - }, - }, - {}, - - { nested: { [key]: req.query } } - ) - )?.[key]; - - const count: any = await baseModel.mmListCount({ - colId: req.params.colId, - parentId: req.params.rowId, - }); +export async function dataList(req: Request, res: Response) { res.json( - new PagedResponseImpl(data, { - count, - ...req.query, + await dataService.dataListByViewId({ + viewId: req.params.viewId, + query: req.query, }) ); } -export async function mmExcludedList(req: Request, res: Response, next) { - const view = await View.get(req.params.viewId); - - const model = await Model.getByIdOrName({ - id: view?.fk_model_id || req.params.viewId, - }); - - 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 key = 'List'; - const requestObj: any = { - [key]: 1, - }; - - const data = ( - await nocoExecute( - requestObj, - { - [key]: async (args) => { - return await baseModel.getMmChildrenExcludedList( - { - colId: req.params.colId, - pid: req.params.rowId, - }, - args - ); - }, - }, - {}, - - { nested: { [key]: req.query } } - ) - )?.[key]; - - const count = await baseModel.getMmChildrenExcludedListCount( - { +export async function mmList(req: Request, res: Response) { + res.json( + await dataService.mmList({ + viewId: req.params.viewId, colId: req.params.colId, - pid: req.params.rowId, - }, - req.query + rowId: req.params.rowId, + query: req.query, + }) ); +} +export async function mmExcludedList(req: Request, res: Response) { res.json( - new PagedResponseImpl(data, { - count, - ...req.query, + await dataService.mmExcludedList({ + viewId: req.params.viewId, + colId: req.params.colId, + rowId: req.params.rowId, + query: req.query, }) ); } -export async function hmExcludedList(req: Request, res: Response, next) { - const view = await View.get(req.params.viewId); - - const model = await Model.getByIdOrName({ - id: view?.fk_model_id || req.params.viewId, - }); - - 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 key = 'List'; - const requestObj: any = { - [key]: 1, - }; - - const data = ( - await nocoExecute( - requestObj, - { - [key]: async (args) => { - return await baseModel.getHmChildrenExcludedList( - { - colId: req.params.colId, - pid: req.params.rowId, - }, - args - ); - }, - }, - {}, - - { nested: { [key]: req.query } } - ) - )?.[key]; - - const count = await baseModel.getHmChildrenExcludedListCount( - { +export async function hmExcludedList(req: Request, res: Response) { + res.json( + await dataService.hmExcludedList({ + viewId: req.params.viewId, colId: req.params.colId, - pid: req.params.rowId, - }, - req.query + rowId: req.params.rowId, + query: req.query, + }) ); +} +export async function btExcludedList(req: Request, res: Response) { res.json( - new PagedResponseImpl(data, { - count, - ...req.query, + await dataService.btExcludedList({ + viewId: req.params.viewId, + colId: req.params.colId, + rowId: req.params.rowId, + query: req.query, }) ); } -export async function btExcludedList(req: Request, res: Response, next) { - const view = await View.get(req.params.viewId); - - const model = await Model.getByIdOrName({ - id: view?.fk_model_id || req.params.viewId, - }); - - 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 key = 'List'; - const requestObj: any = { - [key]: 1, - }; - - const data = ( - await nocoExecute( - requestObj, - { - [key]: async (args) => { - return await baseModel.getBtChildrenExcludedList( - { - colId: req.params.colId, - cid: req.params.rowId, - }, - args - ); - }, - }, - {}, - - { nested: { [key]: req.query } } - ) - )?.[key]; - - const count = await baseModel.getBtChildrenExcludedListCount( - { +export async function hmList(req: Request, res: Response) { + res.json( + await dataService.hmList({ + viewId: req.params.viewId, colId: req.params.colId, - cid: req.params.rowId, - }, - req.query + rowId: req.params.rowId, + query: req.query, + }) ); +} +async function dataRead(req: Request, res: Response) { res.json( - new PagedResponseImpl(data, { - count, - ...req.query, + await dataService.dataReadByViewId({ + viewId: req.params.viewId, + rowId: req.params.rowId, + query: req.query, }) ); } -export async function hmList(req: Request, res: Response, next) { - const view = await View.get(req.params.viewId); - - const model = await Model.getByIdOrName({ - id: view?.fk_model_id || req.params.viewId, - }); - - 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 key = `${model.title}List`; - const requestObj: any = { - [key]: 1, - }; - - const data = ( - await nocoExecute( - requestObj, - { - [key]: async (args) => { - return await baseModel.hmList( - { - colId: req.params.colId, - id: req.params.rowId, - }, - args - ); - }, - }, - {}, - { nested: { [key]: req.query } } - ) - )?.[key]; - - const count = await baseModel.hmListCount({ - colId: req.params.colId, - id: req.params.rowId, - }); - +async function dataInsert(req: Request, res: Response) { res.json( - new PagedResponseImpl(data, { - totalRows: count, - } as any) + await dataService.dataInsertByViewId({ + viewId: req.params.viewId, + body: req.body, + cookie: req, + }) ); } -async function dataRead(req: Request, res: Response, next) { - try { - const model = await Model.getByIdOrName({ - id: req.params.viewId, - }); - 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, - dbDriver: NcConnectionMgrv2.get(base), - }); - - res.json( - await nocoExecute( - await getAst({ model, query: req.query }), - await baseModel.readByPk(req.params.rowId), - {}, - {} - ) - ); - } catch (e) { - console.log(e); - NcError.internalServerError( - 'Internal Server Error, check server log for more details' - ); - } -} - -async function dataInsert(req: Request, res: Response, next) { - try { - const model = await Model.getByIdOrName({ - id: req.params.viewId, - }); - 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, - dbDriver: NcConnectionMgrv2.get(base), - }); - - res.json(await baseModel.insert(req.body, null, req)); - } catch (e) { - console.log(e); - res.status(500).json({ msg: e.message }); - } -} - -// async function dataInsertNew(req: Request, res: Response) { -// const { model, view } = await getViewAndModelFromRequest(req); -// -// const base = await Base.get(model.base_id); -// -// const baseModel = await Model.getBaseModelSQL({ -// id: model.id, -// viewId: view?.id, -// dbDriver: NcConnectionMgrv2.get(base) -// }); -// -// res.json(await baseModel.insert(req.body, null, req)); -// } - -// async function dataUpdateNew(req: Request, res: Response) { -// const { model, view } = await getViewAndModelFromRequest(req); -// const base = await Base.get(model.base_id); -// -// const baseModel = await Model.getBaseModelSQL({ -// id: model.id, -// viewId: view.id, -// dbDriver: NcConnectionMgrv2.get(base) -// }); -// -// res.json(await baseModel.updateByPk(req.params.rowId, req.body, null, req)); -// } -async function dataUpdate(req: Request, res: Response, next) { - try { - const model = await Model.getByIdOrName({ - id: req.params.viewId, - }); - 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, - dbDriver: NcConnectionMgrv2.get(base), - }); - - res.json(await baseModel.updateByPk(req.params.rowId, req.body, null, req)); - } catch (e) { - console.log(e); - res.status(500).json({ msg: e.message }); - } +async function dataUpdate(req: Request, res: Response) { + res.json( + await dataService.dataUpdateByViewId({ + viewId: req.params.viewId, + rowId: req.params.rowId, + body: req.body, + cookie: req, + }) + ); } -// -// async function dataDeleteNew(req: Request, res: Response) { -// const { model, view } = await getViewAndModelFromRequest(req); -// const base = await Base.get(model.base_id); -// const baseModel = await Model.getBaseModelSQL({ -// id: model.id, -// viewId: view.id, -// dbDriver: NcConnectionMgrv2.get(base) -// }); -// -// res.json(await baseModel.delByPk(req.params.rowId, null, req)); -// } -async function dataDelete(req: Request, res: Response, next) { - try { - const model = await Model.getByIdOrName({ - id: req.params.viewId, - }); - 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, - dbDriver: NcConnectionMgrv2.get(base), - }); - - res.json(await baseModel.delByPk(req.params.rowId, null, req)); - } catch (e) { - console.log(e); - res.status(500).json({ msg: e.message }); - } +async function dataDelete(req: Request, res: Response) { + res.json( + await dataService.dataDeleteByViewId({ + viewId: req.params.viewId, + rowId: req.params.rowId, + cookie: req, + }) + ); } -async function getDataList(model, view: View, req) { - const base = await Base.get(model.base_id); - - const baseModel = await Model.getBaseModelSQL({ - id: model.id, - viewId: view?.id, - dbDriver: NcConnectionMgrv2.get(base), - }); - - const requestObj = await getAst({ query: req.query, model, view }); - - const listArgs: any = { ...req.query }; - try { - listArgs.filterArr = JSON.parse(listArgs.filterArrJson); - } catch (e) {} - try { - listArgs.sortArr = JSON.parse(listArgs.sortArrJson); - } catch (e) {} - - let data = []; - let count = 0; - try { - data = await nocoExecute( - requestObj, - await baseModel.list(listArgs), - {}, - listArgs - ); - count = await baseModel.count(listArgs); - } catch (e) { - // show empty result instead of throwing error here - // e.g. search some text in a numeric field - console.log(e); - NcError.internalServerError( - 'Internal Server Error, check server log for more details' - ); - } - - return new PagedResponseImpl(data, { - count, - ...req.query, - }); -} -//@ts-ignore async function relationDataDelete(req, res) { - const view = await View.get(req.params.viewId); - - const model = await Model.getByIdOrName({ - id: view?.fk_model_id || req.params.viewId, - }); - - 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), - }); - - await baseModel.removeChild({ + await dataService.relationDataDelete({ + viewId: req.params.viewId, colId: req.params.colId, childId: req.params.childId, rowId: req.params.rowId, @@ -508,23 +122,8 @@ async function relationDataDelete(req, res) { //@ts-ignore async function relationDataAdd(req, res) { - const view = await View.get(req.params.viewId); - - const model = await Model.getByIdOrName({ - id: view?.fk_model_id || req.params.viewId, - }); - - 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), - }); - - await baseModel.addChild({ + await dataService.relationDataAdd({ + viewId: req.params.viewId, colId: req.params.colId, childId: req.params.childId, rowId: req.params.rowId, @@ -536,25 +135,6 @@ async function relationDataAdd(req, res) { const router = Router({ mergeParams: true }); -// router.get('/data/:orgs/:projectName/:tableName',apiMetrics,ncMetaAclMw(dataListNew)); -// router.get( -// '/data/:orgs/:projectName/:tableName/views/:viewName', -// ncMetaAclMw(dataListNew) -// ); -// -// router.post( -// '/data/:orgs/:projectName/:tableName/views/:viewName', -// ncMetaAclMw(dataInsertNew) -// ); -// router.patch( -// '/data/:orgs/:projectName/:tableName/views/:viewName/:rowId', -// ncMetaAclMw(dataUpdateNew) -// ); -// router.delete( -// '/data/:orgs/:projectName/:tableName/views/:viewName/:rowId', -// ncMetaAclMw(dataDeleteNew) -// ); - router.get('/data/:viewId/', apiMetrics, ncMetaAclMw(dataList, 'dataList')); router.post( '/data/:viewId/', diff --git a/packages/nocodb/src/lib/controllers/publc/index.ts b/packages/nocodb/src/lib/controllers/public/index.ts similarity index 100% rename from packages/nocodb/src/lib/controllers/publc/index.ts rename to packages/nocodb/src/lib/controllers/public/index.ts diff --git a/packages/nocodb/src/lib/controllers/publc/publicDataController.ts b/packages/nocodb/src/lib/controllers/public/publicDataController.ts similarity index 100% rename from packages/nocodb/src/lib/controllers/publc/publicDataController.ts rename to packages/nocodb/src/lib/controllers/public/publicDataController.ts diff --git a/packages/nocodb/src/lib/controllers/publc/publicDataExportController.ts b/packages/nocodb/src/lib/controllers/public/publicDataExportController.ts similarity index 100% rename from packages/nocodb/src/lib/controllers/publc/publicDataExportController.ts rename to packages/nocodb/src/lib/controllers/public/publicDataExportController.ts diff --git a/packages/nocodb/src/lib/controllers/publc/publicMetaController.ts b/packages/nocodb/src/lib/controllers/public/publicMetaController.ts similarity index 100% rename from packages/nocodb/src/lib/controllers/publc/publicMetaController.ts rename to packages/nocodb/src/lib/controllers/public/publicMetaController.ts diff --git a/packages/nocodb/src/lib/meta/api/index.ts b/packages/nocodb/src/lib/meta/api/index.ts index c96fe7d91d..390d251bad 100644 --- a/packages/nocodb/src/lib/meta/api/index.ts +++ b/packages/nocodb/src/lib/meta/api/index.ts @@ -46,7 +46,7 @@ import { publicDataController, publicDataExportController, publicMetaController, -} from '../../controllers/publc'; +} from '../../controllers/public'; import { Server, Socket } from 'socket.io'; import passport from 'passport'; diff --git a/packages/nocodb/src/lib/services/dataService/export.ts b/packages/nocodb/src/lib/services/dataService/export.ts index f74e5299f9..53afe1ff85 100644 --- a/packages/nocodb/src/lib/services/dataService/export.ts +++ b/packages/nocodb/src/lib/services/dataService/export.ts @@ -1,22 +1,12 @@ -import { Request, Response, Router } from 'express'; -import { isSystemColumn } from 'nocodb-sdk'; -import * as XLSX from 'xlsx'; -import getAst from '../../db/sql-data-mapper/lib/sql/helpers/getAst'; -import ncMetaAclMw from '../../helpers/ncMetaAclMw'; import { - extractXlsxData, - serializeCellValue, -} from '../../meta/api/dataApis/helpers'; -import { - extractCsvData, getViewAndModelByAliasOrId, - getViewAndModelFromRequestByAliasOrId, PathParams, } from './helpers'; -import apiMetrics from '../../helpers/apiMetrics'; -import View from '../../../models/View'; +import { View } from '../../models'; + -async function excelDataExport( +// Todo: bring logic from controller +export async function excelDataExport( param: PathParams & { query: any; } @@ -26,39 +16,39 @@ async function excelDataExport( if (!targetView) { targetView = await View.getDefaultView(model.id); } - const { offset, elapsed, data } = await extractXlsxData({ - view: targetView, - query: req.query, - }); - const wb = XLSX.utils.book_new(); - XLSX.utils.book_append_sheet(wb, data, targetView.title); - const buf = XLSX.write(wb, { type: 'base64', bookType: 'xlsx' }); - res.set({ - 'Access-Control-Expose-Headers': 'nc-export-offset', - 'nc-export-offset': offset, - 'nc-export-elapsed-time': elapsed, - 'Content-Disposition': `attachment; filename="${encodeURI( - targetView.title - )}-export.xlsx"`, - }); - res.end(buf); + // const { offset, elapsed, data } = await extractXlsxData({ + // view: targetView, + // query: param.query, + // }); + // const wb = XLSX.utils.book_new(); + // XLSX.utils.book_append_sheet(wb, data, targetView.title); + // const buf = XLSX.write(wb, { type: 'base64', bookType: 'xlsx' }); + // res.set({ + // 'Access-Control-Expose-Headers': 'nc-export-offset', + // 'nc-export-offset': offset, + // 'nc-export-elapsed-time': elapsed, + // 'Content-Disposition': `attachment; filename="${encodeURI( + // targetView.title + // )}-export.xlsx"`, + // }); + // res.end(buf); } -async function csvDataExport(req: Request, res: Response) { - const { model, view } = await getViewAndModelFromRequestByAliasOrId(req); - let targetView = view; - if (!targetView) { - targetView = await View.getDefaultView(model.id); - } - const { offset, elapsed, data } = await extractCsvData(targetView, req); - - res.set({ - 'Access-Control-Expose-Headers': 'nc-export-offset', - 'nc-export-offset': offset, - 'nc-export-elapsed-time': elapsed, - 'Content-Disposition': `attachment; filename="${encodeURI( - targetView.title - )}-export.csv"`, - }); - res.send(data); -} +// async function csvDataExport(req: Request, res: Response) { +// const { model, view } = await getViewAndModelFromRequestByAliasOrId(req); +// let targetView = view; +// if (!targetView) { +// targetView = await View.getDefaultView(model.id); +// } +// const { offset, elapsed, data } = await extractCsvData(targetView, req); +// +// res.set({ +// 'Access-Control-Expose-Headers': 'nc-export-offset', +// 'nc-export-offset': offset, +// 'nc-export-elapsed-time': elapsed, +// 'Content-Disposition': `attachment; filename="${encodeURI( +// targetView.title +// )}-export.csv"`, +// }); +// res.send(data); +// }