Setup and migrate to Prisma (#456)

This commit is contained in:
Peerawas Archavanuntakun 2022-01-06 03:30:32 +07:00 committed by GitHub
parent 129d121364
commit 51d378e4cb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
30 changed files with 605 additions and 273 deletions

View file

@ -1,5 +1,5 @@
import {injectable} from 'inversify';
import {KeyValueCache} from '../models/index.js';
import {prisma} from '../utils/db.js';
import debug from '../utils/debug.js';
type Seconds = number;
@ -29,7 +29,11 @@ export default class KeyValueCacheProvider {
throw new Error(`Cache key ${key} is too short.`);
}
const cachedResult = await KeyValueCache.findByPk(key);
const cachedResult = await prisma.keyValueCache.findUnique({
where: {
key,
},
});
if (cachedResult) {
if (new Date() < cachedResult.expiresAt) {
@ -37,7 +41,11 @@ export default class KeyValueCacheProvider {
return JSON.parse(cachedResult.value) as F;
}
await cachedResult.destroy();
await prisma.keyValueCache.delete({
where: {
key,
},
});
}
debug(`Cache miss: ${key}`);
@ -45,10 +53,21 @@ export default class KeyValueCacheProvider {
const result = await func(...options as any[]);
// Save result
await KeyValueCache.upsert({
key,
value: JSON.stringify(result),
expiresAt: futureTimeToDate(expiresIn),
const value = JSON.stringify(result);
const expiresAt = futureTimeToDate(expiresIn);
await prisma.keyValueCache.upsert({
where: {
key,
},
update: {
value,
expiresAt,
},
create: {
key,
value,
expiresAt,
},
});
return result;