Founderflow Logo

Database Models

Understanding the database schema and models used in the Founderflow Boilerplate. Learn about the data structure, relationships, and how to work with the database.

Database Features

MongoDB

NoSQL document database for flexible data storage

Mongoose

ODM for MongoDB with schema validation

Indexing

Optimized queries with proper database indexing

Validation

Schema validation for data integrity

Relationships

Document references for data relationships

Type Safety

TypeScript definitions for all models

User Model

The main user model that stores user information and subscription details:

models/user.ts
interface User {
id: string,
emailAddress: string[],
firstName: string,
lastName: string,
picture: string,
stripeCustomerId: string,
lemonsqueezyCustomerId: string,
credits: number,
plan: {
id: string,
type: string,
name: string,
price: string,
isPremium: boolean
}
}

Authentication Fields

  • • id - Unique user identifier
  • • emailAddress - User's email addresses
  • • firstName/lastName - User's name
  • • picture - Profile picture URL

Subscription Fields

  • • stripeCustomerId - Stripe customer ID
  • • lemonsqueezyCustomerId - Lemon Squeezy ID
  • • credits - Available user credits
  • • plan - Current subscription plan

Payment History Model

Tracks all payment transactions and subscription changes:

models/payment-history.ts
interface PaymentHistory {
userId: string,
provider: "stripe" | "lemonsqueezy",
status: "pending" | "succeeded" | "failed",
amount: number,
currency: string,
planId: string,
creditsAllocated: number,
stripePaymentIntentId?: string,
lemonsqueezyOrderId?: string
}

Payment Tracking Fields:

userId: Reference to the user
provider: Payment provider used
status: Payment status
amount: Payment amount
planId: Associated plan
creditsAllocated: Credits granted

Additional Models

Feedback Model

Collects and stores user feedback and suggestions:

Fields:

  • • userId (optional)
  • • message
  • • rating
  • • category
  • • createdAt

Purpose:

  • • User feedback collection
  • • Product improvement insights
  • • Customer satisfaction tracking
  • • Support ticket integration

Leads Model

Tracks potential customers and marketing leads:

Fields:

  • • email
  • • name (optional)
  • • source
  • • status
  • • createdAt

Purpose:

  • • Lead generation tracking
  • • Marketing campaign analysis
  • • Conversion tracking
  • • Email list building

Database Connection

The database connection is configured in lib/db.ts:

lib/db.ts
import mongoose from 'mongoose';

const MONGODB_URI = process.env.MONGODB_URI;

if (!MONGODB_URI) {
throw new Error('Please define the MONGODB_URI environment variable');
}

const connectDB = async () => {
try {
await mongoose.connect(MONGODB_URI);
console.log('MongoDB connected successfully');
} catch (error) {
console.error('MongoDB connection error:', error);
process.exit(1);
}
};

Best Practices

Schema Validation

Always define proper schemas with validation rules to ensure data integrity.

const userSchema = new mongoose.Schema({
email: { type: String, required: true, unique: true },
credits: { type: Number, default: 0, min: 0 }
});

Indexing

Create indexes for frequently queried fields to improve performance.

userSchema.index({ email: 1 });
userSchema.index({ 'plan.type': 1 });

Error Handling

Implement proper error handling for database operations.

try {
const user = await User.findById(userId);
} catch (error) {
console.error('Database error:', error);
throw new Error('User not found');
}