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,16 +1,16 @@
import sqlite3 from 'sqlite3'
import path from 'path'
import crypto from 'crypto'
import { db, schema } from './db/connection'
import { eq, and } from 'drizzle-orm'
export interface ApiKey {
id: number
key_hash: string
keyHash: string
name: string
permissions: string[]
rate_limit: number
is_active: boolean
last_used?: string
created_at: string
rateLimit: number
isActive: boolean
lastUsed?: Date
createdAt: Date
}
export function generateApiKey(): string {
@@ -25,48 +25,38 @@ export async function validateApiKey(key: string): Promise<ApiKey | null> {
if (!key || !key.startsWith('ak_')) return null
const keyHash = hashApiKey(key)
const dbPath = path.join(process.cwd(), 'database', 'antihoax.db')
const db = new sqlite3.Database(dbPath)
try {
const apiKey = await new Promise<ApiKey | null>((resolve, reject) => {
db.get(
'SELECT * FROM api_keys WHERE key_hash = ? AND is_active = 1',
[keyHash],
(err, row: any) => {
if (err) reject(err)
else if (row) {
resolve({
...row,
permissions: row.permissions ? JSON.parse(row.permissions) : []
})
} else {
resolve(null)
}
}
)
})
const apiKeys = await db.select()
.from(schema.apiKeys)
.where(and(
eq(schema.apiKeys.keyHash, keyHash),
eq(schema.apiKeys.isActive, true)
))
.limit(1)
if (apiKey) {
// Update last_used timestamp
await new Promise<void>((resolve, reject) => {
db.run(
'UPDATE api_keys SET last_used = datetime("now") WHERE id = ?',
[apiKey.id],
(err) => {
if (err) reject(err)
else resolve()
}
)
})
if (apiKeys.length === 0) return null
const apiKey = apiKeys[0]
// Update last_used timestamp
await db.update(schema.apiKeys)
.set({ lastUsed: new Date() })
.where(eq(schema.apiKeys.id, apiKey.id))
return {
id: apiKey.id,
keyHash: apiKey.keyHash,
name: apiKey.name,
permissions: apiKey.permissions ? JSON.parse(apiKey.permissions) : [],
rateLimit: apiKey.rateLimit,
isActive: apiKey.isActive,
lastUsed: apiKey.lastUsed,
createdAt: apiKey.createdAt
}
return apiKey
} catch (error) {
console.error('API key validation error:', error)
return null
} finally {
db.close()
}
}