2022-03-26 07:34:00 +01:00
|
|
|
import { db } from '@/db/postgre.js';
|
2022-02-27 03:07:39 +01:00
|
|
|
import { Meta } from '@/models/entities/meta.js';
|
2018-11-05 23:14:43 +01:00
|
|
|
|
2019-04-24 01:11:19 +02:00
|
|
|
let cache: Meta;
|
|
|
|
|
|
|
|
export async function fetchMeta(noCache = false): Promise<Meta> {
|
|
|
|
if (!noCache && cache) return cache;
|
|
|
|
|
2022-03-26 07:34:00 +01:00
|
|
|
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, {
|
2019-04-16 17:45:33 +02:00
|
|
|
order: {
|
2021-12-09 15:58:30 +01:00
|
|
|
id: 'DESC',
|
|
|
|
},
|
2019-04-16 17:45:33 +02:00
|
|
|
});
|
|
|
|
|
2022-03-27 09:16:13 +02:00
|
|
|
const meta = metas[0];
|
|
|
|
|
2019-04-16 17:45:33 +02:00
|
|
|
if (meta) {
|
2019-04-24 01:11:19 +02:00
|
|
|
cache = meta;
|
2019-04-16 17:45:33 +02:00
|
|
|
return meta;
|
|
|
|
} else {
|
2022-05-14 08:16:45 +02:00
|
|
|
// metaが空のときfetchMetaが同時に呼ばれるとここが同時に呼ばれてしまうことがあるのでフェイルセーフなupsertを使う
|
|
|
|
const saved = await transactionalEntityManager
|
|
|
|
.upsert(
|
|
|
|
Meta,
|
|
|
|
{
|
|
|
|
id: 'x',
|
|
|
|
},
|
|
|
|
['id'],
|
|
|
|
)
|
|
|
|
.then((x) => transactionalEntityManager.findOneByOrFail(Meta, x.identifiers[0]));
|
2019-04-24 01:11:19 +02:00
|
|
|
|
|
|
|
cache = saved;
|
|
|
|
return saved;
|
2019-04-16 17:45:33 +02:00
|
|
|
}
|
|
|
|
});
|
2018-11-05 23:14:43 +01:00
|
|
|
}
|
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);
|