Overview
This portfolio uses Convex as a real-time serverless database to track blog post views. Convex provides a type-safe, reactive database that automatically syncs data between your backend and frontend.Why Convex?
- Real-time Updates: View counts update automatically across all connected clients
- Type Safety: Full TypeScript support with generated types
- Serverless: No infrastructure management required
- Generous Free Tier: Perfect for personal portfolios
- Zero Configuration: Deploy with a single command
Initial Setup
Initialize Convex
Set up Convex in your project:This command will:
- Create a new Convex project (or connect to existing)
- Generate configuration files
- Set up authentication
- Start the development server
Database Schema
Define your database schema inconvex/schema.ts:
convex/schema.ts
Schema Breakdown
| Field | Type | Description |
|---|---|---|
slug | String | Unique identifier for the blog post (e.g., “my-first-post”) |
count | Number | Total number of views for this post |
updatedAt | Number | Timestamp of last view (milliseconds since epoch) |
The
by_slug index enables fast lookups by blog post slug, essential for real-time view tracking.Database Functions
Implement view tracking functions inconvex/views.ts:
convex/views.ts
Function Details
incrementView (Mutation)
Increments the view count for a blog post:
- Queries the database for existing view record by slug
- If exists: increments count and updates timestamp
- If new: creates record with count of 1
- Returns the new view count
Mutations modify data and are strongly consistent. Use them for writes.
getViewCount (Query)
Retrieves the current view count for a blog post:
- Queries the database by slug
- Returns count if exists, otherwise returns 0
Queries are read-only and reactive. Components using queries automatically re-render when data changes.
Frontend Integration
Setup Convex Provider
Wrap your app with the Convex provider:Track Blog Views
Implement view tracking in your blog post component:BlogPost.tsx
Display All Blog Stats
Create a query to fetch all blog post views:convex/views.ts
BlogList.tsx
Environment Variables
The Convex URL is automatically provided when you run
npx convex dev. Copy it from the terminal output.Deployment
Deploy Convex Functions
Deploy your database functions to production:This command:
- Pushes your schema and functions to Convex
- Generates production API credentials
- Provides your production Convex URL
Set Production Environment Variables
Add the production Convex URL to your hosting platform (Netlify, Vercel, etc.):
Database Indexes
Theby_slug index optimizes queries by blog post slug:
Why Indexes Matter
- Performance: Enables O(log n) lookups instead of O(n) scans
- Scalability: Critical as your blog grows
- Cost Efficiency: Reduces function execution time
Always create indexes for fields you frequently query. Convex automatically uses the most efficient index available.
Type Safety
Convex generates TypeScript types automatically. Import them in your frontend:Advanced Features
Pagination
Implement pagination for large datasets:convex/views.ts
Real-time Subscriptions
Convex queries are reactive by default. Components automatically update when data changes:Scheduled Functions
Reset view counts weekly (example):convex/crons.ts
Best Practices
Use Indexes Wisely
Create indexes for all fields you query frequently. Check query performance in the Convex dashboard.
Troubleshooting
Connection Issues
CONVEX_URL environment variable is set correctly and restart your dev server.
Schema Mismatch
npx convex dev to sync your schema changes. Clear your browser cache if issues persist.
Missing Generated Files
npx convex dev to generate type definitions. Ensure convex.json points to the correct directory.
Monitoring and Analytics
Access the Convex dashboard for:- Real-time Logs: Function execution logs and errors
- Usage Metrics: Database reads, writes, and storage
- Function Performance: Execution time and frequency
- Data Browser: Query and modify data directly