Overview
The Global Resource Pool is a core architectural pattern in Study Sync that prevents duplication when the same resource (YouTube video, PDF, article, etc.) is used across multiple study plans.The Problem
Without de-duplication:- Same YouTube video added to 10 different study plans → 10 duplicate database entries
- Wasted storage space
- Inconsistent metadata (e.g., video title updated in one plan but not others)
- Difficulty tracking which resources are most popular
The Solution: URL-Based De-duplication
Architecture
How It Works
- User adds a resource to their study plan
- System checks if a resource with that URL already exists
- If exists: Return the existing resource ObjectId
- If new: Create resource, then return its ObjectId
- Study plan stores only the resource ObjectId in its
resourceIdsarray
Implementation
Database Schema
Collection:resources
Location: src/lib/db.js:54
API Endpoint: Create or Get Resource
Endpoint:POST /api/resources
Location: src/app/api/resources/route.js:84
Flow Diagram
Code Implementation
Single Resource (src/app/api/resources/route.js:238)
src/app/api/resources/route.js:268)
Resource Types
1. YouTube Videos
Type:youtube-video
Metadata Fetching: src/lib/youtube.js
Process:
- Extract video ID from URL
- Call YouTube Data API v3 to fetch:
- Title
- Duration (in seconds)
- Thumbnail URL
- Store metadata in
resource.metadata
src/app/api/resources/route.js:119
2. YouTube Playlists
Type:youtube-playlist (expanded to individual videos)
Process:
- Extract playlist ID from URL
- Fetch all videos in playlist via YouTube API
- Create individual
youtube-videoresources for each video - De-duplicate: skip videos already in database
src/app/api/resources/route.js:141
3. PDF Documents
Type:pdf
Metadata (user-provided):
pages: Number of pagesminsPerPage: Estimated minutes per page (default: 3)
42 pages × 4 mins = 168 minutes
Implementation: src/app/api/resources/route.js:172
4. Articles
Type:article
Metadata:
estimatedMins: User-estimated reading time
5. Google Drive & Custom Links
Types:google-drive, custom-link
Metadata: Optional estimatedMins
Implementation: src/app/api/resources/route.js:212
Study Plan References
Storing Resource IDs
Collection:studyplans
Field: resourceIds: [ObjectId]
Location: src/lib/db.js:97
Adding Resources to Plan
When a user adds a resource:- Create/get resource via
POST /api/resources - Update study plan via
PUT /api/study-plans/:id - Append resource ID to
plan.resourceIds
Fetching Plan Resources
API Endpoint:GET /api/study-plans/:id
Query:
plan.resourceIds
Implementation: src/app/api/resources/route.js:59
Benefits of Global Resource Pool
1. Storage Efficiency
Before:2. Metadata Consistency
If YouTube video title changes, one database update affects all study plans.3. Analytics Potential
- Find most popular resources (count references)
- Track which resources appear in successful study plans
- Recommend resources based on patterns
4. Bulk Operations
When importing a playlist with 50 videos:- Check all 50 URLs against database in one query
- Insert only new videos (maybe 10 are already in DB)
- Save API quota and processing time
Edge Cases
1. Same Video, Different URLs
Problem:youtube.com/watch?v=abc vs youtu.be/abc are the same video but different URLs.
Solution: Normalize URLs before storage (future enhancement).
2. URL Changes
Problem: Video moves to a different URL. Current: Creates a new resource. Old resource remains orphaned. Future: Implement URL redirect detection or manual merge tools.3. Deleted Resources
Problem: YouTube video is deleted, but resource still in DB. Current: Resource remains, but video is inaccessible. Future: Periodic validation job to check resource availability.Duration Calculation
Helper Function:src/lib/db.js:156
Future Enhancements
1. Resource Versioning
Track when resource metadata changes:2. Resource Tagging
3. User-Contributed Metadata
Allow users to suggest better titles or durations:4. Smart Recommendations
Summary
Key Principles:- Resources are stored once in a global pool
- URL uniqueness prevents duplicates
- Study plans reference resources by ObjectId
- Automatic de-duplication on creation
- Metadata consistency across all plans
src/app/api/resources/route.js- Resource creation endpointsrc/lib/db.js- Schema definitions and helperssrc/lib/youtube.js- YouTube API integration