Skip to content

Commit

Permalink
feat: Add ability to set DB to SQS subscriber context
Browse files Browse the repository at this point in the history
  • Loading branch information
Neurostep committed Nov 22, 2024
1 parent 29b0a21 commit a12605e
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions pkg/transport/sqs/request_response_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (

"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/aws/aws-sdk-go-v2/service/sqs/types"
"gorm.io/gorm"

sdkdatabasecontext "github.com/scribd/go-sdk/pkg/context/database"
sdkloggercontext "github.com/scribd/go-sdk/pkg/context/logger"
sdkmetricscontext "github.com/scribd/go-sdk/pkg/context/metrics"
sdkrequestidcontext "github.com/scribd/go-sdk/pkg/context/requestid"
Expand Down Expand Up @@ -108,3 +110,33 @@ func SetSubscriberMetrics(m sdkmetrics.Metrics) SubscriberRequestFunc {
return sdkmetricscontext.ToContext(ctx, m)
}
}

// SetSubscriberDatabase returns SubscriberRequestFunc that sets the GORM database to the request context.
func SetSubscriberDatabase(db *gorm.DB) SubscriberRequestFunc {
return func(ctx context.Context, cancel context.CancelFunc, message types.Message) context.Context {
return sdkdatabasecontext.ToContext(ctx, db)
}
}

// SetSubscriberDatabaseLogging returns SubscriberRequestFunc that sets the SDL Logger to GORM database
// and sets new gorm DB to the request context.
func SetSubscriberDatabaseLogging() SubscriberRequestFunc {
return func(ctx context.Context, cancel context.CancelFunc, message types.Message) context.Context {
db, err := sdkdatabasecontext.Extract(ctx)
if err != nil {
return ctx
}

l, err := sdkloggercontext.Extract(ctx)
if err != nil {
return ctx
}

newDB := db.Session(&gorm.Session{
Logger: sdklogger.NewGormLogger(l),
NewDB: true,
})

return sdkdatabasecontext.ToContext(ctx, newDB)
}
}

0 comments on commit a12605e

Please sign in to comment.