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, gte, desc, count, sql } from 'drizzle-orm'
|
||||
|
||||
interface RiskyDomain {
|
||||
domain: string
|
||||
@@ -20,49 +20,36 @@ export default async function handler(
|
||||
|
||||
const { limit = '20' } = req.query
|
||||
|
||||
const dbPath = path.join(process.cwd(), 'database', 'antihoax.db')
|
||||
const db = new sqlite3.Database(dbPath)
|
||||
|
||||
try {
|
||||
const riskyDomains = await new Promise<RiskyDomain[]>((resolve, reject) => {
|
||||
db.all(
|
||||
`SELECT
|
||||
s.domain,
|
||||
COUNT(*) as source_count,
|
||||
AVG(s.risk_level) as avg_risk_level,
|
||||
MAX(s.risk_level) as max_risk_level,
|
||||
GROUP_CONCAT(DISTINCT c.name) as categories
|
||||
FROM sources s
|
||||
LEFT JOIN source_categories sc ON s.id = sc.source_id
|
||||
LEFT JOIN categories c ON sc.category_id = c.id
|
||||
WHERE s.status = 'verified'
|
||||
GROUP BY s.domain
|
||||
HAVING AVG(s.risk_level) >= 3
|
||||
ORDER BY avg_risk_level DESC, source_count DESC
|
||||
LIMIT ?`,
|
||||
[parseInt(limit as string)],
|
||||
(err, rows: any[]) => {
|
||||
if (err) reject(err)
|
||||
else {
|
||||
const domains = rows.map(row => ({
|
||||
domain: row.domain,
|
||||
source_count: row.source_count,
|
||||
avg_risk_level: Math.round(row.avg_risk_level * 10) / 10,
|
||||
max_risk_level: row.max_risk_level,
|
||||
categories: row.categories ? row.categories.split(',') : []
|
||||
}))
|
||||
resolve(domains)
|
||||
}
|
||||
}
|
||||
)
|
||||
})
|
||||
const riskyDomainsResult = await db
|
||||
.select({
|
||||
domain: schema.sources.domain,
|
||||
sourceCount: count(),
|
||||
avgRiskLevel: sql<number>`AVG(${schema.sources.riskLevel})`,
|
||||
maxRiskLevel: sql<number>`MAX(${schema.sources.riskLevel})`,
|
||||
categories: sql<string>`string_agg(DISTINCT ${schema.categories.name}, ',')`
|
||||
})
|
||||
.from(schema.sources)
|
||||
.leftJoin(schema.sourceCategories, eq(schema.sources.id, schema.sourceCategories.sourceId))
|
||||
.leftJoin(schema.categories, eq(schema.sourceCategories.categoryId, schema.categories.id))
|
||||
.where(eq(schema.sources.status, 'verified'))
|
||||
.groupBy(schema.sources.domain)
|
||||
.having(gte(sql`AVG(${schema.sources.riskLevel})`, 3))
|
||||
.orderBy(desc(sql`AVG(${schema.sources.riskLevel})`), desc(count()))
|
||||
.limit(parseInt(limit as string))
|
||||
|
||||
const riskyDomains: RiskyDomain[] = riskyDomainsResult.map(row => ({
|
||||
domain: row.domain,
|
||||
source_count: row.sourceCount,
|
||||
avg_risk_level: Math.round(row.avgRiskLevel * 10) / 10,
|
||||
max_risk_level: row.maxRiskLevel,
|
||||
categories: row.categories ? row.categories.split(',').filter(Boolean) : []
|
||||
}))
|
||||
|
||||
return res.status(200).json(riskyDomains)
|
||||
|
||||
} 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