Awesome-omni-skill NestJS Database
Data access patterns, Scaling, Migrations, and ORM selection.
install
source · Clone the upstream repo
git clone https://github.com/diegosouzapw/awesome-omni-skill
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/diegosouzapw/awesome-omni-skill "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/development/nestjs-database-ngxtm" ~/.claude/skills/diegosouzapw-awesome-omni-skill-nestjs-database && rm -rf "$T"
manifest:
skills/development/nestjs-database-ngxtm/SKILL.mdsource content
NestJS Database Standards
Selection Framework
1. Data Structure Analysis (The "What")
- Structured & Highly Related: Users, Orders, Inventory, Financials.
- Choice: PostgreSQL (Default).
- Why: Strict schema validation, ACID transactions, complex generic queries (Joins).
- Unstructured / Polymorphic: Product Catalogs (lots of unique attributes), CMS Content, Raw JSON blobs.
- Choice: MongoDB.
- Why: Schema flexibility, fast development speed for flexible data models.
- Time-Series / Metrics: IoT Sensor Data, Stock Prices, Server Logs.
- Choice: TimescaleDB (Postgres Extension).
- Why: Compression, hypertable partitioning, rapid ingestion.
2. Access Pattern Analysis (The "How")
- Transactional (OLTP): "User buys items to cart".
- Requirement: Strong Consistency (ACID). SQL is mandatory.
- Analytical (OLAP): "Dashboard showing sales trends".
- Requirement: Aggregation speed. Columnar storage (ClickHouse) or Read Replicas.
- High Throughput Write: "1M events/sec".
- Requirement: Append-only speed. Cassandra / DynamoDB (Leaderless replication).
3. Decision Matrix
| Feature Needed | Primary Choice | Alternative |
|---|---|---|
| General Purpose App | PostgreSQL | MySQL |
| Flexible JSON Docs | MongoDB | PostgreSQL (JSONB) |
| Search Engine | ElasticSearch | PostgreSQL (Full Text) |
| Financial Transactions | PostgreSQL | (None) |
Patterns
- Repository Pattern: Isolate database logic.
- TypeORM: Inject
.@InjectRepository(Entity) - Prisma: Create a comprehensive
.PrismaService
- TypeORM: Inject
- Abstraction: Services should call Repositories, not raw SQL queries.
Configuration (TypeORM)
- Async Loading: Always use
to load secrets fromTypeOrmModule.forRootAsync
.ConfigService - Sync: Set
in production; use migrations instead.synchronize: false
Scaling & Production
- Read Replicas: Configure separate
connections (Master for Write, Slaves for Read) in TypeORM/Prisma to distribute load.replication - Connection Multiplexing:
- Problem: Scaling K8s pods to 100+ exhausts DB connection limits (100 pods * 10 connections = 1000 conns).
- Solution: Use PgBouncer (Postgres) or ProxySQL (MySQL) in transaction mode. Do NOT rely solely on ORM pooling.
- Migrations:
- NEVER run
in production.synchronize: true - Execution: Run migrations via a dedicated "init container" or CD job step. Do NOT auto-run inside the main app process on startup (race conditions when scaling to multiple pods).
- NEVER run
- Soft Deletes: Use
(TypeORM) or middleware (Prisma) to preserve data integrity.@DeleteDateColumn
Architectures (Multi-Tenancy & Sharding)
- Column-Based (SaaS Standard): Single DB,
column.tenant_id- Scale: High. Isolation: Low.
- Code: Requires Row-Level Security (RLS) policies or strict
scopes.Where
- Schema-Based: One DB, one Schema per Tenant.
- Scale: Medium. Isolation: Medium. Good for B2B.
- Database-Based: One DB per Tenant.
- Scale: Low (max ~500 tenants per cluster). Isolation: High.
- Code: Requires "Connection Switching" middleware. Complex.
- Horizontal Sharding:
- Logic: Shard massive tables by a key (e.g.
) across physical nodes to exceed single-node write limits.user_id - Complexity: Extreme. Avoid until >10TB data. Use "Partitioning" first.
- Logic: Shard massive tables by a key (e.g.
- Partioning (Postgres):
- Strategy: Use native Table Partitioning (e.g., by range/date) for massive tables (Logs, Audit, Events).
- App Logic: Ensure partition keys (e.g.,
) are included increated_at
clauses to enable "Partition Pruning".WHERE
Migrations & Data Evolution
- Separation:
- Schema Migrations (DDL): Structural changes (
,CREATE TABLE
). Fast. Run before app deploy.ADD COLUMN - Data Migrations (DML): transforming data (
). Slow. Run as background jobs or separate scripts purely to avoid locking tables for too long.UPDATE users SET name = ...
- Schema Migrations (DDL): Structural changes (
- Zero-Downtime Field Migration (Expand-Contract Pattern):
- Expand: Add new column
(nullable). Deploy App v1 (Writes to bothnew_field
andold
).new - Migrate: Backfill data from
toold
in batches (background script).new - Contract: Deploy App v2 (Reads/Writes only
). Dropnew
in next schema migration.old_field
- Expand: Add new column
- Seeding:
- Dev: Use factories (
) to generate mock data.@faker-js/faker - Prod: Only seed static dictionaries (Roles, Countries) using "Upsert" logic to prevent duplicates.
- Dev: Use factories (
Best Practices
- Pagination: Mandatory. Use limit/offset or cursor-based pagination.
- Indexing: Define indexes in code (decorators/schema) for frequently filtered columns (
,where
).order by - Transactions: Use
(TypeORM) orQueryRunner
(Prisma) for all multi-step mutations to ensure atomicity.$transaction