fix: lazy DB initialization — useDb() called inside handlers, not at import
useRuntimeConfig() and better-sqlite3 were being called at module top-level, which crashes during Nitro server startup. Now all DB access is lazy via useDb(), and auth uses process.env directly. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import { createHash } from 'crypto'
|
||||
import db from '../../utils/db'
|
||||
import { useDb } from '../../utils/db'
|
||||
|
||||
export default defineEventHandler((event) => {
|
||||
const db = useDb()
|
||||
const id = getRouterParam(event, 'id')
|
||||
const ip = getRequestIP(event, { xForwardedFor: true }) || 'unknown'
|
||||
const voterHash = createHash('sha256').update(ip + ':' + id).digest('hex')
|
||||
@@ -10,7 +11,6 @@ export default defineEventHandler((event) => {
|
||||
db.prepare('INSERT INTO vote_log (idea_id, voter_hash) VALUES (?, ?)').run(id, voterHash)
|
||||
db.prepare('UPDATE ideas SET votes = votes + 1 WHERE id = ?').run(id)
|
||||
} catch {
|
||||
// UNIQUE constraint = already voted
|
||||
throw createError({ statusCode: 409, statusMessage: 'Already voted' })
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user