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