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

Loading…
Cancel
Save