Browse Source

refactor(api): handle all error cases and on error log and return as null

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/3962/head
Pranav C 2 years ago
parent
commit
4adc242f7e
  1. 107
      packages/nocodb/src/lib/meta/api/utilApis.ts

107
packages/nocodb/src/lib/meta/api/utilApis.ts

@ -184,33 +184,38 @@ export async function urlToDbConfig(req: Request, res: Response) {
} }
} }
interface ViewCount {
formCount: number | null;
gridCount: number | null;
galleryCount: number | null;
kanbanCount: number | null;
total: number | null;
sharedFormCount: number | null;
sharedGridCount: number | null;
sharedGalleryCount: number | null;
sharedKanbanCount: number | null;
sharedTotal: number | null;
sharedLockedCount: number | null;
}
interface AllMeta { interface AllMeta {
projectCount: number; projectCount: number;
projects: { projects: (
external?: boolean; | {
external?: boolean | null;
tableCount: { tableCount: {
table: number; table: number;
view: number; view: number;
}; } | null;
viewCount: { viewCount: ViewCount;
formCount: number; webhookCount: number | null;
gridCount: number; filterCount: number | null;
galleryCount: number; sortCount: number | null;
kanbanCount: number; rowCount: ({ totalRecords: number } | null)[] | null;
total: number; userCount: number | null;
sharedFormCount: number; }
sharedGridCount: number; | { error: string }
sharedGalleryCount: number; )[];
sharedKanbanCount: number;
sharedTotal: number;
sharedLockedCount: number;
};
webhookCount: number;
filterCount: number;
sortCount: number;
rowCount: { totalRecords: number }[];
userCount: number;
}[];
userCount: number; userCount: number;
sharedBaseCount: number; sharedBaseCount: number;
} }
@ -229,7 +234,8 @@ export async function getAggregatedMetaInfo(_req: Request, res: Response) {
}; };
result.projects.push( result.projects.push(
...(await Promise.all( ...extractResultOrNull(
await Promise.allSettled(
projects.map(async (project) => { projects.map(async (project) => {
if (project.uuid) result.sharedBaseCount++; if (project.uuid) result.sharedBaseCount++;
const [ const [
@ -241,30 +247,29 @@ export async function getAggregatedMetaInfo(_req: Request, res: Response) {
sortCount, sortCount,
rowCount, rowCount,
userCount, userCount,
] = await Promise.all([ ] = extractResultOrNull(
await Promise.allSettled([
// db tables count // db tables count
Noco.ncMeta.metaCount(null, null, MetaTable.MODELS, { Noco.ncMeta.metaCount(project.id, null, MetaTable.MODELS, {
condition: { condition: {
project_id: project.id,
type: 'table', type: 'table',
}, },
}), }),
// db views count // db views count
Noco.ncMeta.metaCount(null, null, MetaTable.MODELS, { Noco.ncMeta.metaCount(project.id, null, MetaTable.MODELS, {
condition: { condition: {
project_id: project.id,
type: 'view', type: 'view',
}, },
}), }),
// views count // views count
(async () => { (async () => {
const views = await Noco.ncMeta.metaList2( const views = await Noco.ncMeta.metaList2(
null, project.id,
null, null,
MetaTable.VIEWS MetaTable.VIEWS
); );
// grid, form, gallery, kanban and shared count // grid, form, gallery, kanban and shared count
return views.reduce<AllMeta['projects'][number]['viewCount']>( return views.reduce<ViewCount>(
(out, view) => { (out, view) => {
out.total++; out.total++;
@ -306,31 +311,21 @@ export async function getAggregatedMetaInfo(_req: Request, res: Response) {
); );
})(), })(),
// webhooks count // webhooks count
Noco.ncMeta.metaCount(null, null, MetaTable.HOOKS, { Noco.ncMeta.metaCount(project.id, null, MetaTable.HOOKS),
condition: {
project_id: project.id,
},
}),
// filters count // filters count
Noco.ncMeta.metaCount(null, null, MetaTable.FILTER_EXP, { Noco.ncMeta.metaCount(project.id, null, MetaTable.FILTER_EXP),
condition: {
project_id: project.id,
},
}),
// sorts count // sorts count
Noco.ncMeta.metaCount(null, null, MetaTable.SORT, { Noco.ncMeta.metaCount(project.id, null, MetaTable.SORT),
condition: {
project_id: project.id,
},
}),
// row count per base // row count per base
project.getBases().then((bases) => { project.getBases().then(async (bases) => {
return Promise.all( return extractResultOrNull(
await Promise.allSettled(
bases.map((base) => bases.map((base) =>
NcConnectionMgrv2.getSqlClient(base) NcConnectionMgrv2.getSqlClient(base)
.totalRecords?.() .totalRecords?.()
?.then((result) => result?.data) ?.then((result) => result?.data)
) )
)
); );
}), }),
// project users count // project users count
@ -340,10 +335,11 @@ export async function getAggregatedMetaInfo(_req: Request, res: Response) {
}, },
aggField: '*', aggField: '*',
}), }),
]); ])
);
return { return {
tableCount: { table: tableCount, view: dbViewCount }, tableCount: { table: tableCount.status, view: dbViewCount },
external: !project.is_meta, external: !project.is_meta,
viewCount, viewCount,
webhookCount, webhookCount,
@ -353,12 +349,23 @@ export async function getAggregatedMetaInfo(_req: Request, res: Response) {
userCount, userCount,
}; };
}) })
)) )
)
); );
res.json(result); res.json(result);
} }
const extractResultOrNull = (results: PromiseSettledResult<any>[]) => {
return results.map((result) => {
if (result.status === 'fulfilled') {
return result.value;
}
console.log(result.reason);
return null;
});
};
export default (router) => { export default (router) => {
router.post( router.post(
'/api/v1/db/meta/connection/test', '/api/v1/db/meta/connection/test',

Loading…
Cancel
Save