{"id":27288374,"url":"https://github.com/go-universal/sql","last_synced_at":"2025-08-12T09:10:31.310Z","repository":{"id":287169447,"uuid":"963312312","full_name":"go-universal/sql","owner":"go-universal","description":"🔥 SQL migration and utility toolkit for Go.","archived":false,"fork":false,"pushed_at":"2025-08-08T15:09:15.000Z","size":58,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-08T17:15:42.306Z","etag":null,"topics":["database-migration","golang","mysql","postgres","postgresql"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/go-universal.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,"zenodo":null}},"created_at":"2025-04-09T13:40:27.000Z","updated_at":"2025-08-08T15:09:18.000Z","dependencies_parsed_at":"2025-04-10T10:49:50.595Z","dependency_job_id":"80c393ec-0a7d-47b4-98fc-6950f1890c60","html_url":"https://github.com/go-universal/sql","commit_stats":null,"previous_names":["go-universal/sql"],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/go-universal/sql","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-universal%2Fsql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-universal%2Fsql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-universal%2Fsql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-universal%2Fsql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/go-universal","download_url":"https://codeload.github.com/go-universal/sql/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-universal%2Fsql/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270033321,"owners_count":24515479,"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","status":"online","status_checked_at":"2025-08-12T02:00:09.011Z","response_time":80,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["database-migration","golang","mysql","postgres","postgresql"],"created_at":"2025-04-11T20:46:11.104Z","updated_at":"2025-08-12T09:10:31.285Z","avatar_url":"https://github.com/go-universal.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Go SQL Utility Documentation\n\n![GitHub Tag](https://img.shields.io/github/v/tag/go-universal/sql?sort=semver\u0026label=version)\n[![Go Reference](https://pkg.go.dev/badge/github.com/go-universal/sql.svg)](https://pkg.go.dev/github.com/go-universal/sql)\n[![License](https://img.shields.io/badge/license-ISC-blue.svg)](https://github.com/go-universal/sql/blob/main/LICENSE)\n[![Go Report Card](https://goreportcard.com/badge/github.com/go-universal/sql)](https://goreportcard.com/report/github.com/go-universal/sql)\n![Contributors](https://img.shields.io/github/contributors/go-universal/sql)\n![Issues](https://img.shields.io/github/issues/go-universal/sql)\n\n`sql` is a Go library designed to simplify database interactions, migrations, and query management.\n\n## Packages\n\n### Query Builder\n\nThe ConditionBuilder provide functions for dynamically constructing SQL conditions.\n\n```go\nimport \"github.com/go-universal/sql/query\"\n\nfunc main() {\n    cond := query.NewCondition(query.NumbericResolver)\n    cond.And(\"name = ?\", \"John\").\n        AndClosure(\"age \u003e ? AND age \u003c ?\", 9, 31).\n        OrIf(false, \"age IS NULL\").\n        OrClosureIf(true, \"membership @in\", \"admin\", \"manager\", \"accountant\")\n\n    // Result: \"name = $1 AND (age \u003e $2 AND age \u003c $3) OR (membership IN ($4, $5, $6))\"\n}\n```\n\n### Query Manager\n\nThe `query` package provides tools for managing and generating SQL queries.\n\n#### Example\n\n```go\nimport (\n    \"github.com/go-universal/sql/query\"\n    \"github.com/go-universal/fs\"\n)\nfunc main() {\n    queriesFS := fs.NewDir(\"database/queries\")\n    queryManager, _err_ := query.NewQueryManager(fs, query.WithRoot(\"database\"))\n\n    usersList :=  queryManager.Get(\"queries/users/users_list\")\n    usersTrash :=  queryManager.Get(\"queries/users/deleted users\")\n    customers, exists :=  queryManager.Find(\"queries/customers/list\")\n    customers, exists :=  queryManager.Find(\"queries/customers/deleted\") // \"\", false\n}\n```\n\nQuery files style:\n\n```sql\n-- users.sql\n\n-- { query: users_list }\nSELECT * FROM users WHERE `deleted_at` IS NULL AND `name` LIKE ?;\n\n-- { query: deleted users }\nSELECT * FROM users WHERE deleted_at IS NOT NULL;\n\n\n-- customers.sql\n-- { query: list }\nSELECT * from customers;\n```\n\n### Postgres Package\n\nThe `postgres` package provides tools for constructing and executing SQL commands specifically for PostgreSQL databases. Query placeholders must `?`.\n\n```go\npackage main\n\nimport (\n    \"context\"\n\n    \"github.com/go-universal/sql/postgres\"\n    \"github.com/jackc/pgx/v5/pgconn\"\n)\n\nfunc main() {\n    ctx := context.Background()\n    config := postgres.NewConfig().\n        Host(\"localhost\").\n        Port(5432).\n        User(\"postgres\").\n        Password(\"password\").\n        Database(\"test\").\n        SSLMode(\"disable\").\n        MinConns(2)\n    conn, err := postgres.New(\n        ctx, config.Build(),\n        func(c *pgxpool.Config) { c.MaxConns = 7 },\n    )\n    defer conn.Close(ctx)\n\n    cmd := postgres.NewCmd(conn)\n    result, err := cmd.Command(\"INSERT INTO users (name) VALUES (?)\").Exec(ctx, \"John Doe\")\n}\n```\n\n### MySQL Package\n\nThe `mysql` package provides tools for constructing and executing SQL commands specifically for MySQL databases.\n\n```go\npackage main\n\nimport (\n    \"context\"\n\n    \"github.com/go-universal/sql/mysql\"\n)\n\nfunc main() {\n    conn, err := mysql.New(\n        context.Background(),\n        mysql.NewConfig().Database(\"test\").Password(\"root\").Build(),\n    )\n    if err != nil {\n        log.Fatal(err)\n    }\n\n    cmd := mysql.NewCmd(conn)\n    result, err := cmd.Command(\"INSERT INTO users (name) VALUES (?)\").Exec(context.Background(), \"John Doe\")\n}\n```\n\n### Migration Package\n\nThe `migration` package provides tools for managing database migrations by stage.\n\n```go\npackage main\n\nimport (\n    \"log\"\n\n    \"github.com/go-universal/fs\"\n    \"github.com/go-universal/sql/migration\"\n    \"github.com/go-universal/sql/mysql\"\n)\n\nfunc main() {\n    conn := CreateConnection()\n    fs := CreateFS()\n    mig, err := migration.NewMigration(\n        migration.NewMySQLSource(conn),\n        fs,\n        migration.WithRoot(\"migrations\"),\n    )\n\n    err := mig.Up([]string{\"table\", \"index\", \"seed\"})\n    if err != nil {\n        log.Fatal(err)\n    }\n}\n```\n\nMigration files style:\n\n```sql\n-- 1741791024-create-users-table.sql\n-- { up: table } table is sectin name\nCREATE TABLE IF NOT EXISTS ...\n\n-- { down: table }\n\n-- { up: index }\n...\n\n-- { down: index }\n...\n\n-- { up: seed }\n...\n\n-- { down: seed }\n...\n```\n\n## License\n\nThis library is licensed under the ISC License. See the [LICENSE](LICENSE) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgo-universal%2Fsql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgo-universal%2Fsql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgo-universal%2Fsql/lists"}