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. 19
      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

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

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

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

@ -2467,35 +2467,10 @@ export class Api<
*
* @tags DB table row
* @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}
* @originalName csvExport
* @duplicate
* @response `200` `any` OK
*/
csvExport2: (
csvExport: (
orgs: string,
projectName: string,
tableName: string,
@ -2852,6 +2827,29 @@ export class Api<
method: 'DELETE',
...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 = {
/**

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

@ -4,22 +4,11 @@ import {
extractCsvData,
getViewAndModelFromRequestByAliasOrId
} from './helpers';
import papaparse from 'papaparse';
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 data = papaparse.unparse(
{
fields: model.columns.map(c => c.title),
data: csvRows
},
{
escapeFormulae: true
}
);
const { offset, elapsed, data } = await extractCsvData(view, req);
res.set({
'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 LinkToAnotherRecordColumn from '../../../../noco-models/LinkToAnotherRecordColumn';
import papaparse from 'papaparse';
export async function getViewAndModelFromRequestByAliasOrId(
req:
| Request<{ projectName: string; tableName: string; viewName?: string }>
@ -38,10 +39,22 @@ export async function getViewAndModelFromRequestByAliasOrId(
return { model, view };
}
export async function extractCsvData(model: Model, view: View, req: Request) {
const base = await Base.get(model.base_id);
export async function extractCsvData(view: View, req: Request) {
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({
id: model.id,
id: view.model.id,
viewId: view?.id,
dbDriver: NcConnectionMgrv2.get(base)
});
@ -86,7 +99,18 @@ export async function extractCsvData(model: Model, view: View, req: Request) {
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({

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

@ -3,11 +3,13 @@ import oldDataApis from './oldDataApis';
import dataAliasApis from './dataAliasApis';
import bulkDataAliasApis from './bulkDataAliasApis';
import dataAliasNestedApis from './dataAliasNestedApis';
import dataAliasExportApis from './dataAliasExportApis';
export {
dataApis,
oldDataApis,
dataAliasApis,
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 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 { 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 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
}
);
const { offset, elapsed, data } = await extractCsvData(view, req);
res.set({
'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 {
bulkDataAliasApis,
dataAliasApis,
dataAliasExportApis,
dataAliasNestedApis,
dataApis,
oldDataApis
@ -54,6 +55,7 @@ export default function(router: Router, server) {
router.use(bulkDataAliasApis);
router.use(dataAliasApis);
router.use(dataAliasNestedApis);
router.use(dataAliasExportApis);
router.use(oldDataApis);
router.use(sortApis);
router.use(filterApis);

4
scripts/sdk/swagger.json

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

Loading…
Cancel
Save