Skip to main content
Feathers supports multiple database adapters through its unified configuration system. Database configuration is stored in config/default.json and can be customized per environment.

Supported Databases

The Feathers CLI supports the following databases:
  • MongoDB - Document database using the @feathersjs/mongodb adapter
  • PostgreSQL - Relational database using Knex adapter
  • MySQL/MariaDB - Relational database using Knex adapter
  • SQLite - File-based relational database using Knex adapter
  • Microsoft SQL Server - Enterprise relational database using Knex adapter

MongoDB Configuration

MongoDB uses a connection string format for configuration.
mongodb
string
MongoDB connection string.Format: mongodb://[username:password@]host[:port]/database[?options]Default: mongodb://127.0.0.1:27017/[app-name]

MongoDB Connection Example

config/default.json
{
  "mongodb": "mongodb://127.0.0.1:27017/myapp"
}

MongoDB with Authentication

config/default.json
{
  "mongodb": "mongodb://username:password@localhost:27017/myapp?authSource=admin"
}

MongoDB Service Implementation

The CLI generates a MongoDB connection file:
src/mongodb.ts
import { MongoClient } from 'mongodb'
import type { Db } from 'mongodb'
import type { Application } from './declarations'

declare module './declarations' {
  interface Configuration {
    mongodbClient: Promise<Db>
  }
}

export const mongodb = (app: Application) => {
  const connection = app.get('mongodb') as string
  const database = new URL(connection).pathname.substring(1)
  const mongoClient = MongoClient.connect(connection)
    .then(client => client.db(database))

  app.set('mongodbClient', mongoClient)
}

SQL Databases (Knex)

SQL databases (PostgreSQL, MySQL, SQLite, MSSQL) use Knex as the query builder. The configuration varies slightly by database type.

PostgreSQL Configuration

postgresql
object
PostgreSQL database configuration.

Connection Object Format

connection.host
string
Database host address.
connection.port
number
Database port number.
connection.user
string
Database username.
connection.password
string
Database password.
connection.database
string
Database name.

MySQL Configuration

mysql
object
MySQL/MariaDB database configuration.

SQLite Configuration

sqlite
object
SQLite database configuration.

Microsoft SQL Server Configuration

mssql
object
Microsoft SQL Server database configuration.

Configuration Examples

{
  "postgresql": {
    "client": "pg",
    "connection": "postgres://postgres:password@localhost:5432/myapp"
  }
}

Knex Service Implementation

For SQL databases, the CLI generates a Knex connection file:
src/postgresql.ts
import knex from 'knex'
import type { Knex } from 'knex'
import type { Application } from './declarations'

declare module './declarations' {
  interface Configuration {
    postgresqlClient: Knex
  }
}

export const postgresql = (app: Application) => {
  const config = app.get('postgresql')
  const db = knex(config!)

  app.set('postgresqlClient', db)
}

Migrations

When using Knex (SQL databases), the CLI sets up migration support:

Running Migrations

# Run all pending migrations
npm run migrate

# Create a new migration
npm run migrate:make migration_name

Migration Configuration

The CLI generates a knexfile.ts that loads configuration from your app:
knexfile.ts
import { app } from './src/app'

// Load database connection info from app configuration
const config = app.get('postgresql')

module.exports = config

Environment Variables

Store database credentials in environment variables for security:
config/custom-environment-variables.json
{
  "postgresql": {
    "connection": "DATABASE_URL"
  },
  "mongodb": "MONGODB_URL"
}
Then set the environment variable:
export DATABASE_URL="postgres://user:password@localhost:5432/myapp"
export MONGODB_URL="mongodb://localhost:27017/myapp"

Connection Testing

Test your database connection:
// In a service or hook
const db = await app.get('mongodbClient')
const collections = await db.listCollections().toArray()
console.log('Connected to MongoDB:', collections.length, 'collections')

Best Practices

Configure appropriate pool sizes for your application’s load. Start with defaults and adjust based on monitoring.
Never commit database credentials to version control. Use environment variables and custom-environment-variables.json.
Always use migrations to manage database schema changes. This ensures consistency across environments.
Use separate databases for development, testing, and production. Configure them in environment-specific files.
Implement proper error handling for database connection failures and add retry logic where appropriate.