Multi-Connection Strategy
Version:1.1.4
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 insideDB_CONNECTION.
Correct:
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
Recommended .env pattern
Keep one selector and all driver-specific settings together:
How mixed-driver runtime works
When a model definesstatic connectionName, that model can use a different connection from the app default.
Useralways targetsmysqlGeoLocationalways targetsmongoDB_CONNECTIONstill acts as the fallback for models that do not pinconnectionName
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-connectionsis SQL-only by design- Mongo must be targeted explicitly with
--mongo - test flows use
DB_TEST_CONNECTIONunless a model or command target overrides it
Stable design recommendation
For most apps, keep this design:- one default app selector
- one default test selector
- all driver credentials present in
.env - explicit per-model
connectionNameonly where mixed-driver runtime is required