add db call as fallback

This commit is contained in:
Brian Cao 2022-08-29 13:04:58 -07:00
parent 9ddbd12060
commit b6cd9f305b
10 changed files with 71 additions and 40 deletions

3
.gitignore vendored
View File

@ -35,3 +35,6 @@ yarn-error.log*
.env.development.local
.env.test.local
.env.production.local
*.development.yml

View File

@ -6,6 +6,7 @@ import { REDIS } from 'lib/db';
const log = debug('umami:redis');
const INITIALIZED = 'redis:initialized';
export const DELETED = 'deleted';
function getClient() {
const redis = new Redis(process.env.REDIS_URL);

View File

@ -1,7 +1,7 @@
import { parseToken } from 'next-basics';
import { validate } from 'uuid';
import { uuid } from 'lib/crypto';
import redis from 'lib/redis';
import redis, { DELETED } from 'lib/redis';
import { getClientInfo, getJsonBody } from 'lib/request';
import { createSession, getSessionByUuid, getWebsiteByUuid } from 'queries';
@ -30,17 +30,18 @@ export async function getSession(req) {
let websiteId = null;
//console.log(await redis.stageData());
// Check if website exists
if (process.env.REDIS_URL) {
websiteId = await redis.client.get(`website:${website_uuid}`);
} else {
if (redis.client) {
websiteId = BigInt(await redis.client.get(`website:${website_uuid}`));
}
// Check database if redis does not have
if (!websiteId) {
const { website_id } = await getWebsiteByUuid(website_uuid);
websiteId = website_id;
}
if (!websiteId) {
if (!websiteId || websiteId === DELETED) {
throw new Error(`Website not found: ${website_uuid}`);
}
@ -53,9 +54,12 @@ export async function getSession(req) {
let session = null;
// Check if session exists
if (process.env.REDIS_URL) {
if (redis.client) {
sessionCreated = !!(await redis.client.get(`session:${session_uuid}`));
} else {
}
// Check database if redis does not have
if (!sessionCreated) {
session = await getSessionByUuid(session_uuid);
sessionCreated = !!session;
sessionId = session ? session.session_id : null;

View File

@ -1,28 +1,46 @@
import prisma from 'lib/prisma';
import redis, { DELETED } from 'lib/redis';
export async function deleteAccount(user_id) {
const { client } = prisma;
return client.$transaction([
client.pageview.deleteMany({
where: { session: { website: { user_id } } },
}),
client.event_data.deleteMany({
where: { event: { session: { website: { user_id } } } },
}),
client.event.deleteMany({
where: { session: { website: { user_id } } },
}),
client.session.deleteMany({
where: { website: { user_id } },
}),
client.website.deleteMany({
const websiteUuids = await client.website
.findMany({
where: { user_id },
}),
client.account.delete({
where: {
user_id,
},
}),
]);
select: { website_uuid: true },
})
.map(a => a.website_uuid);
return client
.$transaction([
client.pageview.deleteMany({
where: { session: { website: { user_id } } },
}),
client.event_data.deleteMany({
where: { event: { session: { website: { user_id } } } },
}),
client.event.deleteMany({
where: { session: { website: { user_id } } },
}),
client.session.deleteMany({
where: { website: { user_id } },
}),
client.website.deleteMany({
where: { user_id },
}),
client.account.delete({
where: {
user_id,
},
}),
])
.then(async res => {
if (redis.client) {
for (let i = 0; i < websiteUuids.length; i++) {
await redis.client.set(`website:${websiteUuids[i]}`, DELETED);
}
}
return res;
});
}

View File

@ -14,7 +14,7 @@ export async function createWebsite(user_id, data) {
},
})
.then(async res => {
if (process.env.REDIS_URL && res) {
if (redis.client && res) {
await redis.set(`website:${res.website_uuid}`, Number(res.website_id));
}

View File

@ -1,9 +1,12 @@
import prisma from 'lib/prisma';
import redis from 'lib/redis';
import redis, { DELETED } from 'lib/redis';
import { getWebsiteById } from 'queries';
export async function deleteWebsite(website_id) {
const { client, transaction } = prisma;
const { website_uuid } = await getWebsiteById(website_id);
return transaction([
client.pageview.deleteMany({
where: { session: { website: { website_id } } },
@ -21,8 +24,10 @@ export async function deleteWebsite(website_id) {
where: { website_id },
}),
]).then(async res => {
if (process.env.REDIS_URL) {
await redis.client.del(`website:${res.website_uuid}`);
if (redis.client) {
await redis.client.set(`website:${website_uuid}`, DELETED);
}
return res;
});
}

View File

@ -9,7 +9,7 @@ export async function getWebsiteByUuid(website_uuid) {
},
})
.then(async res => {
if (process.env.REDIS_URL && res) {
if (redis.client && res) {
await redis.client.set(`website:${res.website_uuid}`, 1);
}

View File

@ -18,7 +18,7 @@ export async function resetWebsite(website_id) {
where: { website: { website_id } },
}),
]).then(async res => {
if (process.env.REDIS_URL) {
if (redis.client) {
await redis.del(`website:${res.website_uuid}`);
}
});

View File

@ -22,7 +22,7 @@ async function relationalQuery(website_id, data) {
},
})
.then(async res => {
if (process.env.REDIS_URL && res) {
if (redis.client && res) {
await redis.client.set(`session:${res.session_uuid}`, 1);
}
@ -50,7 +50,7 @@ async function clickhouseQuery(
await sendMessage(params, 'session');
if (process.env.REDIS_URL) {
if (redis.client) {
await redis.client.set(`session:${session_uuid}`, 1);
}
}

View File

@ -18,7 +18,7 @@ async function relationalQuery(session_uuid) {
},
})
.then(async res => {
if (process.env.REDIS_URL && res) {
if (redis.client && res) {
await redis.client.set(`session:${res.session_uuid}`, 1);
}
@ -48,7 +48,7 @@ async function clickhouseQuery(session_uuid) {
)
.then(result => findFirst(result))
.then(async res => {
if (process.env.REDIS_URL && res) {
if (redis.client && res) {
await redis.client.set(`session:${res.session_uuid}`, 1);
}