From 3f4a06f9ec63eb55beb1ac4f97703cd3716caeaf Mon Sep 17 00:00:00 2001 From: Marcus Noble Date: Sat, 17 Oct 2020 19:50:02 +0100 Subject: [PATCH] Reuse DB --- internal/feeds/store.go | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/internal/feeds/store.go b/internal/feeds/store.go index 36b80e3..5de4ea2 100644 --- a/internal/feeds/store.go +++ b/internal/feeds/store.go @@ -5,17 +5,24 @@ import ( "gorm.io/driver/sqlite" "gorm.io/gorm" "gorm.io/gorm/clause" + "gorm.io/gorm/logger" ) func (fs *FeedStore) getDB() *gorm.DB { - db, err := gorm.Open(sqlite.Open(viper.GetString("dbPath")), &gorm.Config{}) - if err != nil { - panic("failed to connect database") - } - db.AutoMigrate(&Feed{}) - db.AutoMigrate(&Item{}) + if fs.db == nil { + db, err := gorm.Open(sqlite.Open(viper.GetString("dbPath")), &gorm.Config{ + Logger: logger.Default.LogMode(logger.Error), + }) + if err != nil { + panic("failed to connect database") + } + db.AutoMigrate(&Feed{}) + db.AutoMigrate(&Item{}) - return db + fs.db = db + } + + return fs.db } func (fs *FeedStore) GetFeed(id string) *Feed {