Skip to content

fix(db): lazy URL resolution for Docker/K8s deployments#790

Merged
atinux merged 1 commit intonuxt-hub:mainfrom
onmax:fix/lazy-db-url-resolution
Jan 21, 2026
Merged

fix(db): lazy URL resolution for Docker/K8s deployments#790
atinux merged 1 commit intonuxt-hub:mainfrom
onmax:fix/lazy-db-url-resolution

Conversation

@onmax
Copy link
Copy Markdown
Contributor

@onmax onmax commented Jan 20, 2026

Closes #777

Summary

Allow building without DATABASE_URL when applyMigrationsDuringBuild: false. Generate lazy Proxy pattern for postgres-js, neon-http, mysql2, libsql on non-CF deployments. Enables Docker multi-client scenario: build once, deploy many with different DATABASE_URLs.

StackBlitz

Link Expected
Bug nuxthub-777 ❌ Build fails
Fix nuxthub-777-fixed ✅ Build succeeds

CLI Reproduction

git clone --depth 1 --filter=blob:none --sparse https://github.com/onmax/repros.git
cd repros && git sparse-checkout set nuxthub-777
cd nuxthub-777 && pnpm i && pnpm build

Verify fix

git sparse-checkout add nuxthub-777-fixed
cd ../nuxthub-777-fixed && pnpm i && pnpm build
cat .nuxt/hub/db.mjs  # Shows lazy env resolution

The -fixed folder uses pnpm patch to apply the fix locally.

Related

Context

Details

Kenshi:
Hey,
I recently switched to Nuxt-Hub for the simple database implementation and S3 support, but I have run into an issue I can't figure out how to work around. I'm working with Docker to deploy my Nuxt app and build my own Docker images for each one. After building a Nuxt app that's using the Nuxt Hub without setting anything besides the db: 'postgresql', it causes an error to occur on launch Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@electric-sql/pglite' imported from /prod/server/node_modules/drizzle-orm/pglite/driver.js. I tried to follow the documentation and had set the driver to postgres-js but that causes another issue when building the app postinstall: ERROR postgres-js driver requires DATABASE_URL, POSTGRES_URL, or POSTGRESQL_URL environment variable. It is not suitable for me to set the ENV variables on build as the app is being deployed for multiple clients and these change on every Nuxt Instance.
Could you suggest any workaround for this issue? If there are any.
Thanks in advance.

EDIT: I have also tried setting the applyMigrationsDuringBuild to false but nothing changed

NuxtHub needs to know the database env in order to apply the database migrations at build time
Kenshi

Yesterday at 8:54 AM
Yeah, I'm aware of that, that's why I tried to disable the migration at build as stated in the docs.
Atinux

Yesterday at 8:57 AM
Could you try to set a fake DATABASE_URL for the build to see if it passes?
If you disabled migrations it shouldn’t call the database anyway
Kenshi

Yesterday at 8:59 AM
We did try that, but apparently nuxt hub hard codes the variable into the cooked build instead of reading it from the env file
Feels counter intuitive to me but it is what it is
It's for all of the variables as well not only the database
Atinux

Yesterday at 10:52 AM
It should be possible to fix this
How do you plan to manage the migrations @kenshi ?
Atinux

Yesterday at 11:22 AM
what do you think of this @onmax ?
onmax

Yesterday at 1:09 PM
Connection URL gets hardcoded into generated hub/db.mjs at build time via JSON.stringify(connection). Cloudflare deployments (D1, Hyperdrive) work because they use lazy runtime resolution:

const binding = process.env.DB || globalThis.env?.DB

Non-CF deployments (postgres-js, neon-http, mysql2) don't have this.

I propose always use lazy resolution for non-CF drivers too. Zero config changes, Docker multi-client deployments would work:

function getDB() {
const url = process.env.DATABASE_URL || process.env.POSTGRES_URL
if (!url) throw new Error('DATABASE_URL required')
return drizzle({ connection: url, schema })
}

Also: only require DATABASE_URL at build time if applyMigrationsDuringBuild: true.

Should I open a PR @atinux ? Would close #777 as well.

Did i miss something?
Kenshi

Yesterday at 1:32 PM
We have it run checks and in case of changes migrations at the app launch. It might not be the cleanest solution but it works for our use case as of right now.
Vetro

Yesterday at 2:07 PM
Hey, I work with @kenshi . It would be great if migrations could be done on application deploy, not just on build.

Also I found, that probably whole hub's config is baked into nuxt's nitro.mjs on build... blob config what was in env file (api keys for S3) is also here
Atinux

Yesterday at 2:42 PM
sounds good!

@vercel
Copy link
Copy Markdown

vercel bot commented Jan 20, 2026

@onmax is attempting to deploy a commit to the NuxtLabs Team on Vercel.

A member of the Team first needs to authorize it.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Jan 20, 2026

npm i https://pkg.pr.new/@nuxthub/core@790

commit: 4bcd403

@onmax onmax force-pushed the fix/lazy-db-url-resolution branch from 98ad0d4 to 48a39f6 Compare January 20, 2026 15:35
@TheBITLINK
Copy link
Copy Markdown

Can the libsql driver to be considered as well? (with another variable for auth token).

I just ran into this issue while trying to build a generic container image for Kubernetes deployments and it defaults to a local database (which instantly crashes on runtime since the container filesystem is read only)

@onmax onmax force-pushed the fix/lazy-db-url-resolution branch 10 times, most recently from 180b34a to dff01b5 Compare January 21, 2026 08:12
@onmax onmax changed the title fix(db): lazy URL resolution for non-CF deployments fix(db): lazy URL resolution for Docker/K8s deployments Jan 21, 2026
@onmax onmax force-pushed the fix/lazy-db-url-resolution branch from dff01b5 to 4bcd403 Compare January 21, 2026 08:26
@atinux atinux merged commit baf34c5 into nuxt-hub:main Jan 21, 2026
7 of 9 checks passed
@onmax onmax deleted the fix/lazy-db-url-resolution branch January 21, 2026 10:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

POSTGRES binding not found with DATABASE_URL

3 participants