sharkey/packages/backend/src/misc/fetch-meta.ts

36 lines
807 B
TypeScript
Raw Normal View History

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