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