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:
2025-09-06 12:56:33 +02:00
parent 52bde64e7f
commit 860070a302
26 changed files with 2526 additions and 2403 deletions

View File

@@ -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, desc } from 'drizzle-orm'
export default async function handler(
req: NextApiRequest,
@@ -12,44 +12,29 @@ export default async function handler(
const { status = 'pending', page = '1', limit = '20' } = req.query
const dbPath = path.join(process.cwd(), 'database', 'antihoax.db')
const db = new sqlite3.Database(dbPath)
try {
const reports = await new Promise<any[]>((resolve, reject) => {
const offset = (parseInt(page as string) - 1) * parseInt(limit as string)
db.all(
`SELECT *,
CASE
WHEN category_suggestions IS NOT NULL
THEN json_extract(category_suggestions, '$')
ELSE '[]'
END as category_suggestions
FROM reports
WHERE status = ?
ORDER BY created_at DESC
LIMIT ? OFFSET ?`,
[status, parseInt(limit as string), offset],
(err, rows: any[]) => {
if (err) reject(err)
else {
const processedRows = rows.map(row => ({
...row,
category_suggestions: row.category_suggestions ? JSON.parse(row.category_suggestions) : []
}))
resolve(processedRows)
}
}
)
})
const offset = (parseInt(page as string) - 1) * parseInt(limit as string)
const limitInt = parseInt(limit as string)
const reports = await db
.select()
.from(schema.reports)
.where(eq(schema.reports.status, status as any))
.orderBy(desc(schema.reports.createdAt))
.limit(limitInt)
.offset(offset)
return res.status(200).json(reports)
// Process the reports to parse JSON fields
const processedReports = reports.map(report => ({
...report,
categorySuggestions: report.categorySuggestions ? JSON.parse(report.categorySuggestions) : [],
evidenceUrls: report.evidenceUrls ? JSON.parse(report.evidenceUrls) : []
}))
return res.status(200).json(processedReports)
} catch (error) {
console.error('Database error:', error)
return res.status(500).json({ error: 'Internal server error' })
} finally {
db.close()
}
}