Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/api/comments/[[...comment]]/route.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { NextResponse } from 'next/server'
import { NextComment } from '@fuma-comment/server/next'
import { auth, storage } from '@/lib/comment-config'
import { ensureDbTables } from '@/lib/db'

const { GET: _GET, POST: _POST, PATCH: _PATCH, DELETE: _DELETE } = NextComment({ auth, storage })

async function withParams(handler, req, ctx) {
try {
await ensureDbTables()
} catch (e) {
console.error('[comments] DB init failed:', e.message)
return NextResponse.json({ error: 'Database unavailable' }, { status: 503 })
}
const params = await ctx.params
const normalized = { comment: params?.comment ?? [] }
return handler(req, { ...ctx, params: Promise.resolve(normalized) })
Expand Down
2 changes: 0 additions & 2 deletions lib/comment-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { authOptions } from './auth-options.js'
import { db, ensureDbTables } from './db.js'
import { comments, rates, roles, fumaUsers } from './comment-schema.js'

await ensureDbTables()

const nextAuthAdapter = createNextAuthAdapter(authOptions, { sessionId: 'id' })

export const auth = {
Expand Down
7 changes: 5 additions & 2 deletions lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import { drizzle } from 'drizzle-orm/postgres-js'
import postgres from 'postgres'
import * as schema from './comment-schema.js'

const sql = postgres(process.env.DATABASE_URL, { onnotice: () => {} })
export const db = drizzle(sql, { schema })
const sql = process.env.DATABASE_URL
? postgres(process.env.DATABASE_URL, { onnotice: () => {} })
: null
export const db = sql ? drizzle(sql, { schema }) : null

let initialized = false
export async function ensureDbTables() {
if (!sql) return
if (initialized) return
initialized = true
await sql`
Expand Down