A plugin to use gobuffalo/pop with buffalo
Clone or download

README.md

GoDoc Build Status Go Report Card

github.com/gobuffalo/buffalo-pop

This is the home for all things that combine Buffalo and Pop.

Installation

$ go get -u -v github.com/gobuffalo/buffalo-pop

Transaction Middleware

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.

Usage

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/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.WithStack(errors.New("no transaction found"))
  }
}

WARNING: DO NOT OPEN MULTIPLE TRANSACTIONS WITHIN EACH OTHER - doing so will cause many, many, many problems.