{"id":21406446,"url":"https://github.com/arturoeanton/go-struct2serve","last_synced_at":"2025-03-16T17:16:14.525Z","repository":{"id":180550773,"uuid":"665316176","full_name":"arturoeanton/go-struct2serve","owner":"arturoeanton","description":"This is a small library for echo v4 that has a generic repository for accessing the database and populating structures in golang and has a generic controller and service.  You can create a custom repository or custom controller using Go compose.","archived":false,"fork":false,"pushed_at":"2023-07-20T04:09:33.000Z","size":51,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-23T04:11:10.150Z","etag":null,"topics":["database","echo","echo-framework","go","golang","golang-database","golang-library","gorm","repository","repository-pattern","sql"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/arturoeanton.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-07-12T00:10:28.000Z","updated_at":"2023-07-18T09:40:23.000Z","dependencies_parsed_at":null,"dependency_job_id":"bb700675-916f-4e6f-8168-72530204d2c7","html_url":"https://github.com/arturoeanton/go-struct2serve","commit_stats":null,"previous_names":["arturoeanton/go-struct2serve"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arturoeanton%2Fgo-struct2serve","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arturoeanton%2Fgo-struct2serve/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arturoeanton%2Fgo-struct2serve/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arturoeanton%2Fgo-struct2serve/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arturoeanton","download_url":"https://codeload.github.com/arturoeanton/go-struct2serve/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243902320,"owners_count":20366262,"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":["database","echo","echo-framework","go","golang","golang-database","golang-library","gorm","repository","repository-pattern","sql"],"created_at":"2024-11-22T16:39:58.286Z","updated_at":"2025-03-16T17:16:14.463Z","avatar_url":"https://github.com/arturoeanton.png","language":"Go","readme":"# go-struct2serve\nThis is a small library for [echo v4](https://github.com/labstack/echo) that has a generic repository for accessing the database and populating structures in golang and has a generic controller and service.\n\nYou can create a custom repository or custom controller using Go compose.\n\n\n## Question\n\n* Why Echo? Becouse It is my favorite framwork for make web apps :P.\n* Why SQL? **I love SQL**.\n* Can you add feature? **yes**, I love you ;).\n* Why did I make this library? Because I have ***free time***, but it doesn't mean I don't love GORM :).\n* Why make this project? This project is a hobby and We use it in some minor projects. **This is fun**.\n\n\n## Repository\nThis repository provides a generic implementation of repository patterns in Go using SQL databases. It allows you to write less code and easily perform CRUD operations. The library supports deep object graph navigation and loading, thanks to tags that can specify how to load the data.\n\n## Features\n\n* Full CRUD operations\n* Deep object graph navigation\n* Transaction support\n* Custom SQL queries through tags\n* Custom loading of nested structures\n\n## How It Works\n\nThis library uses the reflect package to inspect your data models at runtime. It uses the db tag to map the struct fields to the database columns and it uses custom s2s tags to define how to load the data.\n\nFor example, the tag s2s:\"id in (select role_id from user_roles where user_id = ?)\" tells the library to execute this SQL query to load the roles for a user. The ? placeholder will be replaced with the ID of the user.\n\nYou can also specify how to load nested structures using the s2s tag. For example, s2s:\"id = ?\" s2s_param:\"GroupId\" tells the library to load the group for a user using the GroupId value.\n\n\n## Example \n\n#### user_model.go\n``` go\npackage models\n\ntype User struct {\n\tUserID    int     `json:\"id\" db:\"id\" s2s_id:\"true\"` // mark this field as id with tag s2s_id:\"true\"\n\tFirstName string  `json:\"first_name\" db:\"first_name\"`\n\tEmail     string  `json:\"email\" db:\"email\"`\n\tRoles     *[]Role `json:\"roles,omitempty\" s2s:\"id in (select role_id from user_roles where user_id = ?)\"` // not use s2s_param becuase s2s_param is the id of Struct\n\tGroupId   *int    `json:\"-\" db:\"group_id\" s2s_ref_value:\"MyGroup.ID\"`                                    // mark this field as id with tag s2s_ref_value:\"Group.ID\" because json not send nil values json:\"-\"\n\tMyGroup     *Group  `json:\"group,omitempty\" s2s:\"id = ?\" s2s_param:\"GroupId\"`                               // use s2s_param becuase we need use GroupId value\n\t//other way is  MyGroup *Group `json:\"group,omitempty\" s2s:\"select * from groups where id = ?\" sql_param:\"GroupId\"`\n}\n\n```\n\n#### role_model.go\n``` go\npackage models\n\ntype Role struct {\n\tID    int     `json:\"id\" db:\"id\" s2s_table_name:\"roles\"` // use s2s_table_name:\"roles\" because table name is not the same as struct name\n\tName  string  `json:\"name\" db:\"name\"`\n\tUsers *[]User `json:\"users,omitempty\" s2s:\"id in (select user_id from user_roles where role_id = ?)\"` // not use s2s_param becuase s2s_param is the id of Struct\n}\n```\n\n#### group_model.go\n``` go\npackage models\n\ntype Group struct {\n\tID    int     `json:\"id\" db:\"id\" s2s_table_name:\"groups\"` // use s2s_table_name:\"groups\" because table name is not the same as struct name\n\tName  string  `json:\"name\" db:\"name\"`\n\tUsers *[]User `json:\"users,omitempty\" s2s:\"group_id = ?\"` // not use s2s_param becuase s2s_param is the id of Struct\n}\n```\n\n## Example custom **repository**\n\n#### user_repositories.go\n\n* The project is \"springhub\" and the folder models is springhub/models\n\n``` go\npackage customs\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"log\"\n\n\t\"github.com/arturoeanton/go-struct2serve/config\"\n\t\"github.com/arturoeanton/go-struct2serve/repositories\"\n\t\"github.com/arturoeanton/springhub/models\"\n)\n\ntype UserRepository struct {\n\trepositories.Repository[models.User]\n}\n\nfunc NewUserRepository() *UserRepository {\n\treturn \u0026UserRepository{\n\t\tRepository: *repositories.NewRepository[models.User](),\n\t}\n}\n\nfunc (ur *UserRepository) GetAll() ([]*models.User, error) {\n\tfmt.Println(\"Custom GetAll\")\n\tconn, err := config.DB.Conn(context.Background())\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tdefer conn.Close()\n\tquery := \"SELECT * FROM user\"\n\trows, err := conn.QueryContext(context.Background(), query)\n\tif err != nil {\n\t\tlog.Printf(\"Error al ejecutar la consulta: %v\", err)\n\t\treturn nil, err\n\t}\n\tdefer rows.Close()\n\n\tusers := []*models.User{}\n\tfor rows.Next() {\n\t\tuser := \u0026models.User{}\n\t\terr := rows.Scan(\u0026user.ID, \u0026user.FirstName, \u0026user.Email)\n\t\tif err != nil {\n\t\t\tlog.Printf(\"Error al escanear la fila: %v\", err)\n\t\t\treturn nil, err\n\t\t}\n\t\tusers = append(users, user)\n\t}\n\n\treturn users, nil\n}\n```\n\n\n\n## Example custom **handler**\n\n\n#### project_handlers.go\n\n* The project is \"springhub\" and the folder models is springhub/models\n\n``` go\npackage customs\n\nimport (\n\t\"net/http\"\n\n\t\"github.com/arturoeanton/go-struct2serve/handlers\"\n\t\"github.com/arturoeanton/go-struct2serve/repositories\"\n\t\"github.com/arturoeanton/go-struct2serve/services\"\n\t\"github.com/arturoeanton/springhub/models\"\n\t\"github.com/labstack/echo/v4\"\n)\n\ntype ProjectHandler struct {\n\t*handlers.Handler[models.Project]\n\tprojectService services.IService[models.Project]\n}\n\nfunc NewProjectHandler() *ProjectHandler {\n\treturn \u0026ProjectHandler{\n\t\tHandler: handlers.NewHandler[models.Project](),\n\t\tprojectService: services.NewService[models.Project](\n\t\t\trepositories.NewRepository[models.Project](),\n\t\t),\n\t}\n}\n\nfunc (uh *ProjectHandler) FilterByNameOrDesciption(c echo.Context) error {\n\tname := c.QueryParam(\"name\")\n\tdescription := c.QueryParam(\"description\")\n\n\tprojects, err := uh.projectService.GetByCriteria(\"name like ? or description  like ? \", \"%\"+name+\"%\", \"%\"+description+\"%\")\n\tif err != nil {\n\t\treturn c.JSON(http.StatusInternalServerError, map[string]string{\n\t\t\t\"error\": \"Failed to get projects\",\n\t\t})\n\t}\n\treturn c.JSON(http.StatusOK, projects)\n}\n```\n\n\n\n## Transactions\n\nThe library also supports transactions. You can create a new transaction and set it on your repositories:\n\n```go\ntx, _ := CreateTxAndSet(repoUser, repoRole)\n```\nThen, you can use the Commit() and Rollback() methods to control the transaction:\n\n```go\nerr := repoUser.Commit()\nif err != nil {\n\t// handle error\n}\n\nerr = repoUser.Rollback()\nif err != nil {\n\t// handle error\n}\n```\n\n# Custom SQL Queries\n\nYou can execute custom SQL queries using the **GetByCriteria()** method. This method takes a SQL query string and any number of arguments for the query parameters:\n\n```go\nusers, _ := repoUser.GetByCriteria(\"first_name = ? AND email = ?\", \"admin\", \"admin@admin.com\")\n```\n\n\n## Installation\n\nUse the go get command to install this library:\n\n```sh\ngo get github.com/arturoeanton/go-struct2serve\n```\n\n## Tests\n\nThis library includes a test suite. You can run the tests using the go test command:\n\n```sh\ngo test ./...\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farturoeanton%2Fgo-struct2serve","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farturoeanton%2Fgo-struct2serve","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farturoeanton%2Fgo-struct2serve/lists"}