AGENT SKILL

database-postgresql

Data access patterns, scaling, migrations, and ORM selection for PostgreSQL. (triggers: **/*.entity.ts, prisma/schema.prisma, **/migrations/*.sql, TypeOrmModule, PrismaService, PostgresModule)

使用场景后端与接口
License 许可
MIT
最近更新
2026-06-24
Stars
★ 67
Forks
24

01 前言 Front Matter

name
database-postgresql
description
Data access patterns, scaling, migrations, and ORM selection for PostgreSQL. (triggers: **/*.entity.ts, prisma/schema.prisma, **/migrations/*.sql, TypeOrmModule, PrismaService, PostgresModule)

02 资源文件 6

03 SKILL.md 正文

PostgreSQL Database Standards

Priority: P0 (FOUNDATIONAL)

Integration patterns and ORM standards for PostgreSQL applications.

Patterns & Architecture

  • Repository Pattern: Isolate database logic. Use @InjectRepository() or PrismaService.
  • Relationship Integrity: Avoid redundant raw ID columns. Favor relation properties.

Migrations (Strict Rules)

  • NEVER use synchronize: true in production.
  • Generation: Modify .entity.ts -> run pnpm migration:generate.
  • Zero-Downtime: Use Expand-Contract pattern (Add -> Backfill -> Drop) for destructive changes.
  • RLS: typeorm migration:generate cannot detect Row-Level Security. Use raw queryRunner.query() SQL for RLS.

Performance & Gotchas

  • Pagination: Mandatory. Use limit/offset or cursor-based pagination.
  • Indexing: Define indexes in code for frequently filtered columns. RLS columns MUST be indexed.
  • Transactions: Use QueryRunner or $transaction for multi-step mutations.

Anti-Patterns

  • No N+1 queries: Use query builders or eager-load relations instead of lazy-loading in loops.
  • No heavy RLS joins: Keep RLS predicates simple; move complex logic to the query/view layer.
  • No synchronize in production: Always run explicit migrations; synchronize: true is destructive.

References

04 同仓库更多 Skills