2022-11-20 09:48:13 +01:00
|
|
|
import { User, Website } from '@prisma/client';
|
2022-12-27 06:51:16 +01:00
|
|
|
import redis from '@umami/redis-client';
|
2022-11-20 09:48:13 +01:00
|
|
|
import { getSession, getUser, getWebsite } from '../queries';
|
2022-11-08 07:35:51 +01:00
|
|
|
|
2023-05-06 06:16:13 +02:00
|
|
|
const { fetchObject, storeObject, deleteObject } = redis;
|
2022-11-08 07:35:51 +01:00
|
|
|
|
2022-11-20 09:48:13 +01:00
|
|
|
async function fetchWebsite(id): Promise<Website> {
|
2022-11-08 07:35:51 +01:00
|
|
|
return fetchObject(`website:${id}`, () => getWebsite({ id }));
|
|
|
|
}
|
|
|
|
|
|
|
|
async function storeWebsite(data) {
|
|
|
|
const { id } = data;
|
|
|
|
const key = `website:${id}`;
|
|
|
|
|
|
|
|
return storeObject(key, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function deleteWebsite(id) {
|
|
|
|
return deleteObject(`website:${id}`);
|
|
|
|
}
|
|
|
|
|
2022-11-20 09:48:13 +01:00
|
|
|
async function fetchUser(id): Promise<User> {
|
2022-12-27 06:51:16 +01:00
|
|
|
return fetchObject(`user:${id}`, () => getUser({ id }, { includePassword: true }));
|
2022-11-08 07:35:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async function storeUser(data) {
|
|
|
|
const { id } = data;
|
|
|
|
const key = `user:${id}`;
|
|
|
|
|
|
|
|
return storeObject(key, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function deleteUser(id) {
|
|
|
|
return deleteObject(`user:${id}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function fetchSession(id) {
|
|
|
|
return fetchObject(`session:${id}`, () => getSession({ id }));
|
|
|
|
}
|
|
|
|
|
|
|
|
async function storeSession(data) {
|
|
|
|
const { id } = data;
|
|
|
|
const key = `session:${id}`;
|
|
|
|
|
|
|
|
return storeObject(key, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function deleteSession(id) {
|
|
|
|
return deleteObject(`session:${id}`);
|
|
|
|
}
|
|
|
|
|
2023-05-16 05:41:12 +02:00
|
|
|
async function fetchUserBlock(userId: string) {
|
|
|
|
const key = `user:block:${userId}`;
|
|
|
|
return redis.get(key);
|
|
|
|
}
|
|
|
|
|
2022-11-08 07:35:51 +01:00
|
|
|
export default {
|
|
|
|
fetchWebsite,
|
|
|
|
storeWebsite,
|
|
|
|
deleteWebsite,
|
|
|
|
fetchUser,
|
|
|
|
storeUser,
|
|
|
|
deleteUser,
|
|
|
|
fetchSession,
|
|
|
|
storeSession,
|
|
|
|
deleteSession,
|
2023-05-16 05:41:12 +02:00
|
|
|
fetchUserBlock,
|
2022-11-08 07:35:51 +01:00
|
|
|
enabled: redis.enabled,
|
|
|
|
};
|