chore: query optimization

This commit is contained in:
2024-12-08 10:17:25 +01:00
parent a91c4d56b0
commit b154474075
5 changed files with 53 additions and 66 deletions

View File

@@ -1,4 +1,4 @@
import prisma from '../prisma/prisma';
import prisma from '@prisma';
export async function rateLimiter() {
if (!process.env.RATE_LIMIT) {
@@ -7,21 +7,24 @@ export async function rateLimiter() {
const yesterday = new Date(Date.now() - 24 * 60 * 60 * 1000);
const consumersCount = await prisma.consumer.count({
where: {
createdAt: {
gt: yesterday
const count = await prisma.$transaction(async tx => {
const consumersCount = await tx.consumer.count({
where: {
createdAt: {
gt: yesterday
}
}
}
});
const purchaseListsCount = await tx.purchaseList.count({
where: {
createdAt: {
gt: yesterday
}
}
});
return consumersCount + purchaseListsCount;
});
const purchaseListsCount = await prisma.purchaseList.count({
where: {
createdAt: {
gt: yesterday
}
}
});
return consumersCount + purchaseListsCount > parseInt(process.env.RATE_LIMIT);
return count > parseInt(process.env.RATE_LIMIT);
}