Support runtime env expansion for database config

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

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

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

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.

Description

database discussion runtime-config
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?
Dominant language
TypeScript
Stars
11.5k
Forks
968
Avg merge
4d 9h
Merged PRs (30d)
64

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

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.

More from nitrojs/nitro

All issues in nitrojs/nitro

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.