Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/SpectatorNan/gorm-zero
go zero gorm extension / Integrated processing of db model and Redis cache
https://github.com/SpectatorNan/gorm-zero
cache database go-zero goctl golang gorm
Last synced: 26 days ago
JSON representation
go zero gorm extension / Integrated processing of db model and Redis cache
- Host: GitHub
- URL: https://github.com/SpectatorNan/gorm-zero
- Owner: SpectatorNan
- License: mit
- Created: 2022-01-11T07:41:41.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-09-09T16:54:46.000Z (3 months ago)
- Last Synced: 2024-09-09T20:51:46.451Z (3 months ago)
- Topics: cache, database, go-zero, goctl, golang, gorm
- Language: Go
- Homepage:
- Size: 245 KB
- Stars: 230
- Watchers: 5
- Forks: 36
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-zero - gorm-zero - go-zero gorm extension (Tools and plugins for [go-zero](https://github.com/tal-tech/go-zero))
README
# gorm-zero
A go-zero gorm extension. If you use go-zero, and you want to use GORM. You can use this extension.## Installation
- Add the dependency
```shell
go get github.com/SpectatorNan/gorm-zero
```
- Replace `template/model` in your project with `gorm-zero/template/{goctl version}/model`
- Generate
```shell
goctl model mysql -src={patterns} -dir={dir} -cache --home ./template
```## Basic Usage
Currently we support two databases: MySQL and PostgreSQL. For example:### MySQL
* Config
```go
import (
"github.com/SpectatorNan/gorm-zero/gormc/config/mysql"
)
type Config struct {
Mysql mysql.Mysql
...
}
```
* Initialization
```go
import (
"github.com/SpectatorNan/gorm-zero/gormc/config/mysql"
)
func NewServiceContext(c config.Config) *ServiceContext {
db, err := mysql.Connect(c.Mysql)
if err != nil {
log.Fatal(err)
}
...
}
```### PostgreSQL
* Config
```go
import (
"github.com/SpectatorNan/gorm-zero/gormc/config/pg"
)
type Config struct {
PgSql pg.PgSql
...
}
```* Initialization
```go
import (
"github.com/SpectatorNan/gorm-zero/gormc/config/pg"
)
func NewServiceContext(c config.Config) *ServiceContext {
db, err := pg.Connect(c.PgSql)
if err != nil {
log.Fatal(err)
}
...
}
```## Quick Start
* Query with cache and custom expire duration
```go
gormzeroUsersIdKey := fmt.Sprintf("%s%v", cacheGormzeroUsersIdExpirePrefix, id)
var resp Users
err := m.QueryWithExpireCtx(ctx, &resp, gormzeroUsersIdKey, expire, func(conn *gorm.DB, v interface{}) error {
return conn.Model(&Users{}).Where("`id` = ?", id).First(&resp).Error
})
switch err {
case nil:
return &resp, nil
case gormc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
```* Query with cache and default expire duration
```go
gormzeroUsersIdKey := fmt.Sprintf("%s%v", cacheGormzeroUsersIdPrefix, id)
var resp Users
err := m.QueryCtx(ctx, &resp, gormzeroUsersIdKey, func(conn *gorm.DB, v interface{}) error {
return conn.Model(&Users{}).Where("`id` = ?", id).First(&resp).Error
})
switch err {
case nil:
return &resp, nil
case gormc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
```## Examples
- go zero model example link: [gorm-zero-example](https://github.com/SpectatorNan/gorm-zero-example)