fixup server: refactor meta caching
All checks were successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/lint-client Pipeline was successful
ci/woodpecker/push/lint-foundkey-js Pipeline was successful
ci/woodpecker/push/lint-backend Pipeline was successful
ci/woodpecker/push/test Pipeline was successful

fix: setting meta does not keep cache synced.
fix: handle initially empty meta table.
This commit is contained in:
Johann150 2022-11-16 20:36:22 +01:00
parent 9f6be8d557
commit b958be77b6
Signed by: Johann150
GPG key ID: 9EE6577A2A06F8F1

View file

@ -15,22 +15,36 @@ export async function setMeta(meta: Meta): Promise<void> {
db.manager.clear(Meta);
db.manager.insert(Meta, meta);
cache = meta;
unlock();
}
/**
* Performs the primitive database operation to fetch server configuration.
* If there is no entry yet, inserts a new one.
* Writes to `cache` instead of returning.
*/
async function getMeta(): Promise<void> {
const unlock = await getFetchInstanceMetadataLock('localhost');
// new IDs are prioritised because multiple records may have been created due to past bugs
cache = db.manager.findOne(Meta, {
let metas = await db.manager.find(Meta, {
order: {
id: 'DESC',
},
});
if (metas.length === 0) {
db.manager.insert(Meta, {
id: 'x',
});
metas = await db.manager.find(Meta, {
order: {
id: 'DESC',
},
});
}
cache = metas[0];
unlock();
}