Skip to content

Variable substitution in DataSource JSON files #2556

@bajtos

Description

@bajtos

To support https://12factor.net/ application loading their database connection strings from ENV variables, we should enhance our DataSource booter to provide variable substitution similar to what https://github.com/strongloop/loopback-boot already provides for LB3 applications.

Example LB3 config file:

// server/datasources.json
{
  "db": {
    "connector": "mysql",
    "url": "${MYSQL_URL}",
  }
}

Proposed format for LB4:

// src/datasources/db.json
{
  "name": "db",
  "connector": "mysql",
  "url": "${ENV.MYSQL_URL}",
}

By using ENV. prefix, we are keeping doors open to add other variable sources in the future, e.g. APP. prefix to access application-level bindings.

To support numeric values, allow the following format (notice the + sign after the opening bracket):

{
  "port": "${+ENV.PORT}",
}

Acceptance criteria

  • A new helper function accepting configuration object loaded from the JSON file, an object with values to use for substitutions (conceptually {ENV: process.env}), and returning modified configuration object with substitutions resolved according to rules described above. Include comprehensive test coverage.
  • Substitution should be applied to values at all levels, including deeply nested properties.
  • Support conversion to numeric values as described above.
  • @loopback/boot recognizes and replaces variables in datasource configuration. Leverage the already implemented helper function.
  • process.env must not be accessed directly from individual booters. Instead, the environment object should be provided via app.boot() arguments. For example, we can add a new property to BootExecutionOptions.
  • Modify examples and CLI templates to pass process.env value to app.boot(), so that variable substitution can be performed.
  • Documentation
  • Blog post

Out of scope

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions