73 lines
1.1 KiB
Go
73 lines
1.1 KiB
Go
package models
|
|
|
|
import (
|
|
"github.com/jinzhu/gorm"
|
|
_ "github.com/jinzhu/gorm/dialects/postgres"
|
|
"gorm.io/driver/sqlite"
|
|
)
|
|
|
|
// var (
|
|
// gormdb, err =
|
|
// store = gormstore.New(gormdb, []byte("secret"))
|
|
// )
|
|
|
|
var psqlInfo string
|
|
var db *gorm.DB
|
|
|
|
func init() {
|
|
DB()
|
|
|
|
}
|
|
|
|
func DB() (*gorm.DB, error) {
|
|
if db == nil {
|
|
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
|
|
db.LogMode(true)
|
|
return db, err
|
|
}
|
|
return db, nil
|
|
|
|
}
|
|
|
|
func create(m interface{}) error {
|
|
db, err := DB()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return db.Create(m).Error
|
|
}
|
|
|
|
func read(m interface{}) error {
|
|
db, err := DB()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return db.Where(m).First(m).Error
|
|
}
|
|
|
|
func readAll(m interface{}) error {
|
|
db, err := DB()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return db.Find(m).Error
|
|
}
|
|
|
|
func update(m interface{}) error {
|
|
db, err := DB()
|
|
defer db.Close()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return db.Save(m).Error
|
|
}
|
|
|
|
func delete(m interface{}) error {
|
|
db, err := DB()
|
|
defer db.Close()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return db.Delete(m).Error
|
|
}
|