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
- Host: GitHub
- URL: https://github.com/gobuffalo/buffalo-pop
- Owner: gobuffalo
- License: mit
- Created: 2018-08-14T20:04:17.000Z (almost 7 years ago)
- Default Branch: main
- Last Pushed: 2023-01-28T05:47:08.000Z (over 2 years ago)
- Last Synced: 2025-04-07T08:18:11.887Z (3 months ago)
- Topics: go, golang, middleware, plugin, pop
- Language: Go
- Homepage:
- Size: 708 KB
- Stars: 19
- Watchers: 10
- Forks: 10
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# 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.