2017-05-05 14:20:51 +02:00
package db
import (
"github.com/ewhal/nyaa/config"
"github.com/ewhal/nyaa/model"
2017-05-06 20:27:26 +02:00
"github.com/ewhal/nyaa/util/log"
2017-05-05 14:20:51 +02:00
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
// _ "github.com/go-sql-driver/mysql"
)
2017-05-06 20:27:26 +02:00
var ORM * gorm . DB
2017-05-05 14:20:51 +02:00
// GormInit init gorm ORM.
2017-05-06 20:27:26 +02:00
func GormInit ( conf * config . Config ) ( * gorm . DB , error ) {
2017-05-05 15:08:02 +02:00
db , err := gorm . Open ( conf . DBType , conf . DBParams )
2017-05-05 14:20:51 +02:00
// db, err := gorm.Open("mysql", config.MysqlDSL())
//db, err := gorm.Open("sqlite3", "/tmp/gorm.db")
// Get database connection handle [*sql.DB](http://golang.org/pkg/database/sql/#DB)
db . DB ( )
// Then you could invoke `*sql.DB`'s functions with it
db . DB ( ) . Ping ( )
db . DB ( ) . SetMaxIdleConns ( 10 )
db . DB ( ) . SetMaxOpenConns ( 100 )
// Disable table name's pluralization
// db.SingularTable(true)
if config . Environment == "DEVELOPMENT" {
db . LogMode ( true )
// db.DropTable(&model.User{}, "UserFollower")
2017-05-06 23:46:53 +02:00
db . AutoMigrate ( & model . Torrents { } )
2017-05-05 14:20:51 +02:00
// db.AutoMigrate(&model.User{}, &model.Role{}, &model.Connection{}, &model.Language{}, &model.Article{}, &model.Location{}, &model.Comment{}, &model.File{})
// db.Model(&model.User{}).AddIndex("idx_user_token", "token")
}
log . CheckError ( err )
// relation := gorm.Relationship{}
// relation.Kind = "many2many"
// relation.ForeignFieldNames = []string{"id"} //(M1 pkey)
// relation.ForeignDBNames = []string{"user_id"} //(M1 fkey in m1m2join)
// relation.AssociationForeignFieldNames = []string{"id"} //(M2 pkey)
// // relation.AssociationForeignStructFieldNames = []string{"id", "ID"} //(m2 pkey name in m2 struct?)
// relation.AssociationForeignDBNames = []string{"follower_id"} //(m2 fkey in m1m2join)
// m1Type := reflect.TypeOf(model.User{})
// m2Type := reflect.TypeOf(model.User{})
// handler := gorm.JoinTableHandler{}
// // ORDER BELOW MATTERS
// // Install handler
// db.SetJoinTableHandler(&model.User{}, "Likings", &handler)
// // Configure handler to use the relation that we've defined
// handler.Setup(&relation, "users_followers", m1Type, m2Type)
return db , err
2017-05-06 23:46:53 +02:00
}