Database Integration
Complete guide to MongoDB integration with Mongoose ODM. Learn how to set up, configure, and use the database in your Founderflow Boilerplate application.
Overview
MongoDB + Mongoose
The boilerplate uses MongoDB as the primary database with Mongoose ODM for schema validation, type safety, and easy data manipulation. This combination provides flexibility and scalability for modern applications.
Schema Validation
Automatic data validation and type checking
TypeScript Support
Full type safety with Mongoose schemas
Connection Pooling
Optimized database connections for performance
Middleware Support
Pre/post hooks for data processing
Indexing
Automatic index creation for optimal queries
Error Handling
Comprehensive error handling and logging
Setup & Configuration
Environment Variables
Configure your MongoDB connection using environment variables:
Database Connection
The database connection is handled in the lib/mongodb.ts file:
Database Models
User Model
The main user model with authentication and subscription data:
Payment History Model
Track payment transactions and subscription changes:
Usage Examples
Creating a User
Querying Users
Best Practices
Connection Management
- • Use connection pooling for better performance
- • Always close connections in production
- • Handle connection errors gracefully
- • Use environment variables for connection strings
Schema Design
- • Use appropriate data types
- • Add validation rules
- • Create indexes for frequently queried fields
- • Use references for related data
Query Optimization
- • Use select() to limit returned fields
- • Implement pagination for large datasets
- • Use lean() for read-only operations
- • Monitor query performance
Error Handling
- • Wrap database operations in try-catch
- • Log errors for debugging
- • Return meaningful error messages
- • Handle validation errors separately
Next Steps
Now that you understand the database integration, explore these related areas: