{"id":15672350,"url":"https://github.com/posener/orm","last_synced_at":"2025-07-11T12:45:30.117Z","repository":{"id":57577587,"uuid":"111158405","full_name":"posener/orm","owner":"posener","description":"Go Typed ORM","archived":false,"fork":false,"pushed_at":"2020-03-13T18:47:59.000Z","size":1272,"stargazers_count":16,"open_issues_count":2,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-06T21:11:20.575Z","etag":null,"topics":["code-generation","go","golang","orm","sql"],"latest_commit_sha":null,"homepage":null,"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/posener.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-11-17T22:55:42.000Z","updated_at":"2021-12-04T18:00:56.000Z","dependencies_parsed_at":"2022-09-11T22:51:45.324Z","dependency_job_id":null,"html_url":"https://github.com/posener/orm","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/posener/orm","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/posener%2Form","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/posener%2Form/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/posener%2Form/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/posener%2Form/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/posener","download_url":"https://codeload.github.com/posener/orm/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/posener%2Form/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264811944,"owners_count":23667839,"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":["code-generation","go","golang","orm","sql"],"created_at":"2024-10-03T15:24:32.603Z","updated_at":"2025-07-11T12:45:30.029Z","avatar_url":"https://github.com/posener.png","language":"Go","readme":"# Go ORM\n\n[![Build Status](https://travis-ci.org/posener/orm.svg?branch=master)](https://travis-ci.org/posener/orm)\n[![GoDoc](https://godoc.org/github.com/posener/orm?status.svg)](http://godoc.org/github.com/posener/orm)\n[![Go Report Card](https://goreportcard.com/badge/github.com/posener/orm)](https://goreportcard.com/report/github.com/posener/orm)\n\nAn attempt to write a **typed** ORM for Go.\n\nCheck out the [Wiki](https://github.com/posener/orm/wiki) for documentation.\n\nThis repository gives a command line tool, called `orm`, for generating\nORM code for a given struct. The generated code is typed and has no `interface{}`s arguments\nand returns values as in other ORM Go libraries.\n\nIn addition to compile time checks, editor completion and minimizing coding confusion, the generated\ncode results in accelerated performance - no reflections are needed in run time, and database data\ndecoding is faster when you know the target type.\n\nThis tools gives all basic database operations, and  handling of complex relation between types.\n\n## Example:\n\nCheck out the [examples](./examples).\n\nRunning the orm command on the `Person` struct in the `tests` package, \nwill create a `person_orm.go` file, with ORM functions for the given struct.\nThose functions can interact with a database object created from `orm.Open` function.\n\nNotice that all operations are typed, `Age` is an `int`, `Name` is a `string`, the `tests.Person`\nis used in the arguments and in the return values.\n\n```go\nimport (\n\t\"log\"\n\t\n\t_ \"github.com/go-sql-driver/mysql\"\n\t\"github.com/posener/orm\"\n)\n\nfunc main() {\n\t// Open an SQL connection to a mysql database\n\t// We can set a connection logger with an optional OptLogger function which will log\n\t// all executed SQL statements with our favorite logger.\n\tdb, err := orm.Open(\"mysql\", \"user:password@(127.0.0.1:3306)/db\", orm.OptLogger(log.Printf))\n\tdefer db.Close()\n\t\n\t// NewPersonORM was generated with the `orm` command line.\n\t// It returns an object that interacts with the database.\n\t// This was generated for a struct:\n\t// type Person struct {\n\t//      Name       string\n\t//      Age        int\n\t// }\n\tperson, err := NewPersonORM(db)\n\t\n\t// Create a table:\n\t// The AutoMigrate modifier will cause the create table function to try and\n\t// migrate an existing table. Current implementation supports adding columns\n\t// and foreign keys.\n\terr = person.Create().AutoMigrate().Exec()\n\t\n\t// Insert a row with arguments:\n\t// The Insert() function returns a builder, which has functions according\n\t// to the struct's fields. The nice thing here: SetName's argument is of type\n\t// string, and SetAge's argument is of type int.\n\t// No room for run time errors!\n\t// The returned values are the created object, of type *Person, and an error,\n\t// in case that there was an error in the INSERT operation. Everything is typed!\n\tjohn, err = person.Insert().SetName(\"John\").SetAge(1).Exec()\n\tprintln(john.Name) // Output: John\n\n\t// Insert a row with a struct:\n\t// InsertPerson's argument is of type *Person, no room for run tme errors!\n\t// Again, Exec() returns a *Person and error objects.\n\tdoug, err = person.Insert().InsertPerson(\u0026tests.Person{Name: \"Doug\", Age: 3}).Exec()\n\tprintln(doug.Name, doug.Age) // Output: Doug 3\n\n\t// Select rows from the table:\n\t// The Where() modifier adds a WHERE statement to the SQL query, it's input is a \n\t// generated function with generated modifiers. Name() accepts a constant that \n\t// defines the operation, and the second argument is of type string, so only valid\n\t// comparisons can be made, and they are checked in compile time.\n\t// Query() returns the slice []Person, and an error object. Everything is typed!\n\tpersons, err := person.Select().\n\t\tWhere(person.Where().Name(orm.OpEq, \"John\")).\n\t\tQuery() // returns []tests.Person, typed return value.\n\tprintln(persons[0].Age) // Output: 1\n\t\n\t// Get first matching row or \"not found\" error\n\t// Again, the first argument is of type *Person!\n\tp1, err := person.Select().First()\n\t\n\t// Delete a row:\n\t// Here, we are using the Age() function of the Where() modifier, and it's second\n\t// argument is of type int, so only valid comparisons can be made.\n\t_, err = person.Delete().Where(person.Where().Age(orm.OpGt, 12)).Exec()\n\t\n\t// I think you get the vibe by now, needless to say it again. Everything is ...\n}\n```\n\n## Command Line Tool\n\n### Install:\n\n```bash\ngo get -u github.com/posener/orm/cmd/orm\n```\n\n### Usage\n\nRun `orm -h` to get detailed usage information.\n\nA simple use-case is to run `orm -type MyStruct\n\n#### go generate\n\nBy adding the comment aside to the type deceleration, as shown below, one could run `go generate ./...`\nto generate the ORM files for `MyType`.\n\n```go\n//go:generate orm -type MyType\n\ntype MyType struct {\n\t...\n}\n```\n\n### Currently Supported Dialects:\n\n- [x] mysql\n- [x] postgres\n- [x] sqlite\n\n### Compare with other Go ORMs \n\nHere are the strengths of `orm` in comparison to other ORM libraries for Go.\n\n\u003e [GORM](http://jinzhu.me/gorm/): is a mature, widely used, heavily debugged, favorable, \n  and very recommended to use. `orm` advantages over GORM are: fully typed APIs, clear and explicit APIs, speed,\n  support for `Context`, and support for custom logger.\n\nAlso, [benchmarking results](./bench) are available.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fposener%2Form","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fposener%2Form","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fposener%2Form/lists"}