Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Become a gorm feeling and use gorm

linyows
April 15, 2018

Become a gorm feeling and use gorm

Gormの気持ちになってGormを使う
Aprio 15, 2018 @Go Conference 2018 Spring でお話しした資料です

linyows

April 15, 2018
Tweet

More Decks by linyows

Other Decks in Programming

Transcript

  1. (P$POGFSFODF4QSJOH (PSN࢖ͬͨ͜ͱ͋Γ·͔͢ʁ ▸ 03.0CKFDU3FMBUJPOBM.BQQFS ▸ ଟ෼ɺ(PͰ͸Ұ൪ϝδϟʔͳ03. ▸ (PSNͷଞʹ͸HPSQ YPSN TRMY

    ▸ ΄͔ͷݴޠͩͱɺ"DUJWF3FDPSE 3VCZ &MPRVFOU 1)1  4FRVFMJ[F /PEFKT  "DUJWF3FDPSEͷѹ౗త׬੒౓ʜʜʜʜ
  2. (P$POGFSFODF4QSJOH type Product struct { gorm.Model Code string Price uint

    } func main() { db, _ := gorm.Open("sqlite3", "test.db") defer db.Close() db.Create(&Product{Code: "L1212", Price: 1000}) var p Product db.First(&p, 1) db.First(&p, "code = ?", "L1212") db.Model(&p).Update("Price", 2000) db.Delete(&p) } (PSNͷ࢖͍ํ
  3. (P$POGFSFODF4QSJOH err := db.Debug().Where("role = ?", "admin").Or("role = ?", "super").Find(&users).Error

    // SELECT * FROM users WHERE role = 'admin' OR role = 'super'; (PSNͷಛ௃ (PͰ͸௝͍͠ϝιουνΣʔϯ ϝιουνΣʔϯͳͷͰಈతͳΫΤϦ࡞੒͕؆୯Ͱ͖Δ ϝιουνΣʔϯͷͨΊʹΤϥʔΛߏ଄ମʹೖΕ͍ͯΔ
  4. (P$POGFSFODF4QSJOH package gorm type Model struct { ID uint `gorm:"primary_key"`

    CreatedAt time.Time UpdatedAt time.Time DeletedAt *time.Time `sql:"index"` } package model type User struct { gorm.Model Name string } (PSNͷϞσϧఆٛ
  5. (P$POGFSFODF4QSJOH $ find model -type f -not -name '*_test.go' |

    wc -l 35 $ find model -type f -not -name '*_test.go' | xargs grep 'func' | grep -v '\sfunc' | wc -l 196 # ???????????????? ଟ͘ͳ͍ʁ $ find api -type f -not -name '*_test.go' | wc -l 22 (PSNΛར༻ͨ͠ɺ͋Δ"1*4FSWFS
  6. (P$POGFSFODF4QSJOH func FindUserByName(n string) *User, error { u := new(User)

    if err := db. First(u, "name = ?", n).Error; err != nil { return nil, err } return u, nil } Ϟσϧͷཚཱؔ਺ɺϝιου1BSU ·͋ɺ΍Γ͍ͨ͜ͱ͸Θ͔Δ
  7. (P$POGFSFODF4QSJOH func FindUserByNameWithProfile(n string) *User, error { u := new(User)

    if err := db. Preload("Profile").First(u, "name = ?", n).Error; err != nil { return nil, err } return u, nil } Ϟσϧͷཚཱؔ਺ɺϝιου1BSU ଞͷϞσϧґଘΛ௥Ճʜ
  8. (P$POGFSFODF4QSJOH func FindUserByNameWithAll(n string) *User, error { u := new(User)

    if err := db. Preload("Profile"). Preload("Projects"). Preload("Projects.Repositories"). Preload("Roles").First(u, "name = ?", n).Error; err != nil { return nil, err } return u, nil } Ϟσϧͷཚཱؔ਺ɺϝιου1BSU ͋Ε΍͜Ε΍௥Ճ͞Ε·ͯ͠ʜ
  9. (P$POGFSFODF4QSJOH func FindUserByName(n string, Preload ...string) *User, error { d

    := db for _, v := range preloads { d = d.Preload(v) } u := new(User) if err := d.First(&u, "name = ?", n).Error; err != nil { return nil, err } return u, nil } Ϟσϧͷཚཱؔ਺ɺϝιου1BSU ͏·͘΍͍ͬͯΔ෩ʜ
  10. (P$POGFSFODF4QSJOH (u *User) FindByName(n string) error { if err :=

    db. First(u, "name = ?", n).Error; err != nil { return err } return nil } Ϟσϧͷཚཱؔ਺ɺϝιου1BSU ͪΐͬͱ͓Μͳ͡΍Μ
  11. (P$POGFSFODF4QSJOH DMBTT JOTUBODF WBSJBCMFT %#DPOOFDUJPO GVOD %# DPOOFDUJPO TUSVDU NFUIPE

    6TFS 3VCZ (P ֤ίϯϙʔωϯτ͕෼཭͞ΕΞΫηε͠΍͍͢ Ϟσϧ Ϟσϧ
  12. (P$POGFSFODF4QSJOH package api func GetUserWithProfile(c echo.Context) error { n :=

    c.Param("username") u, e := model.FindUserByUsernameWithProfile(n) if e != nil { return NewError(http.StatusNotFound, e, "not found") } return c.JSON(http.StatusOK, u)) } package model func FindUserByUsernameWithProfile(n string) (*User, error) { u := new(User) if err := db.Preload("Profile").Scopes(Enabled).Where("username = ?", n).First(u).Error; err != nil { return nil, err } return u, nil } มߋલ
  13. (P$POGFSFODF4QSJOH package api func GetUserWithProfile(c echo.Context) error { n :=

    c.Param("username") u := new(model.User) if err := db.Preload("Profile").Scopes(Enabled).Where("username = ?", n).First(u).Error; err != nil { return NewError(http.StatusNotFound, e, "not found") } return c.JSON(http.StatusOK, f) } มߋޙ ͜Ε͚ͩʂϞσϧଆͷ࣮૷͸ແ͘ͳΓ·ͨ͠
  14. (P$POGFSFODF4QSJOH ৽ͨͳ՝୊ɿݺͼग़͠ଆʹࣅͨΑ͏ͳίʔυ͕૿͑ͳ͍ʁ 4DPQFTΛ࢖͑͹େৎ෉ɺݺͼग़͠Λڞ௨Խ͢Δ͜ͱ͕Ͱ͖Δ 4DPQF͸ɺ'VODUJPOͳͷͰϞσϧ໊Λ1SFpYʹ͢Δͱ੔ཧ͠΍͍͢ func UserGolden() func (db *gorm.DB) *gorm.DB

    { return func (db *gorm.DB) *gorm.DB { return db.Preload("Profile").Preload("Projects").Preload("Roles").Where("status = ?", "enabled") } } u := new(user) db.Scopes(UserGolden()).Find(u, 123)