Serverpod Database Design#
한마디로#
이 스킬은 Serverpod 백엔드에서 데이터를 저장할 "표(테이블)"의 구조를 설계해 줍니다. 마치 도서관에서 책을 어떤 서가에 어떤 순서로 꽂을지, 책끼리 어떻게 연결할지 미리 정해 두는 일과 비슷합니다. 잘 설계해 두면 나중에 원하는 정보를 빠르고 정확하게 찾을 수 있습니다.
무엇을·언제#
- 새로운 데이터 저장 공간(테이블)을 만들 때 구조를 잡아 줍니다.
- 서로 다른 데이터를 어떻게 연결할지(예: 사용자와 주문 연결) 정리해 줍니다.
- 데이터 구조를 바꾸거나 새로 적용해야 할 때(마이그레이션) 도와줍니다.
- 데이터를 더 빠르게 찾도록 검색 성능을 개선해야 할 때 작동합니다.
핵심 용어#
| 용어 | 쉬운 설명 |
|---|---|
| 테이블(table) | 데이터를 줄과 칸으로 정리한 표, 엑셀 시트와 비슷합니다 |
| 관계(relation) | 서로 다른 표를 연결하는 고리 (예: 고객과 주문 연결) |
| 인덱스(index) | 책의 색인처럼 원하는 데이터를 빨리 찾게 해 주는 표시 |
| 마이그레이션(migration) | 데이터 구조를 바꾸고 그 변경을 실제로 반영하는 과정 |
| 쿼리(query) | 데이터베이스에 "이런 정보 줘"라고 요청하는 질문 |
| PostgreSQL | Serverpod가 기본으로 쓰는 데이터 저장 프로그램 |
Triggers#
- When designing new database tables
- When defining relations between tables
- When creating or applying migrations
- When query optimization is needed
Actions#
-
Define models and tables in Serverpod protocol YAML
- Configure field types, nullable, and default values
- Declare database mapping with the
tablekeyword - Define indexes
-
Set up relations between tables
- Define 1:1, 1:N, N:M relationships
- Use the
relationkeyword - Configure foreign keys and referential integrity
-
Manage migrations
- Run the
serverpod create-migrationcommand - Review and modify migration files
- Establish rollback strategies
- Run the
-
Implement query patterns
- Use Serverpod's type-safe query builder
- Handle transactions
- Implement pagination
Output#
- Protocol YAML model definitions (with table mapping)
- Migration SQL files
- Query code for Repository implementations
- Index and performance optimization suggestions
Notes#
- Serverpod uses PostgreSQL as its default database
-
Model definitions are managed in
protocol/*.yaml, andserverpod generateauto-generates Dart code and SQL - In Clean Architecture, database access is performed only in the Data layer's Repository implementations
- Database access is through Serverpod's
Session.db, which supports type-safe queries - For large data processing, use Serverpod's streaming queries and batch processing