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 getViewAndModelFromRequestByAliasOrId
} from './helpers'; } from './helpers';
import apiMetrics from '../../helpers/apiMetrics'; import apiMetrics from '../../helpers/apiMetrics';
import View from '../../../models/View';
async function csvDataExport(req: Request, res: Response) { async function csvDataExport(req: Request, res: Response) {
const { view } = await getViewAndModelFromRequestByAliasOrId(req); const { model, view } = await getViewAndModelFromRequestByAliasOrId(req);
let targetView = view;
const { offset, elapsed, data } = await extractCsvData(view, req); if (!targetView) {
targetView = await View.getDefaultView(model.id);
}
const { offset, elapsed, data } = await extractCsvData(targetView, req);
res.set({ res.set({
'Access-Control-Expose-Headers': 'nc-export-offset', 'Access-Control-Expose-Headers': 'nc-export-offset',
'nc-export-offset': offset, 'nc-export-offset': offset,
'nc-export-elapsed-time': elapsed, 'nc-export-elapsed-time': elapsed,
'Content-Disposition': `attachment; filename="${encodeURI( 'Content-Disposition': `attachment; filename="${encodeURI(
view.title targetView.title
)}-export.csv"` )}-export.csv"`
}); });
res.send(data); 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); 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) { public static async list(modelId: string, ncMeta = Noco.ncMeta) {
let viewsList = await NocoCache.getList(CacheScope.VIEW, [modelId]); let viewsList = await NocoCache.getList(CacheScope.VIEW, [modelId]);
if (!viewsList.length) { if (!viewsList.length) {
@ -666,6 +692,9 @@ export default class View implements ViewType {
if (o) { if (o) {
// update data // update data
o = { ...o, ...updateObj }; o = { ...o, ...updateObj };
if (o.is_default) {
await NocoCache.set(`${CacheScope.VIEW}:${o.fk_model_id}:default`, o);
}
// set cache // set cache
await NocoCache.set(key, o); await NocoCache.set(key, o);
} }

Loading…
Cancel
Save