Db-format module code fixups

Change-type: patch
Signed-off-by: Cameron Diver <cameron@balena.io>
This commit is contained in:
Cameron Diver 2020-06-15 10:27:17 +01:00
parent 0b9727c9cb
commit 695d91d494

View File

@ -37,9 +37,11 @@ export async function getApp(id: number): Promise<InstancedApp> {
export async function getApps(): Promise<InstancedAppState> { export async function getApps(): Promise<InstancedAppState> {
const dbApps = await getDBEntry(); const dbApps = await getDBEntry();
const apps: InstancedAppState = {}; const apps: InstancedAppState = {};
for (const app of dbApps) { await Promise.all(
apps[app.appId] = await buildApp(app); dbApps.map(async (app) => {
} apps[app.appId] = await buildApp(app);
}),
);
return apps; return apps;
} }
@ -98,9 +100,7 @@ async function buildApp(dbApp: targetStateCache.DatabaseApp) {
try { try {
imageInfo = await images.inspectByName(svc.image); imageInfo = await images.inspectByName(svc.image);
} catch (e) { } catch (e) {
if (NotFoundError(e)) { if (!NotFoundError(e)) {
imageInfo = undefined;
} else {
throw e; throw e;
} }
} }
@ -140,13 +140,11 @@ export async function setApps(
source: string, source: string,
trx?: db.Transaction, trx?: db.Transaction,
) { ) {
const cloned = _.cloneDeep(apps);
const dbApps = await Promise.all( const dbApps = await Promise.all(
Object.keys(cloned).map(async (appIdStr) => { Object.keys(apps).map(async (appIdStr) => {
const appId = checkInt(appIdStr)!; const appId = checkInt(appIdStr)!;
const app = cloned[appId]; const app = apps[appId];
const services = await Promise.all( const services = await Promise.all(
_.map(app.services, async (s, sId) => ({ _.map(app.services, async (s, sId) => ({
...s, ...s,
@ -176,7 +174,6 @@ export async function setApps(
function getDBEntry(): Promise<targetStateCache.DatabaseApp[]>; function getDBEntry(): Promise<targetStateCache.DatabaseApp[]>;
function getDBEntry(appId: number): Promise<targetStateCache.DatabaseApp>; function getDBEntry(appId: number): Promise<targetStateCache.DatabaseApp>;
async function getDBEntry(appId?: number) { async function getDBEntry(appId?: number) {
await config.initialized;
await targetStateCache.initialized; await targetStateCache.initialized;
return appId != null return appId != null