Added backend
This commit is contained in:
24
backend/database/database.go
Normal file
24
backend/database/database.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var globDatabase *gorm.DB
|
||||
|
||||
func newDatabaseConnection(dialector gorm.Dialector) (*gorm.DB, error) {
|
||||
return gorm.Open(dialector)
|
||||
}
|
||||
|
||||
func NewSqlite3Connection(databaseFile string) (*gorm.DB, error) {
|
||||
return newDatabaseConnection(sqlite.Open(databaseFile))
|
||||
}
|
||||
|
||||
func GetDB() *gorm.DB {
|
||||
return globDatabase
|
||||
}
|
||||
|
||||
func SetGlobDB(database *gorm.DB) {
|
||||
globDatabase = database
|
||||
}
|
||||
13
backend/database/schema.go
Normal file
13
backend/database/schema.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package database
|
||||
|
||||
type Article struct {
|
||||
Id int `gorm:"primaryKey"`
|
||||
Title string
|
||||
Summary string
|
||||
Image string
|
||||
Created int64
|
||||
Modified int64
|
||||
Link string
|
||||
Markdown string
|
||||
Html string
|
||||
}
|
||||
9
backend/database/utils.go
Normal file
9
backend/database/utils.go
Normal file
@@ -0,0 +1,9 @@
|
||||
package database
|
||||
|
||||
import "gorm.io/gorm"
|
||||
|
||||
func Exists(tx *gorm.DB, query interface{}, args ...interface{}) bool {
|
||||
var exists bool
|
||||
tx.Where(query, args...).Select("count(*) > 0").Find(&exists)
|
||||
return exists
|
||||
}
|
||||
Reference in New Issue
Block a user