{"id":37898127,"url":"https://github.com/ysomad/pgxatomic","last_synced_at":"2026-01-16T17:02:21.999Z","repository":{"id":62328962,"uuid":"559643103","full_name":"ysomad/pgxatomic","owner":"ysomad","description":"Transaction manager for pgx","archived":false,"fork":false,"pushed_at":"2025-11-01T14:31:11.000Z","size":27,"stargazers_count":11,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-11-01T16:14:50.902Z","etag":null,"topics":["clean-architecture","go","golang","hexagonal-architecture","pgx","postgresql","transactions"],"latest_commit_sha":null,"homepage":"","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/ysomad.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}},"created_at":"2022-10-30T18:38:45.000Z","updated_at":"2025-11-01T15:15:32.000Z","dependencies_parsed_at":"2022-10-30T21:45:27.361Z","dependency_job_id":null,"html_url":"https://github.com/ysomad/pgxatomic","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/ysomad/pgxatomic","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ysomad%2Fpgxatomic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ysomad%2Fpgxatomic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ysomad%2Fpgxatomic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ysomad%2Fpgxatomic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ysomad","download_url":"https://codeload.github.com/ysomad/pgxatomic/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ysomad%2Fpgxatomic/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28480081,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-16T11:59:17.896Z","status":"ssl_error","status_checked_at":"2026-01-16T11:55:55.838Z","response_time":107,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["clean-architecture","go","golang","hexagonal-architecture","pgx","postgresql","transactions"],"created_at":"2026-01-16T17:02:21.898Z","updated_at":"2026-01-16T17:02:21.990Z","avatar_url":"https://github.com/ysomad.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# pgxatomic\n\nA library for managing PostgreSQL transactions through context propagation using the [pgx](https://github.com/jackc/pgx) driver.\n\npgxatomic allows you to pass transactions via `context.Context`, providing cleaner separation between business logic and transaction management. Repository and service layers work with contexts while transaction boundaries are controlled at higher levels.\n\n![schema](https://i.imgur.com/RpsfuBb.jpg)\n\n## Installation\n\n```bash\ngo get github.com/ysomad/pgxatomic\n```\n\n## Usage\n\n### Repository layer\n\nUse `pgxatomic.Pool` in your repository. It wraps `pgxpool.Pool` and automatically uses transactions from context when available.\n\n```go\ntype orderRepo struct {\n    pool *pgxatomic.Pool\n}\n\ntype order struct {\n    ID   uuid.UUID\n    Cost int\n}\n\nfunc (r *orderRepo) Insert(ctx context.Context, cost int) order {\n    rows, _ := r.pool.Query(ctx, \"INSERT INTO orders(cost) VALUES ($1) RETURNING id, cost\", cost)\n    o, _ := pgx.CollectOneRow(rows, pgx.RowToStructByPos[order])\n    return o\n}\n```\n\nAlternatively, use `Query`, `QueryRow`, and `Exec` functions directly without the pool wrapper.\n\n### Transaction management\n\nUse `Runner` to execute operations within a transaction. The transaction propagates automatically through context.\n\n```go\nconf, _ := pgxpool.ParseConfig(\"postgres://user:pass@localhost:5432/postgres\")\npool, _ := pgxpool.NewWithConfig(context.Background(), conf)\n\nrunner, _ := pgxatomic.NewRunner(pool, pgx.TxOptions{})\n\n_ = runner.Run(context.Background(), func(txCtx context.Context) error {\n    _ = orderService.Create(txCtx)\n    _ = balanceService.Withdraw(txCtx)\n    return nil\n})\n```\n\nAll operations within `Run` use the same transaction. If the function returns an error, the transaction rolls back. Otherwise, it commits.\n\nNote: Error handling is omitted for brevity. Handle errors appropriately in production code.\n\n## References\n\n- [Clean transactions in Golang hexagon](https://www.kaznacheev.me/posts/en/clean-transactions-in-hexagon)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fysomad%2Fpgxatomic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fysomad%2Fpgxatomic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fysomad%2Fpgxatomic/lists"}