https://github.com/nathan-osman/api2go-resource
CRUD actions for using GORM models with api2go
https://github.com/nathan-osman/api2go-resource
api golang gorm json-api
Last synced: 2 months ago
JSON representation
CRUD actions for using GORM models with api2go
- Host: GitHub
- URL: https://github.com/nathan-osman/api2go-resource
- Owner: nathan-osman
- License: mit
- Created: 2018-02-22T06:10:35.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2018-10-04T04:36:01.000Z (over 6 years ago)
- Last Synced: 2024-12-31T19:16:07.492Z (4 months ago)
- Topics: api, golang, gorm, json-api
- Language: Go
- Size: 38.1 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
## api2go-resource
[](https://travis-ci.org/nathan-osman/api2go-resource)
[](https://godoc.org/github.com/nathan-osman/api2go-resource)
[](http://opensource.org/licenses/MIT)This package serves as a bridge between [GORM](https://github.com/jinzhu/gorm) and [api2go](https://github.com/manyminds/api2go), reducing the amount of boilerplate code needed for implementing CRUD actions for GORM models.
### Features
Here are some of the features that api2go-resource provides:
- Works with your existing GORM models
- Enables filtering to be limited to specific fields
- Provides hooks to enable access control and normalization### Usage
Let's suppose you have the following model definition:
```go
type Article struct {
ID int64
Title string
Content string
}
```In order to use the model with api2go, three important methods must be implemented:
```go
func (a *Article) GetName() string {
return "articles"
}func (a *Article) GetID() string {
return strconv.FormatInt(a.ID, 10)
}func (a *Article) SetID(id string) error {
a.ID, _ = strconv.ParseInt(id, 10, 64)
return nil
}
```The next step is to create a `Resource` instance for the model:
```go
import "github.com/nathan-osman/api2go-resource"// db is an instance of *gorm.DB
var articleResource = &resource.Resource{
DB: db,
Type: &Article{},
}
```This resource can now be registered with api2go:
```go
api := api2go.NewAPI("api")
api.AddResource(&Article{}, articleResource)
```### Hooks
Hooks can be used for a number of different purposes.
For example, to make a resource read-only:
```go
func readOnly(p *resource.Params) error {
switch p.Action {
case resource.BeforeCreate, resource.BeforeDelete, resource.BeforeUpdate:
return api2go.NewHTTPError(nil, "read only", http.StatusBadRequest)
}
return nil
}var articleResource = &resource.Resource{
// ...
Hooks: []Hook{readOnly},
}
```To ensure that articles are always retrieved in alphabetical order:
```go
func alphabeticalOrder(p *resource.Params) error {
switch p.Action {
case resource.BeforeFindAll, resource.BeforeFindOne:
p.DB = p.DB.Order('title')
}
return nil
}var articleResource = &resource.Resource{
// ...
Hooks: []Hook{alphabeticalOrder},
}
```To remove any extra whitespace in an article title before saving:
```go
func trimTitle(p *resource.Params) error {
switch p.Action {
case resource.BeforeCreate, resource.BeforeUpdate:
p.Obj.Title = strings.TrimSpace(p.Obj.Title)
}
return nil
}var articleResource = &resource.Resource{
// ...
Hooks: []Hook{trimTitle},
}
```