migrate from SQLite to PostgreSQL with Drizzle ORM
- Updated all packages to latest versions (React 19, Next.js 14.2.32) - Replaced sqlite3 with pg and drizzle-orm dependencies - Created complete PostgreSQL schema with relationships and indexes - Migrated all API endpoints from SQLite to Drizzle queries - Added database seeding with sample data - Updated authentication to use bcrypt instead of pbkdf2 - Configured connection pooling for PostgreSQL - Updated app version to 1.0.0 - All endpoints tested and working correctly
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import type { NextApiRequest, NextApiResponse } from 'next'
|
||||
import sqlite3 from 'sqlite3'
|
||||
import path from 'path'
|
||||
import { db, schema } from '../../../lib/db/connection'
|
||||
import { eq, and, gte, count } from 'drizzle-orm'
|
||||
|
||||
interface DashboardStats {
|
||||
total_sources: number
|
||||
@@ -19,54 +19,65 @@ export default async function handler(
|
||||
return res.status(405).json({ error: 'Method not allowed' })
|
||||
}
|
||||
|
||||
const dbPath = path.join(process.cwd(), 'database', 'antihoax.db')
|
||||
const db = new sqlite3.Database(dbPath)
|
||||
|
||||
try {
|
||||
const stats = await new Promise<DashboardStats>((resolve, reject) => {
|
||||
const queries = [
|
||||
"SELECT COUNT(*) as total_sources FROM sources WHERE status = 'verified'",
|
||||
"SELECT COUNT(*) as pending_sources FROM sources WHERE status = 'pending'",
|
||||
"SELECT COUNT(*) as pending_reports FROM reports WHERE status = 'pending'",
|
||||
"SELECT COUNT(*) as high_risk_sources FROM sources WHERE status = 'verified' AND risk_level >= 4",
|
||||
"SELECT COUNT(*) as sources_added_week FROM sources WHERE created_at > datetime('now', '-7 days')",
|
||||
"SELECT COUNT(*) as reports_today FROM reports WHERE created_at > datetime('now', '-1 day')"
|
||||
]
|
||||
|
||||
const results: any = {}
|
||||
let completed = 0
|
||||
|
||||
queries.forEach((query, index) => {
|
||||
db.get(query, (err, row: any) => {
|
||||
if (err) {
|
||||
reject(err)
|
||||
return
|
||||
}
|
||||
|
||||
const key = Object.keys(row)[0]
|
||||
results[key] = row[key]
|
||||
completed++
|
||||
|
||||
if (completed === queries.length) {
|
||||
resolve({
|
||||
total_sources: results.total_sources || 0,
|
||||
pending_sources: results.pending_sources || 0,
|
||||
pending_reports: results.pending_reports || 0,
|
||||
high_risk_sources: results.high_risk_sources || 0,
|
||||
sources_added_week: results.sources_added_week || 0,
|
||||
reports_today: results.reports_today || 0
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
// Get all stats in parallel
|
||||
const weekAgo = new Date()
|
||||
weekAgo.setDate(weekAgo.getDate() - 7)
|
||||
|
||||
const dayAgo = new Date()
|
||||
dayAgo.setDate(dayAgo.getDate() - 1)
|
||||
|
||||
const [
|
||||
totalSources,
|
||||
pendingSources,
|
||||
pendingReports,
|
||||
highRiskSources,
|
||||
sourcesAddedWeek,
|
||||
reportsToday
|
||||
] = await Promise.all([
|
||||
db.select({ count: count() })
|
||||
.from(schema.sources)
|
||||
.where(eq(schema.sources.status, 'verified')),
|
||||
|
||||
db.select({ count: count() })
|
||||
.from(schema.sources)
|
||||
.where(eq(schema.sources.status, 'pending')),
|
||||
|
||||
db.select({ count: count() })
|
||||
.from(schema.reports)
|
||||
.where(eq(schema.reports.status, 'pending')),
|
||||
|
||||
db.select({ count: count() })
|
||||
.from(schema.sources)
|
||||
.where(
|
||||
and(
|
||||
eq(schema.sources.status, 'verified'),
|
||||
gte(schema.sources.riskLevel, 4)
|
||||
)
|
||||
),
|
||||
|
||||
db.select({ count: count() })
|
||||
.from(schema.sources)
|
||||
.where(gte(schema.sources.createdAt, weekAgo)),
|
||||
|
||||
db.select({ count: count() })
|
||||
.from(schema.reports)
|
||||
.where(gte(schema.reports.createdAt, dayAgo))
|
||||
])
|
||||
|
||||
const stats: DashboardStats = {
|
||||
total_sources: totalSources[0].count,
|
||||
pending_sources: pendingSources[0].count,
|
||||
pending_reports: pendingReports[0].count,
|
||||
high_risk_sources: highRiskSources[0].count,
|
||||
sources_added_week: sourcesAddedWeek[0].count,
|
||||
reports_today: reportsToday[0].count
|
||||
}
|
||||
|
||||
return res.status(200).json(stats)
|
||||
|
||||
} catch (error) {
|
||||
console.error('Database error:', error)
|
||||
return res.status(500).json({ error: 'Internal server error' })
|
||||
} finally {
|
||||
db.close()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user