반응형

Golang/ORM 10

[Entgo] Hooks 사용 시 주의

Hooks 쿼리 등 작업 전 후에 원하는 코드 등을 추가하여 추가 작업을 할 수 있게 해준다. Where - ex) update 시 hook이 작동 될 수 있게 해당 schema에 hook을 OnUpdate에 적용을 해놨을 때, ent.Mutation의 데이터에 값이 들어있지 않는다. import ( gen "project/ent" "project/ent/hook" ) // user schema func (User) Fields() []ent.Field { return []ent.Field{ field.String("email"), field.String("password"), } } func (User) Hooks []ent.Hook { return []ent.Hook{ hook.If(func(next..

Golang/ORM 2023.03.23

[Entgo] Hooks

Hooks - 필드를 미리 작성 후 generate 한 뒤 hook을 작성해야 각종 필드에 맞는 Mutatotion이 생성되어 있다. - 사용을 원할 때 main.go에 import _ "/ent/runtime" 를 작성해야 작동한다. Mutation Create: 생성될 때 작동 UpdateOne: 하나의 데이터가 업데이트 될 때 작동 Update: 여러 개의 데이터가 업데이트 될 때 작동 DeleteOne: 하나의 데이터를 삭제할 때 작동 Delete: 여러 개의 데이터를 삭제할 때 작동 Runtime Hooks - 로깅 등에 자주 사용 - schema hooks 보다 먼저 실행된다. - 아래와 같이 모든 Entity에 적용하는 hook을 작성할 수 있다. - hook.On을 사용하여 원하는 시점에..

Golang/ORM 2022.12.07

[GORM] Query

Query 기본 type User struct { ID uint Name string Age int } var users []User db.Model(&user{}).Find(&users) // user에 대한 모든 데이터 출력 Map으로 찾기 var results []map[string]any db.Model(&User{}).Find(&results)//key : column명, value : 값으로 매칭되어 map slice로 들어가게 된다. FirstOrInit db.FirstOrInit(&user, User{Name: "none_existing"}) db.FirstOrInit(&user, User{Name: "soomin"}) // User데이터 존재 시 DB에서 first(order by Id)..

Golang/ORM 2022.09.28

[Entgo] About Annotations

Annotations fields 및 edges 와 같은 스키마 객체에 metadata를 첨부하고 외부 템블릿에 삽입할 수 있다. Annotation은 json(struct, map, slice)으로 직렬화 할 수 있는 Go 타입이어야 하고 annotation 인터페이스를 상속해야 된다. Custom Table Name 테이블 이름을 직접 지정하는 방법 func (User) Annotations() []schema.Annotation { return []schema.Annotation{ entsql.Annotation{Table: "user"}, } } Foreign Keys Configuration 외래키 설정을 직접 지정하는 방법 func (User) Edges() []ent.Edge { return..

Golang/ORM 2022.09.03

[Entgo] About Indexes

Indexes Multiple Fields 인덱스는 데이터 검색 속도 향상 또는 unique를 정의하기 위해 하나 이상의 필드를 구성할 수 있다. func (User) Indexes() []ent.Index { return []ent.Index{ index.Fields("field1", "field2"), index.Fields("first_name", "last_name").//unique인덱스 Unique(), } } 하나의 필드만 unique를 정의하려면 Fields()에서 정의할 수 있다. Index On Edges 필드 및 edge의 구성에 따라 인덱스를 사용할 수 있다. 주로 관계에서 필드의 unique를 설정하기 위해 사용한다. ex) 한 city에서 street의 이름은 unique하다.(..

Golang/ORM 2022.08.30

[Entgo] About Edges

Edges To and From edge.To 1 : N 일 때 1인 테이블의 Edges()에 To edge를 정의하는 스키마 관계를 소유 edge.From 1 : N 일 때 N인 테이블의 Edges()에 From 관계에 대한 역참조를 제공하는 빌더 O2O O2O other entity User Entity : Card Entity 두 Entity에 Unique()를 지정함으로써 가능 //ent/schema/user.go func (User) Edges() []ent.Edge { return []ent.Edge{ edge.To("card", Card.Type). Unique(), } } //ent/schema/card.go func (Card) Edges() []ent.Edge { return []ent..

Golang/ORM 2022.08.29

[Entgo] About Fields

Fields Database Type DB 별로 column type을 다르게 할 때 사용한다. import ( "entgo.io/ent" "entgo.io/ent/dialect" "entgo.io/ent/schema/field" ) func (Entity1) Fields() []ent.Field { return []ent.Field { field.Float("column1"). SchemaType(map[string]string { dialect.MySQL: "decimal(6, 2)", dialect.Postgres: "numeic", }), } } Go Type 기본 ent type을 사용자 정의 타입으로 재정의한다. type Column float64 func (Entity1) Fields() [..

Golang/ORM 2022.08.28

[Entgo] Entgo ORM란

Entgo Entgo 준비 Entgo란? - facebook에서 개발한 GO ORM - GO 코드로 스키마 작성 시 DB에 모델링된다. 설치 go get 명령어를 통해 패키지 다운로드 go get -d entogo.io/ent/cmd/ent 스키마 생성 해당 명령어로 entity가 생성된다. 여러개의 Entity를 한 번에 생성하고 싶은 경우 뒤에 띄어쓰기로 나눠서 쓰면 된다. go run entgo.io/ent/cmd/ent init project ├── ent │├── generate.go │└── schema.go │├── entity1.go │└── entity2.go ├── go.mod └── go.sum 기본 Entity 파일 //ent/schema/entity1.go package schem..

Golang/ORM 2022.08.18
728x90
반응형