Overview
The Telegram Subscription Manager uses MongoDB as its database with Prisma ORM for type-safe database access. This guide covers setting up MongoDB Atlas (cloud-hosted MongoDB) and initializing your database schema.Prerequisites
- A valid email address
- Credit card for verification (free tier available, no charges)
Create MongoDB Atlas Account
Sign Up for MongoDB Atlas
- Go to MongoDB Atlas
- Click “Try Free” or “Sign Up”
- Sign up with:
- Google account (recommended)
- Email and password
- GitHub account
- Complete the registration
Create Organization and Project
After signing up, Atlas will prompt you to create an organization and project:
- Organization Name: Your company or personal name
- Project Name:
telegram-subscription-bot(or your preferred name) - Click “Next” or “Create Project”
Organizations can contain multiple projects. One project is sufficient for this bot.
Create Database Cluster
Build a Cluster
- Click “Build a Database” or “Create”
- Choose deployment option:
- Free Tier (Recommended for Testing)
- Paid Tiers (For Production)
M0 Cluster (FREE):
- 512 MB storage
- Shared RAM
- Perfect for development and testing
- No credit card required
- Provider: AWS, Google Cloud, or Azure (any works)
- Region: Choose closest to you or Vercel’s servers (recommended:
us-east-1) - Click “Create”
Configure Security - Database Access
Create a database user:
- Go to Database Access (in the Security section of sidebar)
- Click “Add New Database User”
- Choose authentication method:
- Authentication Method: Password (recommended)
- Set credentials:
- Username:
botuser(or your preferred username) - Password: Click “Autogenerate Secure Password” and copy it
- Username:
- Database User Privileges:
- Select “Read and write to any database”
- Click “Add User”
Configure Security - Network Access
Allow connections from Vercel:
- Go to Network Access (in the Security section)
- Click “Add IP Address”
- Choose “Allow Access from Anywhere”:
- IP Address:
0.0.0.0/0 - Comment:
Vercel and development
- IP Address:
- Click “Confirm”
0.0.0.0/0 allows connections from any IP, which is required for Vercel’s serverless functions. MongoDB Atlas still requires username/password authentication.Get Connection String
- Go to Database (in the Deployment section)
- Click “Connect” on your cluster
- Choose “Connect your application”
- Select:
- Driver: Node.js
- Version: 4.1 or later
- Copy the connection string:
- Replace
<password>with your actual password (the one you generated/saved earlier) - Add database name before the
?:
Configure Environment Variable
Add the connection string to your environment variables:Local Development
Add to.env.local:
.env.local
Wrap the URL in quotes to handle special characters in the password.
Vercel (Production)
Add to Vercel environment variables:- Go to Project Settings → Environment Variables
- Add new variable:
- Name:
DATABASE_URL - Value: Your connection string
- Environments: Select all (Production, Preview, Development)
- Name:
- Click “Save”
Prisma Schema
Your database schema is defined inprisma/schema.prisma:
The schema also includes
Mt5Setup and ConversationState models for premium features like MT5 copier integration.Initialize Database
Install Dependencies
Ensure Prisma is installed:This installs:
@prisma/client- Prisma Client for database queriesprisma- Prisma CLI for migrations and management
Generate Prisma Client
Generate the TypeScript types and client:This creates the Prisma Client based on your schema in
prisma/schema.prisma.Run this command whenever you modify the Prisma schema.
Push Schema to Database
For MongoDB, use This will:
db push to sync your schema:- Connect to your MongoDB Atlas cluster
- Create the database if it doesn’t exist
- Create collections for each model
- Create indexes defined in the schema
If you see this output, your database is ready!
Database Indexes
The schema includes optimized indexes for common queries:| Model | Index | Purpose |
|---|---|---|
Subscription | telegramUserId | Fast user lookup |
Subscription | expiresAt, isRemoved | Efficient cron job queries |
Subscription | planType | Filter by subscription plan |
PromoCode | isActive | Query active promo codes |
PromoCode | expiresAt | Find expired promo codes |
User | telegramUserId | Fast user authentication |
MongoDB automatically creates these indexes when you run
prisma db push.Testing Database Connection
Create a simple test to verify your connection:Common Prisma Operations
Create Subscription
Find Active Subscription
Find Expired Subscriptions
Update Subscription
Troubleshooting
Connection Timeout Errors
Connection Timeout Errors
Error:
Error: Connection timeoutSolutions:-
Check Network Access:
- Go to MongoDB Atlas → Network Access
- Ensure
0.0.0.0/0is added - Wait 2-3 minutes for changes to propagate
-
Verify credentials:
- Check username and password are correct
- Ensure no special characters are unencoded
- Test connection string in MongoDB Compass
-
Check cluster status:
- Go to Database → Clusters
- Ensure cluster is not paused or provisioning
Authentication Failed
Authentication Failed
Error:
Error: Authentication failedSolutions:-
Verify password:
- Password is case-sensitive
- Check for copy-paste errors
- Ensure
<password>placeholder is replaced
-
Check user exists:
- Go to Database Access
- Verify user is created
- Check user has “Read and write” privileges
-
Special characters in password:
If password has special characters, URL-encode them:
@→%40#→%23%→%25- Or regenerate password without special characters
Database Not Found
Database Not Found
Error: The database will be created automatically.
Error: Database telegram_bot does not existSolution:
MongoDB creates databases on first write. This is normal - just run:Prisma Generate Fails
Prisma Generate Fails
Error during
prisma generateSolutions:-
Clear Prisma cache:
-
Reinstall dependencies:
-
Check schema syntax:
Ensure
prisma/schema.prismahas no syntax errors.
Vercel Build Fails (DATABASE_URL)
Vercel Build Fails (DATABASE_URL)
Error:
Error: DATABASE_URL environment variable not foundSolution:
Prisma needs DATABASE_URL at build time:- Go to Vercel → Project Settings → Environment Variables
- Add
DATABASE_URLwith your MongoDB connection string - Ensure it’s enabled for all environments (Production, Preview, Development)
- Redeploy
The build command
prisma generate requires DATABASE_URL to generate the client.Database Maintenance
Backup Strategy
Free Tier (M0):- No automated backups
- Export data manually via MongoDB Compass or
mongodump
- Automated continuous backups
- Point-in-time recovery
- Scheduled snapshots
Export Data
Using Prisma Studio:- Download MongoDB Compass
- Connect with your connection string
- Export collections to JSON/CSV
Monitor Usage
Check database metrics in MongoDB Atlas:- Go to Database → Metrics
- Monitor:
- Connections
- Operations per second
- Storage usage
- Network traffic
Next Steps
With your database configured, you’re ready to deploy:Deploy to Vercel
Complete the deployment process
Test Your Bot
Verify everything works end-to-end