I am working on a NestJS project and using Prisma as my ORM. I need to aggregate multiple counts in my getAdminOverview method. Currently, I am making separate requests for each aggregation, which is inefficient. Here is my current implementation:
async getAdminOverview(): Promise<any> {
try {
// Aggregate total number of users active
const activeUser = await this.prisma.user.aggregate({
where: { status: UserStatus.ACTIVE },
_count: true,
});
// Aggregate total number of users inActive
const inActiveUser = await this.prisma.user.aggregate({
where: { status: UserStatus.INACTIVE },
_count: true,
});
// Aggregate total number of users BANNED
const bannedUser = await this.prisma.user.aggregate({
where: { status: UserStatus.BANNED },
_count: true,
});
// Aggregate total number of documents
const totalDocument = await this.prisma.document.aggregate({
where: {
type: {
notIn: [
DocumentType.LOGO,
DocumentType.BANK_TRANSFER,
DocumentType.CONVENTION,
],
},
},
_count: true,
});
// Aggregate total number of association profiles ACTIVE
const activeAssociation = await this.prisma.associationProfile.aggregate({
where: {
status: AssociationStatus.ACTIVE,
},
_count: true,
});
// Aggregate total number of association profiles INACTIVE
const inActiveAssociation =
await this.prisma.associationProfile.aggregate({
where: {
status: AssociationStatus.INACTIVE,
},
_count: true,
});
// Aggregate total number of association profiles BANNED
const bannedAssociation = await this.prisma.associationProfile.aggregate({
where: {
status: AssociationStatus.BANNED,
},
_count: true,
});
// Aggregate total number of PENDING bank transfers
const pendingBankTransfer = await this.prisma.bankTransfer.aggregate({
where: { status: BankTransferStatus.PENDING },
_count: true,
});
// Aggregate total number of APPROVED bank transfers
const approvedBankTransfer = await this.prisma.bankTransfer.aggregate({
where: { status: BankTransferStatus.APPROVED },
_count: true,
});
// Aggregate total number of APPROVED bank transfers
const sumTransferAmount = await this.prisma.bankTransfer.aggregate({
where: { status: BankTransferStatus.APPROVED },
_sum: {
transferAmount: true,
},
});
// Aggregate total number of conventions
const totalConvention = await this.prisma.convention.aggregate({
_count: true,
});
// Aggregate total number of conventions
const conventionWithoutCenter = await this.prisma.convention.aggregate({
where: {
center: null,
},
_count: true,
});
return {
userCount: {
activeUser: activeUser._count,
inActiveUser: inActiveUser._count,
bannedUser: bannedUser._count,
},
associationProfileCount: {
activeAssociation: activeAssociation._count,
inActiveAssociation: inActiveAssociation._count,
bannedAssociation: bannedAssociation._count,
},
bankTransferCount: {
pendingBankTransfer: pendingBankTransfer._count,
approvedBankTransfer: approvedBankTransfer._count,
sumTransferAmount: sumTransferAmount._sum.transferAmount,
},
conventionCount: {
totalConvention: totalConvention._count,
conventionWithoutCenter: conventionWithoutCenter._count,
},
totalDocument: totalDocument._count,
};
} catch (error) {
console.error(error);
throw new HttpException(
ErrorMessages.UNKNOWN_ERROR,
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
Question: How can I optimize this to perform multiple _count aggregations in a single request instead of multiple separate requests?
Any guidance on how to achieve this would be greatly appreciated. Thank you!