Skip to content

Dynamic database connection config #18474

@evan-thinksync

Description

@evan-thinksync

Bug report

Required System information

  • Node.js version: v18.14.2
  • Yarn version: 1.22.19
  • Strapi version: 4.14.4
  • Database: postgres
  • Operating system: darwin-arm64
  • Is your project Javascript or Typescript: Typescript

Describe the bug

Knex supports dynamic database connection by providing a function for the database connection config. It's been noted that the data inside of the strapi db config is passed directly to knex however when I use a function for the strapi db connection config, I get the following error
TypeError: Cannot read properties of undefined (reading 'schema')
from the getSchemaName() function resulting in a failed startup.

The issue is that this.connection.client.connectionSettings is undefined when providing a function as the connection config.

Steps to reproduce the behavior

  1. Provide a function as the strapi db connection config. i.e.
export default ({ env }) => ({
  connection: {
    client: 'postgres',
    connection: async () => {
      const {
        token,
        tokenExpiration
      } = await someCallToGetTheToken();

      return {
        host : 'your_host',
        port : 5432,
        user : 'your_database_user',
        password : token,
        database : 'myapp_test',
        expirationChecker: () => {
          return tokenExpiration <= Date.now();
        }
      };
    }
  }
})
  1. Start strapi app

Expected behavior

Strapi to support dynamic database connection via db connection config function.

Screenshots

N/A

Code snippets

N/A

Additional context

N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Fixed/Shipped

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions