Conditional socketPath

In config/database.ts I’m trying to set a conditional socket path to be used if it’s running locally and remove it if it’s in production. But I’m running into a problem I think it’s because it’s inside the mysql object but I was wondering if this is a common usecase and if so how you go about fixing it.

const connections = {
    mysql: {
      connection: {
        connectionString: env('DATABASE_URL'),
        host: env('DATABASE_HOST', 'localhost'),
        port: env.int('DATABASE_PORT', 3306),
        database: env('DATABASE_NAME', 'strapi'),
        user: env('DATABASE_USERNAME', 'strapi'),
        password: env('DATABASE_PASSWORD', 'strapi'),
        ssl: env.bool('DATABASE_SSL', false) && {
          key: env('DATABASE_SSL_KEY', undefined),
          cert: env('DATABASE_SSL_CERT', undefined),
          ca: env('DATABASE_SSL_CA', undefined),
          capath: env('DATABASE_SSL_CAPATH', undefined),
          cipher: env('DATABASE_SSL_CIPHER', undefined),
          rejectUnauthorized: env.bool(
            'DATABASE_SSL_REJECT_UNAUTHORIZED',
            true
          ),
        },
      },
      
      pool: { min: env.int('DATABASE_POOL_MIN', 2), max: env.int('DATABASE_POOL_MAX', 10) },
      if (prcess.env.NODE_ENV !== 'production') {
        connection = {
          ...connection,
          socketPath: '/Users/cparrish817/cloudsql/causal-shell-426900-m9:us-west1:azol',
        }
      }
    },

This topic has been created from a Discord post (1256689492203667479) to give it more visibility.
It will be on Read-Only mode here.
Join the conversation on Discord

The way is ok, but I would recommend using plain env vars

Sorry I’m not sure what you mean by plain env vars?

in my local environment I’m using a proxy to access the database and need the socketPath. In product I access the database directly this is why I’m attempting this.

{
…(env(SOME_VAR) && { someVar: … }),
}