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, sql } from 'drizzle-orm'
|
||||
import { rateLimit, getRateLimitHeaders } from '../../../lib/rate-limiter'
|
||||
import { cache, getCacheKey } from '../../../lib/cache'
|
||||
|
||||
@@ -88,25 +88,23 @@ export default async function handler(
|
||||
return res.status(200).json(cachedResult)
|
||||
}
|
||||
|
||||
const dbPath = path.join(process.cwd(), 'database', 'antihoax.db')
|
||||
const db = new sqlite3.Database(dbPath)
|
||||
|
||||
try {
|
||||
const sources = await new Promise<any[]>((resolve, reject) => {
|
||||
db.all(
|
||||
`SELECT s.*, GROUP_CONCAT(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.domain = ? AND s.status = 'verified'
|
||||
GROUP BY s.id`,
|
||||
[domain],
|
||||
(err, rows) => {
|
||||
if (err) reject(err)
|
||||
else resolve(rows)
|
||||
}
|
||||
const sources = await db
|
||||
.select({
|
||||
id: schema.sources.id,
|
||||
riskLevel: schema.sources.riskLevel,
|
||||
categories: sql<string>`string_agg(${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(
|
||||
and(
|
||||
eq(schema.sources.domain, domain),
|
||||
eq(schema.sources.status, 'verified')
|
||||
)
|
||||
)
|
||||
})
|
||||
.groupBy(schema.sources.id, schema.sources.riskLevel)
|
||||
|
||||
let result: CheckResponse
|
||||
|
||||
@@ -119,7 +117,7 @@ export default async function handler(
|
||||
source_count: 0
|
||||
}
|
||||
} else {
|
||||
const maxRiskLevel = Math.max(...sources.map(s => s.risk_level))
|
||||
const maxRiskLevel = Math.max(...sources.map(s => s.riskLevel))
|
||||
const allCategories = sources
|
||||
.map(s => s.categories)
|
||||
.filter(Boolean)
|
||||
@@ -156,7 +154,5 @@ export default async function handler(
|
||||
} 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