RenaiApp/src/main/modules/store/store.ts

31 lines
915 B
TypeScript

import { injectable } from 'inversify';
import { Database, getConnection } from '../../core/database';
import { StoreValue } from '../../entities/store/store-value';
const CACHE_ID = 'store';
@injectable()
export class Store implements StoreInterface {
public async load(key: StoreKey): Promise<unknown> {
const c = await getConnection(Database.STORE);
const repository = c.getRepository(StoreValue);
const storeValue = await repository.findOne(key, {
cache: {
id: CACHE_ID,
milliseconds: 0,
},
});
return storeValue?.value;
}
public async save(key: StoreKey, data: unknown): Promise<void> {
const c = await getConnection(Database.STORE);
const { manager } = c;
const storeValue = new StoreValue();
storeValue.key = key;
storeValue.value = data;
await manager.save(storeValue);
await c.queryResultCache?.remove([CACHE_ID]);
}
}