Database Documentation
Complete guide to SveltyCMS database architecture, covering both database-agnostic infrastructure and MongoDB-specific implementation.
Last updated: 2/15/2026
Database Documentation
Complete guide to SveltyCMS database architecture, covering both database-agnostic infrastructure and MongoDB-specific implementation.
π Documentation Structure
Database-Agnostic Architecture
These documents explain the core database infrastructure that works with any database backend (MongoDB, PostgreSQL, MySQL, SQLite, etc.):
-
db.ts- Database manager/orchestratordb-interface.ts- Database adapter contracttheme-manager.ts- Theme service- How the adapter pattern enables database agnosticism
-
cache-service.ts- Dual-layer caching (Redis + MongoDB)CacheMetrics.ts- Performance monitoringCacheWarmingService.ts- Predictive prefetching- 8 cache categories with dynamic TTL
- NEW: Pattern-based predictive prefetching with custom fetchers
- ContentManager: LRU cache & batch initialization optimization
-
- Complete auth infrastructure (12 files)
- User authentication & authorization
- Session management & cleanup
- OAuth integration (Google)
- Two-factor authentication (2FA/TOTP)
- Permission system (RBAC)
Database-Specific Implementations
Implementation guides for specific database backends:
-
- Enterprise connection pool configuration
- 29 optimized indexes (4 TTL + 25 compound)
- Cursor pagination (99.9% faster)
- Streaming API (97% memory savings)
- Query hints & optimization
- 70-90% performance improvement
- NEW: Optimized
upsertManywithbulkWritefor high-volume writes
-
MariaDB Implementation π (Beta)
- Drizzle ORM with mysql2 driver
- 13 relational tables with proper indexes
- Connection pooling and health monitoring
- Automatic migration system
- Multi-tenant support (nullable tenantId)
- Type-safe queries with full TypeScript support
- Status: Implementation Complete (100%)
-
PostgreSQL Implementation π (Beta)
- Drizzle ORM with postgres.js driver
- Full schema with PostgreSQL-specific types (UUID, JSONB)
- Connection management and health checks
- Status: Beta (Seeding and Core CRUD Implemented)
-
SQLite Implementation π (Beta)
- Drizzle ORM with better-sqlite3/bun:sqlite
- Zero-config single-file database
- Ideal for local development and edge
- Status: Implementation Complete (100%)
π― Quick Navigation
I want toβ¦
- Understand the overall architecture β Read Core Infrastructure
- Learn about caching β Read Cache System
- Understand authentication β Read Authentication System
- Optimize MongoDB β Read MongoDB Implementation
- Implement MariaDB β Read MariaDB Implementation
- Implement PostgreSQL β Read PostgreSQL Implementation
- Implement SQLite β Read SQLite Implementation
- Add a new database β Read Core Infrastructure section βAdding New Adaptersβ
- Check cache performance β Read Cache System section βMetricsβ
- Implement permissions β Read Authentication System section βPermissionsβ
π Getting Started
For New Developers
- Start with Core Infrastructure to understand the 3-layer architecture
- Read Cache System to understand performance optimization
- Read Authentication System to understand security
For MongoDB Users
- Read MongoDB Implementation for best practices
- Check the βQuick Referenceβ section for common operations
- Review index strategy for your use case
For MariaDB Users
- Read MariaDB Implementation for setup guide
- Review the schema design and migration system
- Check implementation roadmap for completion status
For PostgreSQL Users
- Read PostgreSQL Implementation for current status
- Note: Beta implementation - stub methods need completion
- Follow MariaDB pattern for contributing implementations
For Adding New Database Support
- Read Core Infrastructure β βAdding New Adaptersβ
- Implement the
DatabaseAdapterinterface - Follow the PostgreSQL or MariaDB example
- Test against the database-agnostic tests
π Architecture Overview
π§ Key Features
Database Agnostic
- β Works with MongoDB, MariaDB, PostgreSQL, MySQL, SQLite
- β
Unified
DatabaseAdapterinterface - β No database-specific code in business logic
- β
DatabaseResult<T>pattern (no exceptions)
High Performance
- β Dual-layer cache (Redis L1 + MongoDB L2)
- β 92% cache hit rate
- β 97% faster response times
- β 29 optimized MongoDB indexes
- β Cursor pagination (O(1) time)
- β Streaming API (O(1) memory)
- β Batch Widget API (Solves N+1 problem for relations)
Enterprise Security
- β Multi-factor authentication (2FA/TOTP)
- β OAuth integration (Google)
- β Role-based access control (RBAC)
- β Granular permissions
- β Session management with automatic cleanup
- β API endpoint protection
Developer Experience
- β TypeScript throughout
- β Comprehensive documentation
- β Code examples for every feature
- β Performance metrics built-in
- β Best practices documented
- β Query builder API
π Performance & Scalability
SveltyCMS achieving sub-millisecond latency is not just about the database; itβs about the Unified Caching Layer that sits between the application and the persistent storage. This architecture ensures high performance regardless of your database choice.
Unified Caching Flow
Benchmarking Environment
Benchmarks were conducted on the following production hardware:
- CPU: AMD Ryzen 5 3600 6-Core Processor (12 threads)
- RAM: 64GB DDR4
- OS: Ubuntu 24.04.3 LTS
- Environment: Node.js v24.x, Local Database Instance
Performance Matrix
The following metrics represent benchmarks across different environments. High-level gains like the 92% cache hit rate apply across all supported databases due to the agnostic infrastructure.
| Metric | MongoDB | MariaDB | PostgreSQL | SQLite | Why it matters |
|---|---|---|---|---|---|
| Cache Hit Rate | 98% | 92% | 92% | 94% | LRU Memory Cache |
| Response Time | <0.1ms | 1.2ms | 1.8ms | 0.2ms | In-memory content serving |
| P99 Latency | <2ms | <15ms | <12ms | <0.5ms | Batch initialization |
| Storage Engine | WiredTiger | InnoDB | RocksDB/B-Tree | SQLite WAL | Data integrity and speed |
| JSON Performance | $$$ | $$ | $$$ | $$ | JSON1 extension enablement |
| Scaling | Sharding | Replication | Clustering | Embedded | Zero-latency local IO |
Key Performance Features
- 97% Faster Response Times: Reduced from average 50ms to ~2.5ms via predictive prefetching.
- 97.5% Memory Savings: Achieved through advanced result streaming for large datasets.
- 99.9% Pagination Improvement: Cursor-based navigation ensures constant O(1) time complexity even on page 1000+.
- Database-Specific Optimizations:
- MongoDB: 29 optimized compound and TTL indexes.
- MariaDB: Relational indexing and optimized
mysql2pool. - PostgreSQL: Native
jsonbsupport and tamper-evident audit logs. - SQLite: WAL mode for concurrent access and zero-latency local IO.
π Related Documentation
API Documentation
- Database Agnostic Verification - API endpoint verification
Development Guides
- Contributing - How to contribute to docs
π Documentation Standards
All database documentation follows these standards:
- β
.mdxformat with complete frontmatter - β Code examples with syntax highlighting
- β Clear explanations of purpose and usage
- β Performance metrics where applicable
- β Best practices sections
- β How components work together
- β Real-world usage examples
See Contributing Guidelines for more details.
π€ Contributing
Want to improve the database documentation?
- Follow the Contributing Guidelines
- Ensure
.mdxformat with proper frontmatter - Include code examples and performance data
- Add to this README if adding new docs
- Test all code examples before submitting
Last Updated: 2025-11-15
Maintained by: SveltyCMS Team