What is Convex?
Convex is a real-time backend platform that provides:- Real-time Synchronization: Automatic data sync across all clients
- Type Safety: End-to-end TypeScript support
- Serverless Functions: Built-in backend logic execution
- Automatic Caching: Optimized data fetching
- Live Queries: Data updates automatically without polling
Why Convex for Your Bible?
Your Bible uses Convex to manage:- Personal Collections: Save and organize favorite Bible verses
- Chapter Notes: Create and edit notes with real-time sync
- AI-Generated Stories: Store and manage generated stories
- Real-time Updates: Changes sync instantly across devices
While user authentication data is stored in PostgreSQL via Better Auth, all user-generated content (collections, notes, stories) is stored in Convex for optimal real-time performance.
Creating a Convex Account
Sign Up for Convex
Visit convex.dev and sign up using GitHub or Google authentication.
Project Setup
Initialize Convex
In your project directory, initialize Convex:This command will:
- Create a new Convex project (or link to an existing one)
- Generate a
convex/directory if it doesn’t exist - Deploy your schema to Convex
- Start the development sync
Select or Create Project
The CLI will prompt you to either:
- Create a new Convex project
- Link to an existing project
Database Schema
Your Bible uses the following Convex schema defined inconvex/schema.ts:
Schema Breakdown
Collections Table
Stores user-created verse collections:- name: Collection name (e.g., “Favorite Psalms”)
- userId: ID of the user who created the collection
CollectionVerses Table
Stores individual verses within collections:- bibleId: Bible translation ID
- verseId: Unique verse identifier
- chapterId: Chapter identifier
- verseText: The actual verse text
- collectionId: Reference to parent collection
- Index:
by_collection_idfor efficient verse lookups
Notes Table
Stores user notes for Bible chapters:- chapterId: Chapter this note belongs to
- content: Rich text content of the note
- userId: ID of the user who created the note
- Index:
by_chapter_idfor efficient note retrieval
Stories Table
Stores AI-generated stories:- title: Story title
- bibleId: Source Bible translation
- chapterId: Source chapter
- chapterReference: Human-readable reference (e.g., “John 3”)
- userId: ID of the user who created the story
- perspective: Narrative perspective used
- setting: Story setting description
- tone: Emotional tone of the story
- storyLength: Length specification (short/medium/long)
- story: The generated story content
- Index:
by_user_idfor efficient user story lookups
Indexes are crucial for query performance. The schema uses indexes to optimize common queries like fetching all verses in a collection or all stories by a user.
Deploying Schema Changes
When you modify the schema inconvex/schema.ts:
During Development
- Save your changes to
convex/schema.ts - The
convex devprocess automatically detects changes - Schema is deployed to your development environment
- Changes are immediately available
For Production
Real-Time Data Sync
Convex automatically synchronizes data across all connected clients in real-time.How It Works
- Client Subscribes: Component queries data using Convex hooks
- Data Updates: When data changes on the server
- Automatic Sync: All subscribed clients receive updates instantly
- UI Updates: React components re-render with new data
Example: Querying Collections
Example: Creating a Collection
Development Workflow
Daily Development
-
Start Convex Dev:
-
Start Your App:
- Make Changes: Edit schema or functions as needed
- Auto-Deploy: Convex automatically deploys changes
Adding New Tables
Adding Indexes
Indexes improve query performance:Add indexes for fields you frequently query or filter by. Indexes are automatically maintained by Convex.
Convex Dashboard
The Convex Dashboard provides:- Data Browser: View and edit data in your tables
- Logs: Real-time function execution logs
- Metrics: Performance and usage statistics
- Schema Viewer: Visual representation of your schema
- Environment Management: Switch between dev and prod
Useful Dashboard Features
- Query Data: Run queries directly in the dashboard
- Monitor Functions: See function execution times and errors
- View Logs: Debug issues with detailed logs
- Manage Deployments: View and manage all deployments
Best Practices
Schema Design
- Use Indexes: Add indexes for commonly queried fields
- Denormalize When Needed: Store duplicate data for faster reads
- Plan for Growth: Design schema with scalability in mind
- Use References: Link tables using Convex IDs
Query Optimization
- Limit Results: Use
.take()to limit query results - Filter Early: Apply filters before sorting or pagination
- Use Indexes: Ensure queries use available indexes
- Paginate Long Lists: Don’t load all data at once
Security
- Validate Inputs: Always validate data in mutations
- Check Authorization: Verify user permissions in functions
- Sanitize Data: Clean user input before storage
- Use Server Functions: Keep sensitive logic on the server
Troubleshooting
Schema Deployment Fails
If schema deployment fails:- Check for syntax errors in
schema.ts - Verify all imports are correct
- Ensure no breaking changes to existing tables
- Review error messages in the console
Data Not Syncing
If data isn’t updating in real-time:- Verify
VITE_CONVEX_URLis correct - Check browser console for connection errors
- Ensure
convex devis running - Verify queries are using the correct API endpoints
Connection Issues
If Convex won’t connect:- Check your internet connection
- Verify environment variables are set
- Try logging out and back into Convex CLI
- Check Convex status page for outages
Performance Issues
If queries are slow:- Add indexes for frequently queried fields
- Reduce the amount of data returned
- Use pagination for large datasets
- Check the Convex dashboard for slow functions