up
This commit is contained in:
16
pkg/store/mysql/mysql.go
Normal file
16
pkg/store/mysql/mysql.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 初始化GORM数据库
|
||||
func NewDB(dsn string) *gorm.DB {
|
||||
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return db
|
||||
}
|
||||
24
pkg/store/sqlite/sqlite.go
Normal file
24
pkg/store/sqlite/sqlite.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func NewDB() *gorm.DB {
|
||||
if _, err := os.Stat("db"); os.IsNotExist(err) {
|
||||
errDir := os.MkdirAll("db", 0755)
|
||||
if errDir != nil {
|
||||
log.Fatalln("Error creating directory:", err)
|
||||
}
|
||||
}
|
||||
var err error
|
||||
db, err = gorm.Open(sqlite.Open("./db/cat.db"), &gorm.Config{})
|
||||
if err != nil {
|
||||
panic("failed to connect database")
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user