Rich Bayliss bc9bdd1094
validation: Ensure commit lookup has a bound value
Change-type: patch
Signed-off-by: Rich Bayliss <rich@balena.io>
2020-11-11 11:01:20 +00:00

37 lines
670 B
TypeScript

import * as db from '../db';
const cache: { [appId: number]: string } = {};
export async function getCommitForApp(
appId: number,
): Promise<string | undefined> {
if (isNaN(appId)) {
return;
}
if (cache[appId] != null) {
return cache[appId];
}
const commit = await db
.models('currentCommit')
.where({ appId })
.select('commit');
if (commit?.[0] != null) {
cache[appId] = commit[0].commit;
return commit[0].commit;
}
return;
}
export function upsertCommitForApp(
appId: number,
commit: string,
trx?: db.Transaction,
): Promise<void> {
cache[appId] = commit;
return db.upsertModel('currentCommit', { commit, appId }, { appId }, trx);
}