Browse Source

fix: use default view if no view is specified

pull/2441/head
Wing-Kam Wong 2 years ago
parent
commit
bc1362d5ed
  1. 12
      packages/nocodb/src/lib/meta/api/dataApis/dataAliasExportApis.ts
  2. 29
      packages/nocodb/src/lib/models/View.ts

12
packages/nocodb/src/lib/meta/api/dataApis/dataAliasExportApis.ts

@ -5,18 +5,22 @@ import {
getViewAndModelFromRequestByAliasOrId
} from './helpers';
import apiMetrics from '../../helpers/apiMetrics';
import View from '../../../models/View';
async function csvDataExport(req: Request, res: Response) {
const { view } = await getViewAndModelFromRequestByAliasOrId(req);
const { offset, elapsed, data } = await extractCsvData(view, req);
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(
view.title
targetView.title
)}-export.csv"`
});
res.send(data);

29
packages/nocodb/src/lib/models/View.ts

@ -157,6 +157,32 @@ export default class View implements ViewType {
return viewId && this.get(viewId?.id || viewId);
}
public static async getDefaultView(
fk_model_id: string,
ncMeta = Noco.ncMeta
) {
let view =
fk_model_id &&
(await NocoCache.get(
`${CacheScope.VIEW}:${fk_model_id}:default`,
CacheGetType.TYPE_OBJECT
));
if (!view) {
view = await ncMeta.metaGet2(
null,
null,
MetaTable.VIEWS,
{
fk_model_id,
is_default: 1
},
null
);
await NocoCache.set(`${CacheScope.VIEW}:${fk_model_id}:default`, view);
}
return view && new View(view);
}
public static async list(modelId: string, ncMeta = Noco.ncMeta) {
let viewsList = await NocoCache.getList(CacheScope.VIEW, [modelId]);
if (!viewsList.length) {
@ -666,6 +692,9 @@ export default class View implements ViewType {
if (o) {
// update data
o = { ...o, ...updateObj };
if (o.is_default) {
await NocoCache.set(`${CacheScope.VIEW}:${o.fk_model_id}:default`, o);
}
// set cache
await NocoCache.set(key, o);
}

Loading…
Cancel
Save