Skip to main content
The 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 generate connection
Alias: feathers g connection

Interactive Prompts

When you run this command, you’ll be prompted for:

Database Type

database
string
required
The type of database to connect toChoices:
  • sqlite - SQLite (file-based, no server required)
  • mongodb - MongoDB
  • postgresql - PostgreSQL
  • mysql - MySQL/MariaDB
  • mssql - Microsoft SQL Server
  • other - Other database via custom connection

Connection String

connectionString
string
required
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

name
string
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:
1

Install database adapter

Installs the appropriate Feathers database adapter package:
  • @feathersjs/knex for SQL databases (PostgreSQL, MySQL, SQLite, MSSQL)
  • @feathersjs/mongodb for MongoDB
2

Install database driver

Installs the database-specific driver:
  • pg for PostgreSQL
  • mysql2 for MySQL
  • better-sqlite3 for SQLite
  • tedious for MSSQL
  • mongodb for MongoDB
3

Update configuration

Adds the connection string to your configuration files:
  • config/default.json - Default configuration
  • config/production.json - Production overrides
4

Create database client

Creates a new client file at src/{name}.ts that initializes the database connection
5

Register in application

Updates src/app.ts to configure and use the database connection

Example: Adding PostgreSQL

$ feathers generate connection
? Which database are you connecting to? PostgreSQL
? Enter the database connection string: postgres://user:pass@localhost:5432/mydb
? Enter a name for this connection: (postgres) 
Generated files:
src/postgres.ts
import knex from 'knex'
import type { Application } from './declarations'

export const postgres = (app: Application) => {
  const db = knex({
    client: 'pg',
    connection: app.get('postgres')
  })

  app.set('postgresClient', db)
}
Updated configuration:
config/default.json
{
  "postgres": "postgres://user:pass@localhost:5432/mydb"
}

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.
// Using different connections
const pgClient = app.get('postgresClient')
const mongoClient = app.get('mongodbClient')

Environment Variables

Connection strings can use environment variables in production:
config/production.json
{
  "postgres": "postgres://$POSTGRES_USER:$POSTGRES_PASSWORD@$POSTGRES_HOST:5432/$POSTGRES_DATABASE"
}
Never commit actual credentials to your repository. Use environment variables for sensitive connection information.

Next Steps

Generate a Service

Create a service that uses this database connection

Database Configuration

Learn more about database configuration options

Build docs developers (and LLMs) love