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,24 +1,23 @@
|
||||
import type { NextApiRequest, NextApiResponse } from "next"
|
||||
import sqlite3 from "sqlite3"
|
||||
import path from "path"
|
||||
import { db, schema } from '../../../lib/db/connection'
|
||||
import { desc, eq } from 'drizzle-orm'
|
||||
import { generateApiKey, hashApiKey } from "../../../lib/api-auth"
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const dbPath = path.join(process.cwd(), "database", "antihoax.db")
|
||||
const db = new sqlite3.Database(dbPath)
|
||||
|
||||
try {
|
||||
if (req.method === "GET") {
|
||||
const keys = await new Promise<any[]>((resolve, reject) => {
|
||||
db.all(
|
||||
`SELECT id, name, permissions, rate_limit, is_active, last_used, created_at
|
||||
FROM api_keys ORDER BY created_at DESC`,
|
||||
(err, rows) => {
|
||||
if (err) reject(err)
|
||||
else resolve(rows)
|
||||
}
|
||||
)
|
||||
})
|
||||
const keys = await db
|
||||
.select({
|
||||
id: schema.apiKeys.id,
|
||||
name: schema.apiKeys.name,
|
||||
permissions: schema.apiKeys.permissions,
|
||||
rateLimit: schema.apiKeys.rateLimit,
|
||||
isActive: schema.apiKeys.isActive,
|
||||
lastUsed: schema.apiKeys.lastUsed,
|
||||
createdAt: schema.apiKeys.createdAt
|
||||
})
|
||||
.from(schema.apiKeys)
|
||||
.orderBy(desc(schema.apiKeys.createdAt))
|
||||
|
||||
res.json({
|
||||
keys: keys.map(key => ({
|
||||
@@ -38,17 +37,16 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
const apiKey = generateApiKey()
|
||||
const keyHash = hashApiKey(apiKey)
|
||||
|
||||
const result = await new Promise<any>((resolve, reject) => {
|
||||
db.run(
|
||||
`INSERT INTO api_keys (key_hash, name, permissions, rate_limit, is_active, created_at)
|
||||
VALUES (?, ?, ?, ?, 1, datetime('now'))`,
|
||||
[keyHash, name, JSON.stringify(permissions), rate_limit],
|
||||
function(err) {
|
||||
if (err) reject(err)
|
||||
else resolve({ id: this.lastID })
|
||||
}
|
||||
)
|
||||
})
|
||||
const [result] = await db
|
||||
.insert(schema.apiKeys)
|
||||
.values({
|
||||
keyHash: keyHash,
|
||||
name: name,
|
||||
permissions: JSON.stringify(permissions),
|
||||
rateLimit: rate_limit,
|
||||
isActive: true
|
||||
})
|
||||
.returning({ id: schema.apiKeys.id })
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
@@ -62,16 +60,10 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
} else if (req.method === "DELETE") {
|
||||
const { id } = req.query
|
||||
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
db.run(
|
||||
'UPDATE api_keys SET is_active = 0 WHERE id = ?',
|
||||
[id],
|
||||
(err) => {
|
||||
if (err) reject(err)
|
||||
else resolve()
|
||||
}
|
||||
)
|
||||
})
|
||||
await db
|
||||
.update(schema.apiKeys)
|
||||
.set({ isActive: false })
|
||||
.where(eq(schema.apiKeys.id, parseInt(id as string)))
|
||||
|
||||
res.json({ success: true })
|
||||
|
||||
@@ -82,7 +74,5 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
|
||||
} catch (error) {
|
||||
console.error('API keys error:', error)
|
||||
res.status(500).json({ error: "Operation failed" })
|
||||
} finally {
|
||||
db.close()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user