Files
leanote/vendor/github.com/revel/modules/orm/gorm
2017-11-30 19:55:33 +08:00
..
2017-11-30 19:55:33 +08:00
2017-11-30 19:55:33 +08:00
2017-11-30 19:55:33 +08:00

modules/gorm

Gorm module

Activation

module.gorm = github.com/revel/modules/orm/gorm

Drivers

  • sqlite3
  • postgres
  • mysql

Configuration file

# Database config
db.autoinit=true # default=true
db.driver=sqlite # mysql, postgres, sqlite3
db.host=localhost  # Use db.host /tmp/app.db is your driver is sqlite
db.user=dbuser
db.name=dbname
db.password=dbpassword

Example usage with transactions

package controllers

import (
    "github.com/revel/revel"
    gormc "github.com/revel/modules/gorm/orm/app/controllers"
)

type App struct {
    gormc.TxnController
}

type Toy struct {
    Name string
}

func (c App) Index() revel.Result {
    c.Txn.LogMode(true)
    c.Txn.AutoMigrate(&Toy{})
    c.Txn.Save(&Toy{Name: "Fidget spinner"})

    return c.Render()
}

Example usage without transactions

package controllers

import (
    "github.com/revel/revel"
    gormc "github.com/revel/modules/gorm/orm/app/controllers"
)

type App struct {
    gormc.Controller
}

type Toy struct {
    Name string
}

func (c App) Index() revel.Result {
    c.DB.LogMode(true)
    c.DB.AutoMigrate(&Toy{})
    c.DB.Save(&Toy{Name: "Fidget spinner"})

    return c.Render()
}