GORM 가이드

개발자 친화적인 것을 목표로 하는 Go언어 ORM 라이브러리

개요

  • 완전한 기능을 가진 ORM
  • Associations (Has One, Has Many, Belongs To, Many To Many, Polymorphism, Single-table inheritance)
  • Hooks (Before/After Create/Save/Update/Delete/Find)
  • Preload, Joins를 통한 데이터 가져오기
  • Transactions, Nested Transactions, Save Point, RollbackTo to Saved Point
  • Context, Prepared Statement 모드, DryRun 모드
  • Batch Insert, FindInBatches, Find/Create with Map, CRUD with SQL Expr and Context Valuer
  • SQL Builder, Upsert, Locking, Optimizer/Index/Comment Hints, Named Argument, SubQuery
  • Composite Primary Key, Indexes, Constraints
  • Auto Migrations
  • Logger
  • Generics API for type-safe queries and operations
  • Extendable, flexible plugin API: Database Resolver (multiple databases, read/write splitting) / Prometheus…
  • 모든 기능들은 테스트와 함께 제공됩니다
  • 개발자 친화적

설치

go get -u gorm.io/gorm
go get -u gorm.io/driver/sqlite

빠르게 시작하기

Generics API (>= v1.30.0)

package main

import (
"context"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)

type Product struct {
gorm.Model
Code string
Price uint
}

func main() {
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}

ctx := context.Background()

// Migrate the schema
db.AutoMigrate(&Product{})

// Create
err = gorm.G[Product](db).Create(ctx, &Product{Code: "D42", Price: 100})

// Read
product, err := gorm.G[Product](db).Where("id = ?", 1).First(ctx) // find product with integer primary key
products, err := gorm.G[Product](db).Where("code = ?", "D42").Find(ctx) // find product with code D42

// Update - update product's price to 200
err = gorm.G[Product](db).Where("id = ?", product.ID).Update(ctx, "Price", 200)
// Update - update multiple fields
err = gorm.G[Product](db).Where("id = ?", product.ID).Updates(ctx, map[string]interface{}{"Price": 200, "Code": "F42"})

// Delete - delete product
err = gorm.G[Product](db).Where("id = ?", product.ID).Delete(ctx)
}

Traditional API

package main

import (
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)

type Product struct {
gorm.Model
Code string
Price uint
}

func main() {
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}

// Migrate the schema
db.AutoMigrate(&Product{})

// Create
db.Create(&Product{Code: "D42", Price: 100})

// Read
var product Product
db.First(&product, 1) // find product with integer primary key
db.First(&product, "code = ?", "D42") // find product with code D42

// Update - update product's price to 200
db.Model(&product).Update("Price", 200)
// Update - update multiple fields
db.Model(&product).Updates(Product{Price: 200, Code: "F42"}) // non-zero fields
db.Model(&product).Updates(map[string]interface{}{"Price": 200, "Code": "F42"})

// Delete - delete product
db.Delete(&product, 1)
}

Platinum Sponsors

Platinum Sponsors