entgo의 공식 문서를 요약한 치트 시트
초기 세팅
go get entgo.io/ent/cmd/ent
go mod init <project>
go run entgo.io/ent/cmd/ent init <table name>
파일 구조
package schema
// User holds the schema definition for the User entity.
type User struct {
ent.Schema
}
// Fields of the User.
func (User) Fields() []ent.Field {
return nil
}
// Edges of the User.
func (User) Edges() []ent.Edge {
return nil
}
필드 속성
- SchemaType(map[string]string) : dialect별 자료 형 지정
- GoType(Type) : 해당 컬럼을 Golang에 바인드시 자료형 고정
- Default(value), DefaultFunc(func) : 기본 값 (비유일 컬럼만)
- UpdateDefault(value) : Update시 기본 값
- Annotations(&entsql.Annotation{}) : SQL dialect들의 기본값 전용 키워드 사용시
- Validate(func(Type) error) : 검증 함수 (빌트인 검증 함수: Positive, Negative, NonNegative, Min, Max, Range, MinLen, MaxLen, Match, NotEmpty)
- Optional() : 선택 컬럼임
- Nillable() : Optional() 지정된 컬럼에 지정시 Golang에 바인드 시 Type대신 *Type을 반환하며 nil 가능
- Immutable() : 불변 컬럼임, INSERT시만 지정 가능
- Unique() : 각 행의 값이 유일해야 하는 컬럼임
- StorageKey(string) : 컬럼 이름 수동 지정
- StructTag(string) : 구조체 태그 지정
- Sensitive() : 문자열 포매팅, 인코딩 시 표현되지 않음