nitrojs / nitrojs/nitro

Support runtime env expansion for database config

Open
#4,545 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

database discussion v2 v3
Dominant language
TypeScript
Stars
11.2k
Forks
899
Avg merge
2d 24m
Merged PRs (30d)
40

Description

Describe the feature

Currently database connector options are embedded into the build output. It means value such as database credentials may be bundled.

We need a way to get the database config dynamically at runtime. Nitro now supports runtime config so we can get a method for free.

const config = {
  url: useRuntimeConfig().database.url || bundledOptions.url
}

Inspired by env expansion, we also have a more flexible solution:

export default defineConfig({
  database: {
    default: {
      connector: "postgresql",
      options: {
        url: "{{DATABASE_URL}}",
      },
    },
  },
});

For non-string options such as port, we may need type metadata from db0 since environment variables are resolved as strings and we need to convert to number.

Should we consider only converting the required keys? Perhaps we also need db0 to provide the list of keys.

Additional information
  • Would you be willing to help implement this feature?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the useRuntimeConfig() path and the database connector options configured through defineConfig, then compare the linked Nitro env-expansion behavior. Done means database values can be resolved at runtime without bundling credentials, bundled options remain a fallback, and non-string values such as ports are converted correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, typescript
Domain
database
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.