Close SQLite connection in Next.js API routes for `next build` command to work?

So I got Database is locked error when I'm doing next build like this when I'm building docker image. the command underneath is next build which builds for production. this command calls all api routes:

46.40 ahoy!!
46.43 ahoy!!
46.46 ahoy!!
46.47 SqliteError: database is locked


my file is:

import sqlite from 'better-sqlite3'
import { drizzle } from 'drizzle-orm/better-sqlite3'
import { isProduction } from 'std-env'

import { env } from '@/app/lib/env'

console.log(`ahoy!!`)

const url = isProduction
  ? `/data/${env.SQLITE_DATABASE_NAME}`
  : `${env.SQLITE_DATABASE_NAME}`

const client = sqlite(url)
client.pragma('journal_mode = WAL') // see https://github.com/WiseLibs/better-sqlite3/blob/master/docs/performance.md

export const db = drizzle(client)


how do i solve this?

there is a .close() on client but not on
db
? there's a singleton pattern which i haven't tried for sqlite.

what would you do?

i believe this should work & it does locally at least but not with docker.

the problem is it sometimes builds perfectly & randomly fails when i delete previously build. so its hard to debug.
Was this page helpful?