This is the home for all things that combine Buffalo and Pop.
go install github.com/gobuffalo/buffalo-pop/v3@latest
Or with SQLite 3 support:
go get -tags sqlite -v github.com/gobuffalo/buffalo-pop/v3
The popmw.Transaction
will wrap each request inside of a new database transaction and automatically commit, or rollback, based on whether or not an error was returned from an upstream buffalo.Handler
or buffalo.MiddlewareFunc
.
First you need to add the middleware to your application giving it access to your *pop.Connection
, typically found at models.DB
.
import "github.com/gobuffalo/buffalo-pop/v3/pop/popmw"
func App() *buffalo.App {
// ...
app.Use(popmw.Transaction(models.DB))
// ...
}
Once added to the middleware stack for your application you can then use this transaction in upstream middleware or handlers.
func MyHandler(c buffalo.Context) error {
// Get the DB connection from the context
tx, ok := c.Value("tx").(*pop.Connection)
if !ok {
return errors.New("no transaction found")
}
}
WARNING: DO NOT OPEN MULTIPLE TRANSACTIONS WITHIN EACH OTHER - doing so will cause many, many, many problems.