feathers generate connection command adds a new database connection to your application. This command is used to set up additional database connections beyond the initial one configured during app generation.
Usage
feathers g connection
Interactive Prompts
When you run this command, you’ll be prompted for:Database Type
The type of database to connect toChoices:
sqlite- SQLite (file-based, no server required)mongodb- MongoDBpostgresql- PostgreSQLmysql- MySQL/MariaDBmssql- Microsoft SQL Serverother- Other database via custom connection
Connection String
The connection string for the databaseDefault values by database type:
- MongoDB:
mongodb://127.0.0.1:27017/{name} - PostgreSQL:
postgres://postgres:@localhost:5432/{name} - MySQL:
mysql://root:@localhost:3306/{name} - SQLite:
{name}.sqlite - MSSQL:
mssql://root:password@localhost:1433/{name}
Connection Name
Optional name for the connection (used in configuration)If not provided, defaults to the database type (e.g., “mongodb”, “postgres”)
What Gets Generated
Running this command will:Install database adapter
Installs the appropriate Feathers database adapter package:
@feathersjs/knexfor SQL databases (PostgreSQL, MySQL, SQLite, MSSQL)@feathersjs/mongodbfor MongoDB
Install database driver
Installs the database-specific driver:
pgfor PostgreSQLmysql2for MySQLbetter-sqlite3for SQLitetediousfor MSSQLmongodbfor MongoDB
Update configuration
Adds the connection string to your configuration files:
config/default.json- Default configurationconfig/production.json- Production overrides
Create database client
Creates a new client file at
src/{name}.ts that initializes the database connectionExample: Adding PostgreSQL
src/postgres.ts
config/default.json
Multiple Connections
You can run this command multiple times to add connections to different databases. Each connection will have its own configuration key and client file.Environment Variables
Connection strings can use environment variables in production:config/production.json
Next Steps
Generate a Service
Create a service that uses this database connection
Database Configuration
Learn more about database configuration options