LaLiga-FrontEnd/prisma/stats.ts

26 lines
745 B
TypeScript

import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
export const getAllStats = async () => {
try {
const totalPosts = await prisma.post.count()
const totalUsers = await prisma.user.count()
const totalViews = await prisma.post.aggregate({ _sum: { views: true } })
const totalShares = await prisma.post.aggregate({
_sum: { twitterShareCount: true, whatsappShareCount: true }
})
return {
totalPosts,
totalUsers,
totalViews: totalViews._sum || 0,
totalShares: totalShares._sum || 0
}
} catch (error) {
console.error('Error fetching statistics:', error)
throw new Error('Error fetching statistics')
} finally {
await prisma.$disconnect()
}
}