Knex.js Database Configuration
This document describes the Knex.js configuration for the ChainAlign backend, focusing on its dynamic database configuration capabilities.
Overview
The knexfile.cjs is responsible for defining the database connection settings for different environments (development, test, production). It leverages environment variables to provide dynamic configuration, allowing for flexible deployment and secure handling of credentials.
Configuration by Environment
Development Environment
The development configuration uses PostgreSQL (pg) as the client. All connection parameters are sourced from environment variables, ensuring that sensitive information is not hardcoded and can be easily managed.
- Client:
pg(PostgreSQL) - Connection Parameters (from environment variables):
DB_HOSTDB_PORTDB_USERDB_PASSWORDDB_NAME
- SSL:
rejectUnauthorized: false(for development flexibility) - Pool:
min: 0,max: 20,acquireTimeoutMillis: 60000 - Migrations Directory:
backend/migrations - Seeds Directory:
backend/seeds
Test Environment
The test environment uses better-sqlite3 for an in-memory database, which is ideal for fast and isolated unit/integration tests.
- Client:
better-sqlite3 - Connection: In-memory (
filename: ':memory:') - Use Null As Default:
true - Migrations Directory:
backend/migrations - Seeds Directory:
backend/seeds
Production Environment
The production configuration also uses PostgreSQL (pg) and sources its connection parameters from environment variables, similar to the development environment, but with a more conservative connection pool.
- Client:
pg(PostgreSQL) - Connection Parameters (from environment variables):
DB_HOSTDB_PORTDB_USERDB_PASSWORDDB_NAME
- SSL:
rejectUnauthorized: false - Pool:
min: 2,max: 10 - Migrations Directory:
backend/migrations - Seeds Directory:
backend/seeds
Dynamic Configuration
By utilizing process.env to fetch database credentials and settings, the knexfile.cjs enables:
- Security: Sensitive information is kept out of the codebase.
- Flexibility: Easily switch between different database instances or providers by changing environment variables.
- Environment-Specific Settings: Tailor database behavior (e.g., connection pooling) to the specific needs of each deployment environment.