Server Storage Logic
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
The server storage logic in the CoolTools platform encompasses a hybrid architecture designed for high-performance phone data retrieval and persistence. It utilizes a PostgreSQL database managed via Drizzle ORM for structured data storage, alongside an optimized in-memory caching layer for external API interactions.
This system is responsible for managing a vast database of phone specifications, historical price tracking, user-saved comparisons, and benchmark scores. By combining persistent relational storage with a transient caching mechanism, the system ensures low-latency access to frequently requested phone details while maintaining a reliable record of historical data.
Sources: cooltools/shared/schema.ts, cooltools/lib/phoneApi.ts:4-8
Data Persistence Layer
The project uses Drizzle ORM to define and interact with a PostgreSQL schema. This layer handles the long-term storage of device specifications and user-generated content.
Database Schema Overview
The relational schema is centered around the phones table, which serves as the primary repository for device metadata. Related tables track volatile or supplemental data such as price history and benchmarks.
The diagram above illustrates the relationships between core device data and supplemental tracking tables.
Sources: cooltools/shared/schema.ts:4-105
Core Storage Tables
| Table | Description | Key Fields |
|---|---|---|
phones | Main storage for phone specifications. | brand, model, slug, full_specs (JSONB) |
comparisons | User-saved phone comparison lists. | userId, phoneIds (JSONB) |
price_history | Historical price data for tracking trends. | phoneId, price, recordedAt |
benchmarks | Performance metrics (AnTuTu, Geekbench). | benchmarkType, score, details |
Sources: cooltools/shared/schema.ts:4-105
API Integration and Caching Logic
To supplement local storage, the system interfaces with an external mobile specification API. To prevent rate limiting and reduce latency, a specialized caching and deduplication layer is implemented.
In-Memory Cache Mechanism
The application implements a Map-based cache with a Time-To-Live (TTL) of 5 minutes. This is primarily used for external API responses.
The flow shows how the system handles request deduplication and cache validation to optimize API usage.
Sources: cooltools/lib/phoneApi.ts:7-22, cooltools/lib/phoneApi.ts:168-196
Request Deduplication
The getPhoneDetails function utilizes a pendingRequests Map to track active asynchronous operations. If multiple components request the same phone slug simultaneously, they subscribe to the same promise rather than initiating multiple network calls.
Sources: cooltools/lib/phoneApi.ts:12, cooltools/lib/phoneApi.ts:175-196
Client-Side State and Interaction
Data retrieved from the storage layers is managed on the client side using React state hooks for features like comparisons and searches.
Comparison State Management
The comparison engine tracks selected devices and fetches detailed specifications only when needed, storing them in a local record to avoid redundant lookups.
| Functionality | Implementation Detail | Source File |
|---|---|---|
| URL Synchronization | Uses useSearchParams to pre-fill comparisons from shared links. | cooltools/app/compare/page.tsx:16-32 |
| Bulk Fetching | Uses Promise.allSettled to retrieve details for multiple phones. | cooltools/app/compare/page.tsx:43-58 |
| Local Cache | Maintains a phoneDetails state object to store fetched specs. | cooltools/app/compare/page.tsx:11 |
Sources: cooltools/app/compare/page.tsx:10-60
Conclusion
The Server Storage Logic of CoolTools provides a robust foundation for phone comparison by leveraging PostgreSQL for persistent relational data and an intelligent caching layer for external data. This architecture ensures that spec-heavy pages load efficiently while allowing the system to track complex historical data like price changes and benchmark scores over time.