티스토리 뷰

Golang에서의 경량 ORM으로서의 bun package 다루며 경험한 내용을 정리해둔다.

 

Automatic timestamp

 

링크: https://bun.uptrace.dev/guide/models.html#automatic-timestamps

 

INSERT 시에 생성한 시간을 자동으로 추가하도록 하는 기능이다.

 

Example code with comment

 

아래 코드와 코멘트로 설명을 마무리 한다.

 

전체 GitHub Repo: https://github.com/nicewook/bun-orm-study

Query Building Example:  https://github.com/nicewook/bun-orm-study/blob/main/default-timestamp/main.go

 

bun 태그의 의미는 다음과 같다.

참고 링크: https://bun.uptrace.dev/guide/models.html#struct-tags

  • nullzero: Go언어에서 type zero value 값이라면 null 처리하거나 default 값을 넣으라는 제약
  • notnull: null 되어서는 안되는 컬럼이라는 제약(constraint)
  • default:current_timestamp: 현재 시간을 default 값으로 하라는

 

정리하자면 zero value 값이 온다면 null 또는 default 값을 넣으라는 것인데 null 수는 없는 컬럼이니 따로 값을 명시해서 넣지 않으면 항상 current_timestamp 값 들어가게 된다.

type User struct {
  bun.BaseModel `bun:"table:users,alias:u"`
  ID        int64     `bun:"id,pk,autoincrement"`
  Name      string    `bun:"name,notnull"`
  CreatedAt time.Time `bun:"createdAt,nullzero,notnull,default:current_timestamp"`
}

 

테스트를 위해 INSERT 1 간격으로 한다.

func insertUsers() {
  for _, user := range users {
    _, err := db.NewInsert().Model(&user).Exec(context.Background())
    if err != nil {
      panic(err)
    }
    time.Sleep(time.Second)
  }
}

 

실행 부분과 출력 결과이다.

func main() {
  prepareDB()
  var createdTimes []time.Time
  if err := db.NewSelect().Model((*User)(nil)).
    Column("createdAt").
    Scan(context.Background(), &createdTimes); err != nil {
    panic(err)
  }
  for i, ctime := range createdTimes {
    fmt.Printf("record #%d, created at %v\n", i+1, ctime)
  }
}
// 아래와 같이 INSERT 된 시간이 출력된다.
// record #1, created at 2022-12-09 17:26:21 +0000 UTC
// record #2, created at 2022-12-09 17:26:22 +0000 UTC
// record #3, created at 2022-12-09 17:26:23 +0000 UTC
// record #4, created at 2022-12-09 17:26:24 +0000 UTC

PostgresSQL 에서는 Timezone 정보를 포함한 timestamptz 쓰고, 가능한 timestamp 쓰지말라

참고 링크: https://bun.uptrace.dev/postgres/postgres-data-types.html#timestamptz-vs-timestamp

반응형
반응형
잡학툰 뱃지
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함