Gorm module
module.gorm = github.com/revel/modules/orm/gorm
- sqlite3
- postgres
- mysql
# Database config
db.autoinit=true # default=true
db.driver=sqlite3 # mysql, postgres, sqlite3
db.host=/tmp/app.db # Use db.host /tmp/app.db is your driver is sqlite
#db.port=dbport # when the port is not used the default of each driver
#db.user=dbuser
#db.name=dbname
#db.password=dbpassword
#db.singulartable=false # default=false
- autoinit: The
Db
is initialized from the app.conf ifdb.autoinit=true
. - singulartable: By default all tables created based on a struct are pluralized.
For Example: a
type User struct {}
becomes tableusers
in the database , by settingsingulartable
totrue
, User's default table name will beuser
. Note table names set withTableName
won't be affected by this setting. You can also change the created table names by setting gorm.DefaultTableNameHandler on AppStartup or func init() see here for more details
package controllers
import (
"github.com/revel/revel"
gormc "github.com/revel/modules/orm/gorm/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()
}
package controllers
import (
"github.com/revel/revel"
gormc "github.com/revel/modules/orm/gorm/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()
}