iceshrimp-legacy/packages/backend/src/misc/fetch-meta.ts

38 lines
815 B
TypeScript
Raw Normal View History

import { db } from '@/db/postgre.js';
import { Meta } from '@/models/entities/meta.js';
2019-04-24 01:11:19 +02:00
let cache: Meta;
export async function fetchMeta(noCache = false): Promise<Meta> {
if (!noCache && cache) return cache;
return await db.transaction(async transactionalEntityManager => {
2020-02-05 07:41:14 +01:00
// 過去のバグでレコードが複数出来てしまっている可能性があるので新しいIDを優先する
2022-03-27 09:16:13 +02:00
const metas = await transactionalEntityManager.find(Meta, {
order: {
2021-12-09 15:58:30 +01:00
id: 'DESC',
},
});
2022-03-27 09:16:13 +02:00
const meta = metas[0];
if (meta) {
2019-04-24 01:11:19 +02:00
cache = meta;
return meta;
} else {
2019-04-24 01:11:19 +02:00
const saved = await transactionalEntityManager.save(Meta, {
2021-12-09 15:58:30 +01:00
id: 'x',
}) as Meta;
2019-04-24 01:11:19 +02:00
cache = saved;
return saved;
}
});
}
2019-04-24 01:11:19 +02:00
setInterval(() => {
fetchMeta(true).then(meta => {
cache = meta;
});
2021-03-19 10:22:34 +01:00
}, 1000 * 10);