Skip to main content

Multi-Connection Strategy

Version: 1.3.0 Use this page when your app needs more than one database connection at the same time.

Core rule

This ORM supports multiple named connections in one app, but it does not support multiple values inside DB_CONNECTION. Correct:
Incorrect:
Incorrect for the current package contract:
Those custom keys are not part of the current resolver and will be ignored unless you change the package code.

What the package actually supports

  • one active default app selector: DB_CONNECTION
  • one active default test selector: DB_TEST_CONNECTION
  • all driver credential blocks can exist in the same .env
  • models can pin their own static connectionName
  • SQL and Mongo connections can stay open together in one process
Keep one selector and all driver-specific settings together:

How mixed-driver runtime works

When a model defines static connectionName, that model can use a different connection from the app default.
In that setup:
  • User always targets mysql
  • GeoLocation always targets mongo
  • DB_CONNECTION still acts as the fallback for models that do not pin connectionName

When to rely on DB_CONNECTION

Use DB_CONNECTION and DB_TEST_CONNECTION when:
  • your whole app should run on one default driver
  • generated models should inherit one shared default
  • you only switch targets between environments

When to pin connectionName per model

Use explicit static connectionName = "mysql" style declarations when:
  • one part of the app is relational
  • another part is document-first
  • you need one process to talk to SQL and Mongo together
  • you want predictable runtime behavior regardless of environment defaults

CLI behavior

The CLI can target more than one connection, but not all commands treat connections the same way.
  • --all-connections is SQL-only by design
  • Mongo must be targeted explicitly with --mongo
  • test flows use DB_TEST_CONNECTION unless a model or command target overrides it
Examples:

Stable design recommendation

For most apps, keep this design:
  1. one default app selector
  2. one default test selector
  3. all driver credentials present in .env
  4. explicit per-model connectionName only where mixed-driver runtime is required
That keeps the environment simple while still allowing true multi-connection runtime.