{"id":16612330,"url":"https://github.com/taichi/kra","last_synced_at":"2026-03-08T16:35:36.630Z","repository":{"id":38111225,"uuid":"363377071","full_name":"taichi/kra","owner":"taichi","description":"relational database access helper library","archived":false,"fork":false,"pushed_at":"2025-07-25T05:48:36.000Z","size":498,"stargazers_count":34,"open_issues_count":3,"forks_count":2,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-07-25T10:56:05.231Z","etag":null,"topics":["antlr4","postgresql","rdb","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/taichi.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":"2021-05-01T09:59:28.000Z","updated_at":"2025-01-16T08:39:37.000Z","dependencies_parsed_at":"2025-03-31T17:40:00.267Z","dependency_job_id":"8de68321-8661-455a-a9af-adbbe8333b8d","html_url":"https://github.com/taichi/kra","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/taichi/kra","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taichi%2Fkra","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taichi%2Fkra/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taichi%2Fkra/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taichi%2Fkra/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/taichi","download_url":"https://codeload.github.com/taichi/kra/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taichi%2Fkra/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267023160,"owners_count":24022913,"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-07-25T02:00:09.625Z","response_time":70,"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":["antlr4","postgresql","rdb","sql"],"created_at":"2024-10-12T01:41:42.344Z","updated_at":"2026-03-08T16:35:36.568Z","avatar_url":"https://github.com/taichi.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# kra\n\n![](https://github.com/taichi/kra/actions/workflows/push.yml/badge.svg)\n[![PkgGoDev](https://pkg.go.dev/badge/https://pkg.go.dev/github.comtaichi/kra)](https://pkg.go.dev/github.com/taichi/kra)\n[![Go Report Card](https://goreportcard.com/badge/github.com/taichi/kra)](https://goreportcard.com/report/github.com/taichi/kra)\n\n**SQL is the best way to access database**.\n\nkra is a relational database access helper library on top of go.\n\nkra works with `database/sql`, so all of database with `database/sql` based driver is supported.\nand kra also works with `pgx` native API. kra focuses on the convenient use of `CopyFrom`.\n\n# Features\n\n- Named parameter support with dot notation\n- IN statement variable expansion\n- Rows to structure/map mapping\n- Selectable base API. pgx or database/sql\n- Highly configurable behavior\n- Context is required for network access APIs\n- All wrapper object has escape hatches\n\n# Getting Started\n\n## Install\n\n```\ngo get github.com/taichi/kra\n```\n\n## Usage\n\n### native pgx based API\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"database/sql\"\n\t\"fmt\"\n\t\"time\"\n\n\t\"github.com/jackc/pgtype\"\n\n\t\"github.com/taichi/kra/pgx\"\n)\n\ntype Film struct {\n\tCode     string\n\tTitle    string\n\tDid      int\n\tDateProd time.Time `db:\"date_prod\"`\n\tKind     string\n\tLen      pgtype.Interval\n}\n\nfunc main() {\n\tctx := context.Background()\n\n\tdb, err := pgx.Open(ctx, \"user=test password=test host=localhost port=5432 database=test sslmode=disable\")\n\tif err != nil {\n\t\tfmt.Println(\"open\", err)\n\t\treturn\n\t}\n\tdefer db.Close()\n\n\tif _, err := db.Exec(ctx, `CREATE TABLE IF NOT EXISTS films (\n\t    code        char(5) PRIMARY KEY,\n\t    title       varchar(40) NOT NULL,\n\t    did         integer NOT NULL,\n\t    date_prod   date,\n\t    kind        varchar(10),\n\t    len         interval hour to minute\n\t);`); err != nil {\n\t\tfmt.Println(\"create\", err)\n\t\treturn\n\t}\n\tdefer func() {\n\t\tif _, err := db.Exec(ctx, \"DROP TABLE films\"); err != nil {\n\t\t\tfmt.Println(err)\n\t\t}\n\t}()\n\n\ttestdata := []Film{\n\t\t{\"1111\", \"aaaa\", 32, time.Now(), \"CDR\", pgtype.Interval{Microseconds: 5400000000, Status: pgtype.Present}},\n\t\t{\"2222\", \"bbbb\", 34, time.Now(), \"ZDE\", pgtype.Interval{Microseconds: 9000000000, Status: pgtype.Present}},\n\t\t{\"3333\", \"cccc\", 65, time.Now(), \"IOM\", pgtype.Interval{Microseconds: 5400000000, Status: pgtype.Present}},\n\t\t{\"4444\", \"dddd\", 72, time.Now(), \"ERW\", pgtype.Interval{Microseconds: 7200000000, Status: pgtype.Present}},\n\t}\n\n\tif _, err := db.CopyFrom(ctx, pgx.Identifier{\"films\"}, testdata); err != nil {\n\t\tfmt.Println(\"CopyFrom\", err)\n\t\treturn\n\t}\n\n\tvar films []Film\n\tif err := db.FindAll(ctx, \u0026films, \"SELECT * FROM films WHERE kind IN (:kind)\", sql.NamedArg{Name: \"kind\", Value: []string{\"CDR\", \"ZDE\"}}); err != nil {\n\t\tfmt.Println(\"find\", err)\n\t\treturn\n\t}\n\n\tfmt.Printf(\"%v\\n\", films)\n}\n```\n\n### standard database/sql based API\n\n```go\npackage main\n\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"time\"\n\n\t\"github.com/jackc/pgtype\"\n\t_ \"github.com/jackc/pgx/v4/stdlib\"\n\n\t\"github.com/taichi/kra\"\n\t\"github.com/taichi/kra/sql\"\n)\n\ntype Film struct {\n\tCode     string\n\tTitle    string\n\tDid      int\n\tDateProd time.Time `db:\"date_prod\"`\n\tKind     string\n\tLen      pgtype.Interval\n}\n\nfunc main() {\n\tctx := context.Background()\n\n\tdb, err := sql.Open(kra.NewCore(kra.PostgreSQL), \"pgx\", \"user=test password=test host=localhost port=5432 database=test sslmode=disable\")\n\tif err != nil {\n\t\tfmt.Println(\"open\", err)\n\t\treturn\n\t}\n\tdefer db.Close()\n\n\tif _, err := db.Exec(ctx, `CREATE TABLE IF NOT EXISTS films (\n\t    code        char(5) PRIMARY KEY,\n\t    title       varchar(40) NOT NULL,\n\t    did         integer NOT NULL,\n\t    date_prod   date,\n\t    kind        varchar(10),\n\t    len         interval hour to minute\n\t);`); err != nil {\n\t\tfmt.Println(\"create\", err)\n\t\treturn\n\t}\n\tdefer func() {\n\t\tif _, err := db.Exec(ctx, \"DROP TABLE films\"); err != nil {\n\t\t\tfmt.Println(err)\n\t\t}\n\t}()\n\n\tif stmt, err := db.Prepare(ctx, \"INSERT INTO films (code, title, did, date_prod, kind, len) VALUES (:code, :title, :did, :date_prod, :kind, :len)\"); err != nil {\n\t\tfmt.Println(\"prepare\", err)\n\t\treturn\n\t} else {\n\t\ttestdata := []Film{\n\t\t\t{\"1111\", \"aaaa\", 32, time.Now(), \"CDR\", pgtype.Interval{Microseconds: 5400000000, Status: pgtype.Present}},\n\t\t\t{\"2222\", \"bbbb\", 34, time.Now(), \"ZDE\", pgtype.Interval{Microseconds: 9000000000, Status: pgtype.Present}},\n\t\t\t{\"3333\", \"cccc\", 65, time.Now(), \"IOM\", pgtype.Interval{Microseconds: 5400000000, Status: pgtype.Present}},\n\t\t\t{\"4444\", \"dddd\", 72, time.Now(), \"ERW\", pgtype.Interval{Microseconds: 7200000000, Status: pgtype.Present}},\n\t\t}\n\t\tfor _, data := range testdata {\n\t\t\tif _, err := stmt.Exec(ctx, data); err != nil {\n\t\t\t\tfmt.Println(\"insert\", err)\n\t\t\t\treturn\n\t\t\t}\n\t\t}\n\t\tif err := stmt.Close(); err != nil {\n\t\t\tfmt.Println(\"close\", err)\n\t\t\treturn\n\t\t}\n\t}\n\tvar films []Film\n\tif err := db.FindAll(ctx, \u0026films, \"SELECT * FROM films WHERE kind IN (:kind)\", kra.NamedArg{Name: \"kind\", Value: []string{\"CDR\", \"ZDE\"}}); err != nil {\n\t\tfmt.Println(\"find\", err)\n\t\treturn\n\t}\n\n\tfmt.Printf(\"%v\\n\", films)\n}\n```\n\n# Related OSS\n\n- [pgx](https://github.com/jackc/pgx)\n- [scany](https://github.com/georgysavva/scany)\n- [sqlx](https://github.com/jmoiron/sqlx)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftaichi%2Fkra","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftaichi%2Fkra","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftaichi%2Fkra/lists"}