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

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);
}