Browse Source

refactor: export csv api corrections

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/1668/head
Pranav C 3 years ago
parent
commit
f87db007d4
  1. 9
      packages/nc-gui/components/project/spreadsheet/components/moreActions.vue
  2. 50
      packages/nocodb-sdk/src/lib/Api.ts
  3. 15
      packages/nocodb/src/lib/noco/meta/api/dataApis/dataAliasExportApis.ts
  4. 32
      packages/nocodb/src/lib/noco/meta/api/dataApis/helpers.ts
  5. 4
      packages/nocodb/src/lib/noco/meta/api/dataApis/index.ts
  6. 30
      packages/nocodb/src/lib/noco/meta/api/exportApis.ts
  7. 2
      packages/nocodb/src/lib/noco/meta/api/index.ts
  8. 4
      scripts/sdk/swagger.json

9
packages/nc-gui/components/project/spreadsheet/components/moreActions.vue

@ -244,7 +244,12 @@ export default {
} }
}) })
} else { } else {
res = await this.$api.data.csvExport(this.selectedView.id, ExportTypes.CSV, { res = await this.$api.dbViewRow.export(
'noco',
this.projectName,
this.meta.title,
this.selectedView.title,
ExportTypes.CSV, {
responseType: 'blob', responseType: 'blob',
query: { query: {
offset offset
@ -289,7 +294,7 @@ export default {
input = '1' input = '1'
} }
} else if (v.uidt === UITypes.Number) { } else if (v.uidt === UITypes.Number) {
if (input == "") input = null if (input == '') { input = null }
} }
res[col.destCn] = input res[col.destCn] = input
} }

50
packages/nocodb-sdk/src/lib/Api.ts

@ -2467,35 +2467,10 @@ export class Api<
* *
* @tags DB table row * @tags DB table row
* @name CsvExport * @name CsvExport
* @request GET:/api/v1/db/data/{orgs}/{projectName}/{tableName}/views/{viewName}/export/{type}
* @response `200` `any` OK
*/
csvExport: (
orgs: string,
projectName: string,
tableName: string,
viewName: string,
type: 'csv' | 'excel',
params: RequestParams = {}
) =>
this.request<any, any>({
path: `/api/v1/db/data/${orgs}/${projectName}/${tableName}/views/${viewName}/export/${type}`,
method: 'GET',
wrapped: true,
...params,
}),
/**
* @description CSV or Excel export
*
* @tags DB table row
* @name CsvExport2
* @request GET:/api/v1/db/data/{orgs}/{projectName}/{tableName}/export/{type} * @request GET:/api/v1/db/data/{orgs}/{projectName}/{tableName}/export/{type}
* @originalName csvExport
* @duplicate
* @response `200` `any` OK * @response `200` `any` OK
*/ */
csvExport2: ( csvExport: (
orgs: string, orgs: string,
projectName: string, projectName: string,
tableName: string, tableName: string,
@ -2852,6 +2827,29 @@ export class Api<
method: 'DELETE', method: 'DELETE',
...params, ...params,
}), }),
/**
* @description CSV or Excel export
*
* @tags DB view row
* @name Export
* @request GET:/api/v1/db/data/{orgs}/{projectName}/{tableName}/views/{viewName}/export/{type}
* @response `200` `any` OK
*/
export: (
orgs: string,
projectName: string,
tableName: string,
viewName: string,
type: 'csv' | 'excel',
params: RequestParams = {}
) =>
this.request<any, any>({
path: `/api/v1/db/data/${orgs}/${projectName}/${tableName}/views/${viewName}/export/${type}`,
method: 'GET',
wrapped: true,
...params,
}),
}; };
public = { public = {
/** /**

15
packages/nocodb/src/lib/noco/meta/api/dataApis/dataAliasExportApis.ts

@ -4,22 +4,11 @@ import {
extractCsvData, extractCsvData,
getViewAndModelFromRequestByAliasOrId getViewAndModelFromRequestByAliasOrId
} from './helpers'; } from './helpers';
import papaparse from 'papaparse';
async function csvDataExport(req: Request, res: Response) { async function csvDataExport(req: Request, res: Response) {
const { model, view } = await getViewAndModelFromRequestByAliasOrId(req); const { view } = await getViewAndModelFromRequestByAliasOrId(req);
const { offset, csvRows, elapsed } = await extractCsvData(model, view, req); const { offset, elapsed, data } = await extractCsvData(view, req);
const data = papaparse.unparse(
{
fields: model.columns.map(c => c.title),
data: csvRows
},
{
escapeFormulae: true
}
);
res.set({ res.set({
'Access-Control-Expose-Headers': 'nc-export-offset', 'Access-Control-Expose-Headers': 'nc-export-offset',

32
packages/nocodb/src/lib/noco/meta/api/dataApis/helpers.ts

@ -12,6 +12,7 @@ import Column from '../../../../noco-models/Column';
import LookupColumn from '../../../../noco-models/LookupColumn'; import LookupColumn from '../../../../noco-models/LookupColumn';
import LinkToAnotherRecordColumn from '../../../../noco-models/LinkToAnotherRecordColumn'; import LinkToAnotherRecordColumn from '../../../../noco-models/LinkToAnotherRecordColumn';
import papaparse from 'papaparse';
export async function getViewAndModelFromRequestByAliasOrId( export async function getViewAndModelFromRequestByAliasOrId(
req: req:
| Request<{ projectName: string; tableName: string; viewName?: string }> | Request<{ projectName: string; tableName: string; viewName?: string }>
@ -38,10 +39,22 @@ export async function getViewAndModelFromRequestByAliasOrId(
return { model, view }; return { model, view };
} }
export async function extractCsvData(model: Model, view: View, req: Request) { export async function extractCsvData(view: View, req: Request) {
const base = await Base.get(model.base_id); const base = await Base.get(view.base_id);
await view.getModelWithInfo();
await view.getColumns();
view.model.columns = view.columns
.filter(c => c.show)
.map(
c =>
new Column({ ...c, ...view.model.columnsById[c.fk_column_id] } as any)
)
.filter(column => !isSystemColumn(column) || view.show_system_fields);
const baseModel = await Model.getBaseModelSQL({ const baseModel = await Model.getBaseModelSQL({
id: model.id, id: view.model.id,
viewId: view?.id, viewId: view?.id,
dbDriver: NcConnectionMgrv2.get(base) dbDriver: NcConnectionMgrv2.get(base)
}); });
@ -86,7 +99,18 @@ export async function extractCsvData(model: Model, view: View, req: Request) {
csvRows.push(csvRow); csvRows.push(csvRow);
} }
} }
return { offset, csvRows, elapsed };
const data = papaparse.unparse(
{
fields: view.model.columns.map(c => c.title),
data: csvRows
},
{
escapeFormulae: true
}
);
return { offset, csvRows, elapsed, data };
} }
export async function serializeCellValue({ export async function serializeCellValue({

4
packages/nocodb/src/lib/noco/meta/api/dataApis/index.ts

@ -3,11 +3,13 @@ import oldDataApis from './oldDataApis';
import dataAliasApis from './dataAliasApis'; import dataAliasApis from './dataAliasApis';
import bulkDataAliasApis from './bulkDataAliasApis'; import bulkDataAliasApis from './bulkDataAliasApis';
import dataAliasNestedApis from './dataAliasNestedApis'; import dataAliasNestedApis from './dataAliasNestedApis';
import dataAliasExportApis from './dataAliasExportApis';
export { export {
dataApis, dataApis,
oldDataApis, oldDataApis,
dataAliasApis, dataAliasApis,
bulkDataAliasApis, bulkDataAliasApis,
dataAliasNestedApis dataAliasNestedApis,
dataAliasExportApis
}; };

30
packages/nocodb/src/lib/noco/meta/api/exportApis.ts

@ -1,37 +1,11 @@
import { Request, Response, Router } from 'express'; import { Request, Response, Router } from 'express';
import View from '../../../noco-models/View'; import View from '../../../noco-models/View';
import papaparse from 'papaparse';
import { isSystemColumn } from 'nocodb-sdk';
import Column from '../../../noco-models/Column';
import ncMetaAclMw from '../helpers/ncMetaAclMw'; import ncMetaAclMw from '../helpers/ncMetaAclMw';
import { extractCsvData } from './dataApis/helpers'; import { extractCsvData } from './dataApis/helpers';
async function exportCsv(req: Request, res: Response, next) { async function exportCsv(req: Request, res: Response) {
const view = await View.get(req.params.viewId); const view = await View.get(req.params.viewId);
const { offset, elapsed, data } = await extractCsvData(view, req);
const model = await view.getModelWithInfo();
await view.getColumns();
view.model.columns = view.columns
.filter(c => c.show)
.map(
c =>
new Column({ ...c, ...view.model.columnsById[c.fk_column_id] } as any)
)
.filter(column => !isSystemColumn(column) || view.show_system_fields);
if (!model) return next(new Error('Table not found'));
const { offset, csvRows, elapsed } = await extractCsvData(model, view, req);
const data = papaparse.unparse(
{
fields: model.columns.map(c => c.title),
data: csvRows
},
{
escapeFormulae: true
}
);
res.set({ res.set({
'Access-Control-Expose-Headers': 'nc-export-offset', 'Access-Control-Expose-Headers': 'nc-export-offset',

2
packages/nocodb/src/lib/noco/meta/api/index.ts

@ -30,6 +30,7 @@ import hookFilterApis from './hookFilterApis';
import { import {
bulkDataAliasApis, bulkDataAliasApis,
dataAliasApis, dataAliasApis,
dataAliasExportApis,
dataAliasNestedApis, dataAliasNestedApis,
dataApis, dataApis,
oldDataApis oldDataApis
@ -54,6 +55,7 @@ export default function(router: Router, server) {
router.use(bulkDataAliasApis); router.use(bulkDataAliasApis);
router.use(dataAliasApis); router.use(dataAliasApis);
router.use(dataAliasNestedApis); router.use(dataAliasNestedApis);
router.use(dataAliasExportApis);
router.use(oldDataApis); router.use(oldDataApis);
router.use(sortApis); router.use(sortApis);
router.use(filterApis); router.use(filterApis);

4
scripts/sdk/swagger.json

@ -2957,10 +2957,10 @@
], ],
"get": { "get": {
"summary": "", "summary": "",
"operationId": "db-table-row-csv-export", "operationId": "db-view-row-export",
"description": "CSV or Excel export", "description": "CSV or Excel export",
"tags": [ "tags": [
"DB table row" "DB view row"
], ],
"wrapped": true, "wrapped": true,
"responses": { "responses": {

Loading…
Cancel
Save