summaryrefslogtreecommitdiff
path: root/storage/storage.go
diff options
context:
space:
mode:
Diffstat (limited to 'storage/storage.go')
-rw-r--r--storage/storage.go19
1 files changed, 16 insertions, 3 deletions
diff --git a/storage/storage.go b/storage/storage.go
index 67b8dd8..c863799 100644
--- a/storage/storage.go
+++ b/storage/storage.go
@@ -8,12 +8,18 @@ import (
"github.com/jmoiron/sqlx"
)
+type FullRepo interface {
+ ChatHistory
+ Memories
+}
+
type ChatHistory interface {
ListChats() ([]models.Chat, error)
GetChatByID(id uint32) (*models.Chat, error)
GetLastChat() (*models.Chat, error)
UpsertChat(chat *models.Chat) (*models.Chat, error)
RemoveChat(id uint32) error
+ ChatGetMaxID() (uint32, error)
}
type ProviderSQL struct {
@@ -61,12 +67,19 @@ func (p ProviderSQL) RemoveChat(id uint32) error {
return err
}
-func NewProviderSQL(dbPath string, logger *slog.Logger) ChatHistory {
+func (p ProviderSQL) ChatGetMaxID() (uint32, error) {
+ query := "SELECT MAX(id) FROM chats;"
+ var id uint32
+ err := p.db.Get(&id, query)
+ return id, err
+}
+
+func NewProviderSQL(dbPath string, logger *slog.Logger) FullRepo {
db, err := sqlx.Open("sqlite", dbPath)
if err != nil {
- panic(err)
+ logger.Error("failed to open db connection", "error", err)
+ return nil
}
- // get SQLite version
p := ProviderSQL{db: db, logger: logger}
p.Migrate()
return p