From 6c9ac3bb339f410eeca62163a7ff477bd636bdb2 Mon Sep 17 00:00:00 2001 From: Peter Sanchez Date: Wed, 15 Feb 2023 12:46:33 -0600 Subject: [PATCH] Renaming database context getter helper --- database/middleware.go | 2 +- database/sql.go | 6 +++--- server/server.go | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/database/middleware.go b/database/middleware.go index 72287ce..92d5d7e 100644 --- a/database/middleware.go +++ b/database/middleware.go @@ -9,7 +9,7 @@ import ( func DBTransactionMiddleware(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { res := next(c) - db := DBFromContext(c.Request().Context()) + db := ForContext(c.Request().Context()) db.EnableCommit() if err := db.CommitTx(); err != nil { return err diff --git a/database/sql.go b/database/sql.go index 6ef6af1..3e898c6 100644 --- a/database/sql.go +++ b/database/sql.go @@ -29,8 +29,8 @@ func Context(ctx context.Context, db DBI) context.Context { return context.WithValue(ctx, dbCtxKey, db) } -// DBFromContext pulls db pool from context -func DBFromContext(ctx context.Context) DBI { +// ForContext pulls DBI obj for context +func ForContext(ctx context.Context) DBI { db, ok := ctx.Value(dbCtxKey).(DBI) if !ok { panic(errors.New("Invalid database context")) @@ -40,7 +40,7 @@ func DBFromContext(ctx context.Context) DBI { // WithTx calls a function wrapped in a database transaction func WithTx(ctx context.Context, opts *sql.TxOptions, fn func(tx *sql.Tx) error) error { - db := DBFromContext(ctx) + db := ForContext(ctx) tx, err := db.BeginTx(ctx, opts) if err != nil { return err diff --git a/server/server.go b/server/server.go index 6cb8d5f..f15fe67 100644 --- a/server/server.go +++ b/server/server.go @@ -294,7 +294,7 @@ func (s *Server) WithDefaultMiddleware() *Server { ), ) c.Response().Before(func() { - db := database.DBFromContext(c.Request().Context()) + db := database.ForContext(c.Request().Context()) db.EnableCommit() if err := db.CommitTx(); err != nil { panic(err) -- 2.45.2