forked from gothinkster/golang-gin-realworld-example-app
/
database.go
74 lines (61 loc) · 1.36 KB
/
database.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package common
import (
"fmt"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"os"
//My
//"github.com/Atlar/golang-gin-realworld-example-app/database_agent"
)
type Database struct {
*gorm.DB
}
var DB *gorm.DB
// Opening a database and save the reference to `Database` struct.
func Init() *gorm.DB {
db, err := gorm.Open("sqlite3", "./../gorm.db")
if err != nil {
fmt.Println("db err: ", err)
}
db.DB().SetMaxIdleConns(10)
//db.LogMode(true)
DB = db
return DB
}
// This function will create a temporarily database for running testing cases
func TestDBInit() *gorm.DB {
test_db, err := gorm.Open("sqlite3", "./../gorm_test.db")
if err != nil {
fmt.Println("db err: ", err)
}
test_db.DB().SetMaxIdleConns(3)
test_db.LogMode(true)
DB = test_db
return DB
}
// Delete the database after running testing cases.
func TestDBFree(test_db *gorm.DB) error {
test_db.Close()
err := os.Remove("./../gorm_test.db")
return err
}
// Using this function to get a connection, you can create your connection pool here.
func GetDB() *DBType{
//*gorm.DB {
//original
//return DB
return &clientDB
}
//My
var clientDB DBType
type DBType struct{
*gorm.DB
//database_agent.MongoAgent
}
func InitDB(){
clientDB.InitDB()
//database_agent.Init( &database_agent.DBagent)
}
func (self *DBType) InitDB () *DBType{
return &DBType{ Init() }
}