Overview
The Walle application uses a multi-database architecture combining PostgreSQL for relational data and MongoDB for document storage. TheDatabaseModule provides a flexible configuration system using NestJS’s dynamic module pattern.
Source: src/app/database/database.module.ts
Database Technologies
PostgreSQL with TypeORM
PostgreSQL is used for relational data, particularly for GPS tracking points with table partitioning. The connection is managed through TypeORM.MongoDB with Mongoose
MongoDB stores document-based data like users and application-specific records. Multiple MongoDB connections are supported for different databases:- DELTA_DISPATCH: Main application database
- AUTHSOFTWARE: Authentication and authorization database
DatabaseModule Structure
The module is decorated with@Global() to make database connections available throughout the application.
PostgreSQL Configuration
PostgreSQL connection is configured using TypeORM’s async configuration pattern. Source:src/app/database/config/database-postgresql.config.ts
getPostgresDatabaseConfig()
Factory function that returns TypeORM configuration using environment variables.Configuration Parameters
Database type (PostgreSQL)
PostgreSQL server hostname (from
POSTGRES_HOST env variable)PostgreSQL server port (from
POSTGRES_PORT env variable)Database user (from
POSTGRES_USER env variable)Database password (from
POSTGRES_PASSWORD env variable)Database name (from
POSTGRES_DB env variable)Automatically load TypeORM entities from modules
Automatically sync database schema (from
POSTGRES_SYNC env variable)Enable SQL query logging
Environment Variables
Required environment variables for PostgreSQL:MongoDB Connections
The DatabaseModule provides factory methods for creating named MongoDB connections using Mongoose.forDeltaDispatchApplication()
Creates a MongoDB connection for the main Delta Dispatch application database.Parameters
MongoDB connection URI (e.g.,
mongodb://localhost:27017/delta_dispatch)Returns
A dynamic module with the configured MongoDB connection exported
Usage Example
forAuthSoftwareApplication()
Creates a MongoDB connection for the authentication software database.Parameters
MongoDB connection URI for the auth database
Usage Example
Using Named Connections
When injecting models from named MongoDB connections, specify the connection name:Dynamic Module Pattern
The DatabaseModule uses NestJS’s dynamic module pattern to provide flexible database configuration:- Static Imports: PostgreSQL is configured once globally
- Factory Methods: MongoDB connections are created on-demand with custom URIs
- Named Connections: Multiple MongoDB databases can coexist
Crypto Polyfill
For Node.js v18 compatibility, the module includes a crypto polyfill:Connection Constants
Database connection names are defined in constants:Next Steps
- Partition Manager - Learn about PostgreSQL table partitioning
- UserService - See how services use MongoDB connections