An open API service indexing awesome lists of open source software.

https://github.com/gobuffalo/buffalo-pop

A plugin to use gobuffalo/pop with buffalo
https://github.com/gobuffalo/buffalo-pop

go golang middleware plugin pop

Last synced: 2 months ago
JSON representation

A plugin to use gobuffalo/pop with buffalo

Awesome Lists containing this project

README

        


GoDoc
Go Report Card

# github.com/gobuffalo/buffalo-pop

This is the home for all things that combine [Buffalo](https://github.com/gobuffalo/buffalo) and [Pop](https://github.com/gobuffalo/pop).

## Installation

```bash
go install github.com/gobuffalo/buffalo-pop/v3@latest
```

Or with SQLite 3 support:

```bash
go get -tags sqlite -v github.com/gobuffalo/buffalo-pop/v3
```

## 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`.

```go
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.

```go
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.