{"id":18928456,"url":"https://github.com/nathan-osman/api2go-resource","last_synced_at":"2025-07-05T00:06:16.542Z","repository":{"id":57610170,"uuid":"122438082","full_name":"nathan-osman/api2go-resource","owner":"nathan-osman","description":"CRUD actions for using GORM models with api2go","archived":false,"fork":false,"pushed_at":"2018-10-04T04:36:01.000Z","size":39,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-25T01:43:50.031Z","etag":null,"topics":["api","golang","gorm","json-api"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nathan-osman.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-02-22T06:10:35.000Z","updated_at":"2025-02-21T19:41:54.000Z","dependencies_parsed_at":"2022-09-26T20:02:04.828Z","dependency_job_id":null,"html_url":"https://github.com/nathan-osman/api2go-resource","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/nathan-osman/api2go-resource","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nathan-osman%2Fapi2go-resource","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nathan-osman%2Fapi2go-resource/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nathan-osman%2Fapi2go-resource/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nathan-osman%2Fapi2go-resource/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nathan-osman","download_url":"https://codeload.github.com/nathan-osman/api2go-resource/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nathan-osman%2Fapi2go-resource/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263636796,"owners_count":23492305,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api","golang","gorm","json-api"],"created_at":"2024-11-08T11:25:46.139Z","updated_at":"2025-07-05T00:06:16.521Z","avatar_url":"https://github.com/nathan-osman.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"## api2go-resource\n\n[![Build Status](https://travis-ci.org/nathan-osman/api2go-resource.svg?branch=master)](https://travis-ci.org/nathan-osman/api2go-resource)\n[![GoDoc](https://godoc.org/github.com/nathan-osman/api2go-resource?status.svg)](https://godoc.org/github.com/nathan-osman/api2go-resource)\n[![MIT License](http://img.shields.io/badge/license-MIT-9370d8.svg?style=flat)](http://opensource.org/licenses/MIT)\n\nThis 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.\n\n### Features\n\nHere are some of the features that api2go-resource provides:\n\n- Works with your existing GORM models\n- Enables filtering to be limited to specific fields\n- Provides hooks to enable access control and normalization\n\n### Usage\n\nLet's suppose you have the following model definition:\n\n```go\ntype Article struct {\n    ID      int64\n    Title   string\n    Content string\n}\n```\n\nIn order to use the model with api2go, three important methods must be implemented:\n\n```go\nfunc (a *Article) GetName() string {\n    return \"articles\"\n}\n\nfunc (a *Article) GetID() string {\n    return strconv.FormatInt(a.ID, 10)\n}\n\nfunc (a *Article) SetID(id string) error {\n    a.ID, _ = strconv.ParseInt(id, 10, 64)\n    return nil\n}\n```\n\nThe next step is to create a `Resource` instance for the model:\n\n```go\nimport \"github.com/nathan-osman/api2go-resource\"\n\n// db is an instance of *gorm.DB\n\nvar articleResource = \u0026resource.Resource{\n    DB:   db,\n    Type: \u0026Article{},\n}\n```\n\nThis resource can now be registered with api2go:\n\n```go\napi := api2go.NewAPI(\"api\")\napi.AddResource(\u0026Article{}, articleResource)\n```\n\n### Hooks\n\nHooks can be used for a number of different purposes.\n\nFor example, to make a resource read-only:\n\n```go\nfunc readOnly(p *resource.Params) error {\n    switch p.Action {\n    case resource.BeforeCreate, resource.BeforeDelete, resource.BeforeUpdate:\n        return api2go.NewHTTPError(nil, \"read only\", http.StatusBadRequest)\n    }\n    return nil\n}\n\nvar articleResource = \u0026resource.Resource{\n    // ...\n    Hooks: []Hook{readOnly},\n}\n```\n\nTo ensure that articles are always retrieved in alphabetical order:\n\n```go\nfunc alphabeticalOrder(p *resource.Params) error {\n    switch p.Action {\n    case resource.BeforeFindAll, resource.BeforeFindOne:\n        p.DB = p.DB.Order('title')\n    }\n    return nil\n}\n\nvar articleResource = \u0026resource.Resource{\n    // ...\n    Hooks: []Hook{alphabeticalOrder},\n}\n```\n\nTo remove any extra whitespace in an article title before saving:\n\n```go\nfunc trimTitle(p *resource.Params) error {\n    switch p.Action {\n    case resource.BeforeCreate, resource.BeforeUpdate:\n        p.Obj.Title = strings.TrimSpace(p.Obj.Title)\n    }\n    return nil\n}\n\nvar articleResource = \u0026resource.Resource{\n    // ...\n    Hooks: []Hook{trimTitle},\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnathan-osman%2Fapi2go-resource","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnathan-osman%2Fapi2go-resource","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnathan-osman%2Fapi2go-resource/lists"}