From 566ba834d483e57d7432b59eb91a657e36b1a7c3 Mon Sep 17 00:00:00 2001 From: Kristian Borgwarth <10348902@pm.me> Date: Sat, 4 Apr 2026 00:14:58 +0200 Subject: [PATCH] feat(db_context): added db context --- core/handlers/create_note_handler.go | 6 ++- core/handlers/initialize_handler.go | 6 +-- core/server/server.go | 5 --- go.mod | 3 ++ go.sum | 2 + persistence/db_context.go | 38 +++++++++++++++++++ persistence/repositories/uow.go | 13 +++++-- .../create_note_handler_test.go | 12 +++--- test/test_integration/main_test.go | 9 ++++- 9 files changed, 72 insertions(+), 22 deletions(-) create mode 100644 persistence/db_context.go diff --git a/core/handlers/create_note_handler.go b/core/handlers/create_note_handler.go index 608edaa..18e17ea 100644 --- a/core/handlers/create_note_handler.go +++ b/core/handlers/create_note_handler.go @@ -18,10 +18,12 @@ type createNoteCommand struct { Vars map[string]string `json:"vars"` } -type CreateNoteHandler struct{} +type CreateNoteHandler struct{ + uow *repositories.UnitOfWork +} func NewCreateNoteHandler() *CreateNoteHandler { - return &CreateNoteHandler{} + return &CreateNoteHandler{repositories.NewUnitOfWork()} } func (h *CreateNoteHandler) Handle(ctx context.Context, uow *repositories.UnitOfWork, raw json.RawMessage) (any, error) { diff --git a/core/handlers/initialize_handler.go b/core/handlers/initialize_handler.go index ee43b28..c698f62 100644 --- a/core/handlers/initialize_handler.go +++ b/core/handlers/initialize_handler.go @@ -17,13 +17,13 @@ func NewInitializeHandler() *InitializeHandler { } func (h InitializeHandler) Handle(ctx context.Context, raw json.RawMessage) (any, error) { - var params initializeCommand + var cmd initializeCommand - if err := json.Unmarshal(raw, ¶ms); err != nil { + if err := json.Unmarshal(raw, &cmd); err != nil { return nil, err } - _, err := persistence.InitializeIndex(params.VaultPath) + err := persistence.InitializeDBContext(cmd.VaultPath) if err != nil { return nil, err } diff --git a/core/server/server.go b/core/server/server.go index c7aa004..4a1c8dd 100644 --- a/core/server/server.go +++ b/core/server/server.go @@ -4,7 +4,6 @@ package server import ( "bufio" "context" - "database/sql" "encoding/json" "fmt" "io" @@ -14,7 +13,6 @@ import ( ) type Server struct { - db *sql.DB handlers map[string]handlers.Handler } @@ -75,9 +73,6 @@ func (s *Server) handle(w io.Writer, req rpc.Request) { s.respond(w, req.ID, result, nil) } -func (s *Server) initalize(){ -} - func (s *Server) respond(w io.Writer, id *int, result any, err *rpc.Error) { resultJSON, _ := json.Marshal(result) resp := rpc.Response{ diff --git a/go.mod b/go.mod index c43cce8..2da1664 100644 --- a/go.mod +++ b/go.mod @@ -5,9 +5,12 @@ go 1.26 require ( github.com/google/uuid v1.6.0 github.com/stretchr/testify v1.11.1 + golang.org/x/tools v0.42.0 modernc.org/sqlite v1.47.0 ) +require github.com/yuin/goldmark v1.4.13 // indirect + require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/dustin/go-humanize v1.0.1 // indirect diff --git a/go.sum b/go.sum index 7508947..6109b5b 100644 --- a/go.sum +++ b/go.sum @@ -18,6 +18,8 @@ github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94 github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= +github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/mod v0.33.0 h1:tHFzIWbBifEmbwtGz65eaWyGiGZatSrT9prnU8DbVL8= golang.org/x/mod v0.33.0/go.mod h1:swjeQEj+6r7fODbD2cqrnje9PnziFuw4bmLbBZFrQ5w= golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4= diff --git a/persistence/db_context.go b/persistence/db_context.go new file mode 100644 index 0000000..4195b0a --- /dev/null +++ b/persistence/db_context.go @@ -0,0 +1,38 @@ +package persistence + +import ( + "database/sql" +) + +type DBContext struct { + DB *sql.DB +} + +var dbContext *DBContext + + +func InitializeDBContext(vaultPath string) (error) { + db, err := InitializeIndex(vaultPath) + if err != nil { + return err + } + + dbContext = &DBContext{DB: db} + return nil +} + +func GetDBContext() (*DBContext, error) { + if dbContext == nil { + panic("DBContext is not initialized. Call InitializeDbContext first.") + } + return dbContext, nil +} + +func CloseDBContext() error { + if dbContext == nil { + return nil + } + err := dbContext.DB.Close() + dbContext = nil + return err +} diff --git a/persistence/repositories/uow.go b/persistence/repositories/uow.go index 49db413..3f0abe5 100644 --- a/persistence/repositories/uow.go +++ b/persistence/repositories/uow.go @@ -3,21 +3,26 @@ package repositories import ( "database/sql" + "github.com/KristianJBorgwarth/dendrite.daemon/persistence" "github.com/KristianJBorgwarth/dendrite.daemon/persistence/store" ) type UnitOfWork struct { - db *sql.DB + dbContext *sql.DB Transaction *sql.Tx FileStore *store.FileStore } -func NewUnitOfWork(db *sql.DB) *UnitOfWork { - return &UnitOfWork{db: db, FileStore: store.NewFileStore()} +func NewUnitOfWork() *UnitOfWork { + db, err := persistence.GetDBContext() + if err != nil { + panic("failed to get DB context: " + err.Error()) + } + return &UnitOfWork{dbContext: db.DB, FileStore: store.NewFileStore()} } func (u *UnitOfWork) Begin() (tx *sql.Tx, err error) { - tx, err = u.db.Begin() + tx, err = u.dbContext.Begin() if err != nil { return nil, err } diff --git a/test/test_integration/create_note_handler_test.go b/test/test_integration/create_note_handler_test.go index 02db85c..20787d2 100644 --- a/test/test_integration/create_note_handler_test.go +++ b/test/test_integration/create_note_handler_test.go @@ -16,7 +16,7 @@ import ( func TestCreateNoteHandler_NoTemplate_CreatesNoteFileAndReturnsPath(t *testing.T) { // Arrange handler := handlers.NewCreateNoteHandler() - uow := repositories.NewUnitOfWork(Fixture.DB) + uow := repositories.NewUnitOfWork() notePath := filepath.Join(t.TempDir(), "my-note.md") params, _ := json.Marshal(map[string]any{ "title": "My Note", @@ -41,7 +41,7 @@ func TestCreateNoteHandler_NoTemplate_CreatesNoteFileAndReturnsPath(t *testing.T func TestCreateNoteHandler_WithTemplate_CreatesNoteFileWithTagsAndReturnsPath(t *testing.T) { // Arrange handler := handlers.NewCreateNoteHandler() - uow := repositories.NewUnitOfWork(Fixture.DB) + uow := repositories.NewUnitOfWork() dir := t.TempDir() templatePath := filepath.Join(dir, "template.md") @@ -85,8 +85,8 @@ func TestCreateNoteHandler_DuplicateSlug_Upserts(t *testing.T) { // Arrange dir := t.TempDir() handler := handlers.NewCreateNoteHandler() - uowInit := repositories.NewUnitOfWork(Fixture.DB) - secondUow := repositories.NewUnitOfWork(Fixture.DB) + uowInit := repositories.NewUnitOfWork() + secondUow := repositories.NewUnitOfWork() path1 := filepath.Join(dir, "dup-note.md") params1, _ := json.Marshal(map[string]any{"title": "Dup Note", "path": path1}) @@ -114,7 +114,7 @@ func TestCreateNoteHandler_DuplicateSlug_Upserts(t *testing.T) { func TestCreateNoteHandler_InvalidJSON_ReturnsError(t *testing.T) { // Arrange handler := handlers.NewCreateNoteHandler() - uow := repositories.NewUnitOfWork(Fixture.DB) + uow := repositories.NewUnitOfWork() // Act _, err := handler.Handle(Fixture.TestContext, uow, json.RawMessage(`{invalid json}`)) @@ -126,7 +126,7 @@ func TestCreateNoteHandler_InvalidJSON_ReturnsError(t *testing.T) { func TestCreateNoteHandler_NonExistentTemplatePath_ReturnsError(t *testing.T) { // Arrange handler := handlers.NewCreateNoteHandler() - uow := repositories.NewUnitOfWork(Fixture.DB) + uow := repositories.NewUnitOfWork() params, _ := json.Marshal(map[string]any{ "title": "Ghost Note", "path": filepath.Join(t.TempDir(), "ghost.md"), diff --git a/test/test_integration/main_test.go b/test/test_integration/main_test.go index c14b459..8551caf 100644 --- a/test/test_integration/main_test.go +++ b/test/test_integration/main_test.go @@ -22,15 +22,20 @@ type DBFixture struct { func NewDBFixture() *DBFixture { vaultPath := os.TempDir() - db, err := persistence.InitializeIndex(vaultPath) + err := persistence.InitializeDBContext(vaultPath) if err != nil { panic(err) } dbPath := filepath.Join(os.TempDir(), ".index", "index.db") + dbContext, err := persistence.GetDBContext() + if err != nil { + panic("failed to get DB context: " + err.Error()) + } + return &DBFixture{ - DB: db, + DB: dbContext.DB, DBPath: dbPath, TestContext: context.Background(), }