Browse Source

feat: export data apis

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5444/head
Pranav C 2 years ago
parent
commit
a65ee2d9a1
  1. 58
      packages/nocodb-nest/src/modules/datas/data-alias-export/data-alias-export.controller.ts

58
packages/nocodb-nest/src/modules/datas/data-alias-export/data-alias-export.controller.ts

@ -1,4 +1,58 @@
import { Controller } from '@nestjs/common'; import { Controller, Get, Request, Response } from '@nestjs/common';
import { getViewAndModelFromRequestByAliasOrId } from '../../../../../nocodb/src/lib/controllers/dbData/helpers';
import { Acl } from '../../../middlewares/extract-project-id/extract-project-id.middleware';
import { View } from '../../../models';
import { extractCsvData, extractXlsxData } from '../helpers';
import * as XLSX from 'xlsx';
@Controller('data-alias-export') @Controller('data-alias-export')
export class DataAliasExportController {} export class DataAliasExportController {
@Get([
'/api/v1/db/data/:orgs/:projectName/:tableName/export/excel',
'/api/v1/db/data/:orgs/:projectName/:tableName/views/:viewName/export/excel',
])
@Acl('exportExcel')
async excelDataExport(@Request() req, @Response() res) {
const { model, view } = await getViewAndModelFromRequestByAliasOrId(req);
let targetView = view;
if (!targetView) {
targetView = await View.getDefaultView(model.id);
}
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' });
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);
}
@Get([
'/api/v1/db/data/:orgs/:projectName/:tableName/views/:viewName/export/csv',
'/api/v1/db/data/:orgs/:projectName/:tableName/export/csv',
])
@Acl('exportCsv')
async csvDataExport(@Request() req, @Response() res) {
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);
}
}

Loading…
Cancel
Save