Browse Source

fix(api): public nested data api correction

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/3414/head
Pranav C 2 years ago
parent
commit
b3b6a572bc
  1. 15
      packages/nocodb/src/lib/meta/api/dataApis/dataAliasNestedApis.ts
  2. 15
      packages/nocodb/src/lib/meta/api/dataApis/helpers.ts
  3. 17
      packages/nocodb/src/lib/meta/api/publicApis/publicDataApis.ts
  4. 22
      packages/nocodb/src/lib/meta/helpers/PagedResponse.ts
  5. 48
      packages/nocodb/src/lib/models/Column.ts

15
packages/nocodb/src/lib/meta/api/dataApis/dataAliasNestedApis.ts

@ -4,7 +4,7 @@ import Base from '../../../models/Base';
import NcConnectionMgrv2 from '../../../utils/common/NcConnectionMgrv2'; import NcConnectionMgrv2 from '../../../utils/common/NcConnectionMgrv2';
import { PagedResponseImpl } from '../../helpers/PagedResponse'; import { PagedResponseImpl } from '../../helpers/PagedResponse';
import ncMetaAclMw from '../../helpers/ncMetaAclMw'; import ncMetaAclMw from '../../helpers/ncMetaAclMw';
import { getViewAndModelFromRequestByAliasOrId } from './helpers'; import { getColumnByIdOrName, getViewAndModelFromRequestByAliasOrId } from './helpers'
import { NcError } from '../../helpers/catchError'; import { NcError } from '../../helpers/catchError';
import apiMetrics from '../../helpers/apiMetrics'; import apiMetrics from '../../helpers/apiMetrics';
@ -240,19 +240,6 @@ async function relationDataAdd(req, res) {
res.json({ msg: 'success' }); res.json({ msg: 'success' });
} }
async function getColumnByIdOrName(columnNameOrId: string, model: Model) {
const column = (await model.getColumns()).find(
(c) =>
c.title === columnNameOrId ||
c.id === columnNameOrId ||
c.column_name === columnNameOrId
);
if (!column)
NcError.notFound(`Column with id/name '${columnNameOrId}' is not found`);
return column;
}
const router = Router({ mergeParams: true }); const router = Router({ mergeParams: true });

15
packages/nocodb/src/lib/meta/api/dataApis/helpers.ts

@ -229,3 +229,18 @@ export async function serializeCellValue({
return value; return value;
} }
} }
export async function getColumnByIdOrName(columnNameOrId: string, model: Model) {
const column = (await model.getColumns()).find(
(c) =>
c.title === columnNameOrId ||
c.id === columnNameOrId ||
c.column_name === columnNameOrId
);
if (!column)
NcError.notFound(`Column with id/name '${columnNameOrId}' is not found`);
return column;
}

17
packages/nocodb/src/lib/meta/api/publicApis/publicDataApis.ts

@ -17,6 +17,7 @@ import { mimeIcons } from '../../../utils/mimeTypes';
import slash from 'slash'; import slash from 'slash';
import { sanitizeUrlPath } from '../attachmentApis'; import { sanitizeUrlPath } from '../attachmentApis';
import getAst from '../../../db/sql-data-mapper/lib/sql/helpers/getAst'; import getAst from '../../../db/sql-data-mapper/lib/sql/helpers/getAst';
import { getColumnByIdOrName } from '../dataApis/helpers';
export async function dataList(req: Request, res: Response) { export async function dataList(req: Request, res: Response) {
try { try {
@ -219,7 +220,10 @@ export async function publicMmList(req: Request, res: Response) {
NcError.forbidden(ErrorMessages.INVALID_SHARED_VIEW_PASSWORD); NcError.forbidden(ErrorMessages.INVALID_SHARED_VIEW_PASSWORD);
} }
const column = await Column.get({ colId: req.params.colId }); const column = await getColumnByIdOrName(
req.params.colId,
await view.getModel()
);
if (column.fk_model_id !== view.fk_model_id) if (column.fk_model_id !== view.fk_model_id)
NcError.badRequest("Column doesn't belongs to the model"); NcError.badRequest("Column doesn't belongs to the model");
@ -275,7 +279,10 @@ export async function publicHmList(req: Request, res: Response) {
NcError.forbidden(ErrorMessages.INVALID_SHARED_VIEW_PASSWORD); NcError.forbidden(ErrorMessages.INVALID_SHARED_VIEW_PASSWORD);
} }
const column = await Column.get({ colId: req.params.colId }); const column = await getColumnByIdOrName(
req.params.colId,
await view.getModel()
);
if (column.fk_model_id !== view.fk_model_id) if (column.fk_model_id !== view.fk_model_id)
NcError.badRequest("Column doesn't belongs to the model"); NcError.badRequest("Column doesn't belongs to the model");
@ -317,11 +324,7 @@ export async function publicHmList(req: Request, res: Response) {
id: req.params.rowId, id: req.params.rowId,
}); });
res.json( res.json(new PagedResponseImpl(data, { ...req.query, count }));
new PagedResponseImpl(data, {
totalRows: count,
} as any)
);
} }
const router = Router({ mergeParams: true }); const router = Router({ mergeParams: true });

22
packages/nocodb/src/lib/meta/helpers/PagedResponse.ts

@ -5,21 +5,31 @@ const config: any = {
limitMin: Math.max(+process.env.DB_QUERY_LIMIT_MIN || 1, 1), limitMin: Math.max(+process.env.DB_QUERY_LIMIT_MIN || 1, 1),
limitMax: Math.max(+process.env.DB_QUERY_LIMIT_MAX || 1000, 1), limitMax: Math.max(+process.env.DB_QUERY_LIMIT_MAX || 1000, 1),
}; };
export class PagedResponseImpl<T> { export class PagedResponseImpl<T> {
constructor( constructor(
list: T[], list: T[],
args: { limit?: number; offset?: number; count?: number, l?: number, o?: number } = {} args: {
limit?: number;
offset?: number;
count?: number | string;
l?: number;
o?: number;
} = {}
) { ) {
const limit = Math.max( const limit = Math.max(
Math.min( Math.min(args.limit || args.l || config.limitDefault, config.limitMax),
args.limit || args.l || config.limitDefault,
config.limitMax
),
config.limitMin config.limitMin
); );
const offset = Math.max(+(args.offset || args.o) || 0, 0); const offset = Math.max(+(args.offset || args.o) || 0, 0);
const count = args.count ?? null;
let count = args.count ?? null;
if (count !== null) count = +count;
this.list = list; this.list = list;
if (count !== null) { if (count !== null) {
this.pageInfo = { totalRows: +count }; this.pageInfo = { totalRows: +count };
this.pageInfo.page = offset ? offset / limit + 1 : 1; this.pageInfo.page = offset ? offset / limit + 1 : 1;

48
packages/nocodb/src/lib/models/Column.ts

@ -232,20 +232,33 @@ export default class Column<T = any> implements ColumnType {
} }
case UITypes.MultiSelect: { case UITypes.MultiSelect: {
if (!column.colOptions?.options) { if (!column.colOptions?.options) {
const selectColors = [ '#cfdffe', '#d0f1fd', '#c2f5e8', '#ffdaf6', '#ffdce5', '#fee2d5', '#ffeab6', '#d1f7c4', '#ede2fe', '#eeeeee', ]; const selectColors = [
for (const [i, option] of column.dtxp?.split(',').entries() || [].entries()) { '#cfdffe',
'#d0f1fd',
'#c2f5e8',
'#ffdaf6',
'#ffdce5',
'#fee2d5',
'#ffeab6',
'#d1f7c4',
'#ede2fe',
'#eeeeee',
];
for (const [i, option] of column.dtxp?.split(',').entries() ||
[].entries()) {
await SelectOption.insert( await SelectOption.insert(
{ {
fk_column_id: colId, fk_column_id: colId,
title: option.replace(/^'/, '').replace(/'$/, ''), title: option.replace(/^'/, '').replace(/'$/, ''),
order: i + 1, order: i + 1,
color: selectColors[i % selectColors.length] color: selectColors[i % selectColors.length],
}, },
ncMeta ncMeta
); );
} }
} else { } else {
for (const [i, option] of column.colOptions.options.entries() || [].entries()) { for (const [i, option] of column.colOptions.options.entries() ||
[].entries()) {
// Trim end of enum/set // Trim end of enum/set
if (column.dt === 'enum' || column.dt === 'set') { if (column.dt === 'enum' || column.dt === 'set') {
option.title = option.title.trimEnd(); option.title = option.title.trimEnd();
@ -254,7 +267,7 @@ export default class Column<T = any> implements ColumnType {
{ {
...option, ...option,
fk_column_id: colId, fk_column_id: colId,
order: i + 1 order: i + 1,
}, },
ncMeta ncMeta
); );
@ -264,20 +277,33 @@ export default class Column<T = any> implements ColumnType {
} }
case UITypes.SingleSelect: { case UITypes.SingleSelect: {
if (!column.colOptions?.options) { if (!column.colOptions?.options) {
const selectColors = [ '#cfdffe', '#d0f1fd', '#c2f5e8', '#ffdaf6', '#ffdce5', '#fee2d5', '#ffeab6', '#d1f7c4', '#ede2fe', '#eeeeee', ]; const selectColors = [
for (const [i, option] of column.dtxp?.split(',').entries() || [].entries()) { '#cfdffe',
'#d0f1fd',
'#c2f5e8',
'#ffdaf6',
'#ffdce5',
'#fee2d5',
'#ffeab6',
'#d1f7c4',
'#ede2fe',
'#eeeeee',
];
for (const [i, option] of column.dtxp?.split(',').entries() ||
[].entries()) {
await SelectOption.insert( await SelectOption.insert(
{ {
fk_column_id: colId, fk_column_id: colId,
title: option.replace(/^'/, '').replace(/'$/, ''), title: option.replace(/^'/, '').replace(/'$/, ''),
order: i + 1, order: i + 1,
color: selectColors[i % selectColors.length] color: selectColors[i % selectColors.length],
}, },
ncMeta ncMeta
); );
} }
} else { } else {
for (const [i, option] of column.colOptions.options.entries() || [].entries()) { for (const [i, option] of column.colOptions.options.entries() ||
[].entries()) {
// Trim end of enum/set // Trim end of enum/set
if (column.dt === 'enum' || column.dt === 'set') { if (column.dt === 'enum' || column.dt === 'set') {
option.title = option.title.trimEnd(); option.title = option.title.trimEnd();
@ -286,7 +312,7 @@ export default class Column<T = any> implements ColumnType {
{ {
...option, ...option,
fk_column_id: colId, fk_column_id: colId,
order: i + 1 order: i + 1,
}, },
ncMeta ncMeta
); );
@ -507,6 +533,7 @@ export default class Column<T = any> implements ColumnType {
MetaTable.COLUMNS, MetaTable.COLUMNS,
colId colId
); );
if (colData) {
try { try {
colData.meta = JSON.parse(colData.meta); colData.meta = JSON.parse(colData.meta);
} catch { } catch {
@ -514,6 +541,7 @@ export default class Column<T = any> implements ColumnType {
} }
await NocoCache.set(`${CacheScope.COLUMN}:${colId}`, colData); await NocoCache.set(`${CacheScope.COLUMN}:${colId}`, colData);
} }
}
if (colData) { if (colData) {
const column = new Column(colData); const column = new Column(colData);
await column.getColOptions(ncMeta); await column.getColOptions(ncMeta);

Loading…
Cancel
Save