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