initial commit
This commit is contained in:
commit
a80cd4bd5f
6 changed files with 186 additions and 0 deletions
9
models/book.go
Normal file
9
models/book.go
Normal file
|
@ -0,0 +1,9 @@
|
|||
package models
|
||||
|
||||
type Book struct {
|
||||
ID uint `json:"id" gorm:"primary_key"`
|
||||
Title string `json:"title"`
|
||||
Author string `json:"author"`
|
||||
}
|
||||
|
||||
|
20
models/setup.go
Normal file
20
models/setup.go
Normal file
|
@ -0,0 +1,20 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
_ "gorm.io/driver/sqlite"
|
||||
)
|
||||
|
||||
var DB *gorm.DB
|
||||
|
||||
func ConnectDatabase() {
|
||||
database, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
|
||||
if err != nil {
|
||||
panic("Failed to connect to database!")
|
||||
}
|
||||
err = database.AutoMigrate(&Book{})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
DB = database
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue