{"id":19764311,"url":"https://github.com/grahms/xgor","last_synced_at":"2026-05-10T22:49:40.231Z","repository":{"id":211919670,"uuid":"730270950","full_name":"GraHms/xgor","owner":"GraHms","description":"Xgor is a library that extends Gorm to provide additional functionalities for building robust database repositories with support for custom filters, transactions, and relationship handling.","archived":false,"fork":false,"pushed_at":"2023-12-11T15:09:36.000Z","size":7,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-24T16:17:02.982Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/GraHms.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-12-11T15:06:43.000Z","updated_at":"2024-03-21T12:35:13.000Z","dependencies_parsed_at":"2023-12-11T16:29:51.565Z","dependency_job_id":"a23e23d8-5396-4e54-96ae-28469bb898b8","html_url":"https://github.com/GraHms/xgor","commit_stats":null,"previous_names":["grahms/xgor"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GraHms%2Fxgor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GraHms%2Fxgor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GraHms%2Fxgor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GraHms%2Fxgor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GraHms","download_url":"https://codeload.github.com/GraHms/xgor/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241096182,"owners_count":19908931,"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":[],"created_at":"2024-11-12T04:13:23.560Z","updated_at":"2026-05-10T22:49:35.209Z","avatar_url":"https://github.com/GraHms.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# xgor\n\n`xgor` is a library that extends [Gorm](https://gorm.io/) to provide additional functionalities for building robust database repositories with support for custom filters, transactions, and relationship handling.\n\n## Features\n\n- **Generic Repository:** Use generic repository patterns to handle common CRUD operations for your Gorm models.\n\n- **Custom Filters:** Easily filter entities based on custom conditions using a flexible and intuitive filter syntax.\n\n- **Transaction Support:** Perform operations within a transaction to ensure consistency and atomicity.\n\n- **Relationship Handling:** Simplify relationship management with built-in functions for clearing relationships.\n\n## Installation\n\n```bash\ngo get -u github.com/grahms/xgor\n```\n\n## Usage\n\n### Initializing a Repository\n\n```go\nimport (\n    \"gorm.io/gorm\"\n    \"github.com/grahms/xgor\"\n)\n\n// Initialize  DB\ndb, err := xgor.Open(...)\n\n// Create a new repository\nrepo := xgor.New[BlogPost](db, errors.New(\"blog post not found\"))\n\n// Or with relationships\nrepoWithRelations := xgor.NewWithRelationships[BlogPost](db, errors.New(\"blog post not found\"), \"comments\", \"author\")\n```\n\n### Adding a Blog Post\n\n```go\npost := \u0026BlogPost{\n    Title:       \"Introduction to xgor\",\n    Content:     \"Learn how to use xgor to supercharge your Gorm-based repositories.\",\n    AuthorID:    1,\n    CategoryID:   2,\n    PublishedAt:  time.Now(),\n}\nerr := repo.Add(post)\n```\n\n### Querying Blog Posts with Custom Filters\n\n```go\n// Get all published posts in the \"Technology\" category written by a specific author\nfilters := xgor.FilterType{\n    \"published_at__lte\": time.Now(),\n    \"category.name__eq\": \"Technology\",\n    \"author.id__eq\":     1,\n}\nposts, err := repo.GetAll(nil, nil, nil, filters)\n```\n\n### Performing a Transaction\n\n```go\nerr := repo.PerformTransaction(func(tx *gorm.DB) error {\n    // Update the author's profile and add a new blog post within the same transaction\n    author, err := authorRepo.GetByID(1)\n    if err != nil {\n        return err\n    }\n\n    author.Name = \"Updated Author Name\"\n    if err := authorRepo.Update(author); err != nil {\n        return err\n    }\n\n    newPost := \u0026BlogPost{\n        Title:       \"Advanced xgor Techniques\",\n        Content:     \"Explore advanced techniques for optimizing database queries with xgor.\",\n        AuthorID:    1,\n        CategoryID:   3,\n        PublishedAt:  time.Now(),\n    }\n\n    return repo.Add(newPost)\n})\n```\n\n## Example Use Case: Blogging Application\n\nLet's consider a blogging application where `xgor` is used to manage blog posts. In this scenario, `xgor` simplifies the data access layer, allowing developers to focus on building features rather than dealing with intricate database operations.\n\n### Use Case Scenario\n\n- **Scenario:** The application needs to fetch all published blog posts in a specific category written by a particular author.\n\n- **Solution:** Utilize `xgor`'s custom filters to easily query the database and retrieve the required blog posts without the complexity of crafting intricate SQL queries.\n\n```go\n// Example: Get all published posts in the \"Technology\" category written by a specific author\nfilters := xgor.FilterType{\n    \"published_at__lte\": time.Now(),\n    \"category.name__eq\": \"Technology\",\n    \"author.id__eq\":     1,\n}\nposts, err := repo.GetAll(nil, nil, nil, filters)\n```\n## Example Use Case: Blogging Application (Pagination)\n\n### Use Case Scenario\n\n- **Scenario:** The blogging application needs to display a paginated list of blog posts on the homepage.\n\n- **Solution:** Utilize `xgor` to implement pagination and retrieve a subset of blog posts for display.\n\n```go\n// Example: Get paginated blog posts for the homepage\nlimit := 10  // Number of posts per page\npage := 1    // Current page\norderBy := \"published_at desc\"  // Order posts by published date in descending order\n\n// Use xgor to get paginated blog posts\npaginationFilters := xgor.FilterType{\"category_id__eq\": 1}  // Filter by category ID, if needed\nblogPosts, err := repo.GetAll(\u0026limit, \u0026page, \u0026orderBy, paginationFilters)\n\n// Check for errors and handle the paginated blog posts\nif err != nil {\n    // Handle error\n} else {\n    // Access paginated results\n    totalPosts := blogPosts.TotalCount\n    currentPage := page\n    postsPerPage := limit\n    resultCount := blogPosts.ResultCount\n    displayedPosts := *blogPosts.Items\n\n    // Process and display paginated blog posts\n    for _, post := range displayedPosts {\n        // Process each blog post\n    }\n}\n```\n## Custom Filters\n\nCustom filters allow you to specify conditions for filtering entities. The filter syntax is based on the column name and a suffix that represents the condition. Here is a table of available filters:\n\n| Filter          | Description                                  | Example                           |\n|------------------|----------------------------------------------|-----------------------------------|\n| `__eq`           | Equals                                       | `\"age__eq\": 25`                   |\n| `__gt`           | Greater Than                                  | `\"age__gt\": 21`                   |\n| `__lt`           | Less Than                                     | `\"age__lt\": 30`                   |\n| `__gte`          | Greater Than or Equal To                      | `\"age__gte\": 21`                  |\n| `__lte`          | Less Than or Equal To                         | `\"age__lte\": 30`                  |\n| `__in`           | In Array                                      | `\"age__in\": []int{25, 30}`        |\n| `__not`          | Not Equal To                                  | `\"age__not\": 25`                  |\n| `__not_in`       | Not In Array                                  | `\"age__not_in\": []int{25, 30}`    |\n| `__like`         | Like (substring match)                        | `\"name__like\": \"John\"`            |\n\nCombine these filters to create powerful and flexible queries tailored to your application's needs.\n\n## Contributing\n\nFeel free to contribute by opening issues or submitting pull requests. Please follow the [Contributing Guidelines](CONTRIBUTING.md).\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgrahms%2Fxgor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgrahms%2Fxgor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgrahms%2Fxgor/lists"}