Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wppurking/gin-gorm
gin gorm middleware
https://github.com/wppurking/gin-gorm
gin gin-gonic gin-middleware gorm
Last synced: 10 days ago
JSON representation
gin gorm middleware
- Host: GitHub
- URL: https://github.com/wppurking/gin-gorm
- Owner: wppurking
- Created: 2019-07-17T15:49:04.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-07-17T16:17:20.000Z (over 5 years ago)
- Last Synced: 2024-06-20T11:48:04.991Z (6 months ago)
- Topics: gin, gin-gonic, gin-middleware, gorm
- Language: Go
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Gin Gorm
An gin middleware to provide every gin action an gorm transaction# Install
`go get github.com/wppurking/gin-gorm`# Usage
1. First `gin.Use` this middleware
2. In the `gin Action` method use `gin_gorm.Tx(c)` to get the gorm.DB transactino to use
```
func main() {
router := gin.Default()router.POST("/form_post", func(c *gin.Context) {
message := c.PostForm("message")
nick := c.DefaultPostForm("nick", "anonymous")tx := gin_gorm.Tx(c)
var user model.User
tx.Where("nick = ?", nick).First(&user)c.JSON(200, gin.H{
"status": "posted",
"message": message,
"nick": nick,
"user": user,
})
})
router.Run(":8080")
}```