Overview
Study Sync uses MongoDB as its primary database with 5 core collections. The schema is designed for flexibility, de-duplication, and performance. Database Name:study_sync
Driver: MongoDB Native Driver 7.0.0
Connection: MongoDB Atlas (cloud-hosted)
Collections
1. users
Stores user profiles, notification preferences, and role information.
Schema
Indexes
src/lib/db.js:39
Schema Validation
Location:src/lib/db.js:74
Sample Document
2. resources
Global resource pool storing YouTube videos, PDFs, articles, and custom links. Resources are de-duplicated by URL.
Schema
Indexes
src/lib/db.js:54
Schema Validation
Location:src/lib/db.js:126
Resource Types
| Type | Description | Metadata Fields |
|---|---|---|
youtube-video | YouTube video | duration, videoId, thumbnail |
pdf | PDF document | pages, minsPerPage |
article | Web article | estimatedMins |
google-drive | Google Drive file | estimatedMins (optional) |
custom-link | Any external link | estimatedMins (optional) |
De-duplication Logic
Location:src/app/api/resources/route.js:238
Sample Document
3. studyplans
Study plan templates created by users. Contains metadata, resource references, and sharing settings.
Schema
Indexes
src/lib/db.js:43
Schema Validation
Location:src/lib/db.js:92
Sample Document
4. instances
User’s personal instances of study plans with deadlines, notes, and progress tracking.
Schema
Indexes
src/lib/db.js:49
Schema Validation
Location:src/lib/db.js:111
Snapshot Pattern
When an instance is created, it snapshots the currentresourceIds from the study plan. This ensures instances remain independent even if the original plan is modified.
Implementation: src/app/api/instances/route.js:174
Sample Document
5. userprogresses
Global progress tracking for resources. Progress is tracked per (userId, resourceId) pair, not per instance.
Schema
Indexes
src/lib/db.js:60
Schema Validation
Location:src/lib/db.js:135
Global Progress Pattern
Progress is global across all instances. If a user marks a resource complete in Instance A, it appears complete in Instance B as well. Rationale: Users shouldn’t re-watch a video they’ve already completed just because they started a new instance. Implementation:src/app/api/user-progress/route.js:75
Sample Document
Relationships
Helper Functions
ObjectId Conversion
Location:src/lib/db.js:25
Resource Duration Calculation
Location:src/lib/db.js:156
Index Management
Indexes are created automatically on application startup to ensure optimal query performance. Location:src/lib/db.js:35
Migration Considerations
When adding new fields to existing documents:- Default values are handled in schema validation functions
- Optional fields use
||fallback patterns - Backward compatibility is maintained (e.g.,
instance.snapshotResourceIds || plan.resourceIds)
src/lib/auth.js:29