All Projects → posener → orm

posener / orm

Licence: MIT license
Go Typed ORM

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to orm

fn
Functional library for PHP with proper currying
Stars: ✭ 22 (+37.5%)
Mutual labels:  code-generation
UI2CODE
A tidied repo for UI2CODE, a reverse engineering system convert UI design to code automatically and precisely.
Stars: ✭ 24 (+50%)
Mutual labels:  code-generation
swaggie
Tool for generating TypeScript services for given Swagger API endpoints
Stars: ✭ 18 (+12.5%)
Mutual labels:  code-generation
mybatis-generator-gui-plus
基于MyBatis-Generator+SQLite+beautyeye_lnf开发的一款图形化代码生成器
Stars: ✭ 16 (+0%)
Mutual labels:  code-generation
wb-toolbox
Simulink toolbox to rapidly prototype robot controllers
Stars: ✭ 20 (+25%)
Mutual labels:  code-generation
acgt
Auto Code Generation Tools (DSL)
Stars: ✭ 18 (+12.5%)
Mutual labels:  code-generation
celerio
Celerio is a code generator tool for data-driven application.
Stars: ✭ 73 (+356.25%)
Mutual labels:  code-generation
cog
Command-line utility that makes it easy to organize a project which uses code generation
Stars: ✭ 15 (-6.25%)
Mutual labels:  code-generation
oag
Idiomatic Go (Golang) client package generation from OpenAPI documents
Stars: ✭ 51 (+218.75%)
Mutual labels:  code-generation
solregex
Regex compilation to Solidity
Stars: ✭ 37 (+131.25%)
Mutual labels:  code-generation
jsish
Jsi is a small, C-embeddable javascript interpreter with tightly woven Web and DB support.
Stars: ✭ 32 (+100%)
Mutual labels:  code-generation
pyodesys
∫ Straightforward numerical integration of systems of ordinary differential equations
Stars: ✭ 85 (+431.25%)
Mutual labels:  code-generation
aptk
A toolkit project to enable you to build annotation processors more easily
Stars: ✭ 28 (+75%)
Mutual labels:  code-generation
WebApiToTypeScript
A tool for code generating TypeScript endpoints for your ASP.NET Web API controllers
Stars: ✭ 26 (+62.5%)
Mutual labels:  code-generation
django-code-generator
Generate code from your Django models for faster development
Stars: ✭ 35 (+118.75%)
Mutual labels:  code-generation
copydynamic
Prototype of generating `copyDynamic` extension functions for kotlin data classes
Stars: ✭ 57 (+256.25%)
Mutual labels:  code-generation
Friendly Code Editor
Try this Friendly Code Editor. You'll love it. I made it with a lot of effort. It has some great features. I will update it adequately later. Very helpful for developers. Enjoy and share.
Stars: ✭ 20 (+25%)
Mutual labels:  code-generation
1c http
Подсистема 1С для работы с HTTP
Stars: ✭ 48 (+200%)
Mutual labels:  code-generation
openapi-client
Generate ES6 or Typescript service integration code from an OpenAPI 2 spec
Stars: ✭ 92 (+475%)
Mutual labels:  code-generation
gdbus-codegen-glibmm
Code generator for C++ D-Bus stubs and proxies using Giomm/Glibmm
Stars: ✭ 21 (+31.25%)
Mutual labels:  code-generation

Go ORM

Build Status GoDoc Go Report Card

An attempt to write a typed ORM for Go.

Check out the Wiki for documentation.

This repository gives a command line tool, called orm, for generating ORM code for a given struct. The generated code is typed and has no interface{}s arguments and returns values as in other ORM Go libraries.

In addition to compile time checks, editor completion and minimizing coding confusion, the generated code results in accelerated performance - no reflections are needed in run time, and database data decoding is faster when you know the target type.

This tools gives all basic database operations, and handling of complex relation between types.

Example:

Check out the examples.

Running the orm command on the Person struct in the tests package, will create a person_orm.go file, with ORM functions for the given struct. Those functions can interact with a database object created from orm.Open function.

Notice that all operations are typed, Age is an int, Name is a string, the tests.Person is used in the arguments and in the return values.

import (
	"log"
	
	_ "github.com/go-sql-driver/mysql"
	"github.com/posener/orm"
)

func main() {
	// Open an SQL connection to a mysql database
	// We can set a connection logger with an optional OptLogger function which will log
	// all executed SQL statements with our favorite logger.
	db, err := orm.Open("mysql", "user:password@(127.0.0.1:3306)/db", orm.OptLogger(log.Printf))
	defer db.Close()
	
	// NewPersonORM was generated with the `orm` command line.
	// It returns an object that interacts with the database.
	// This was generated for a struct:
	// type Person struct {
	//      Name       string
	//      Age        int
	// }
	person, err := NewPersonORM(db)
	
	// Create a table:
	// The AutoMigrate modifier will cause the create table function to try and
	// migrate an existing table. Current implementation supports adding columns
	// and foreign keys.
	err = person.Create().AutoMigrate().Exec()
	
	// Insert a row with arguments:
	// The Insert() function returns a builder, which has functions according
	// to the struct's fields. The nice thing here: SetName's argument is of type
	// string, and SetAge's argument is of type int.
	// No room for run time errors!
	// The returned values are the created object, of type *Person, and an error,
	// in case that there was an error in the INSERT operation. Everything is typed!
	john, err = person.Insert().SetName("John").SetAge(1).Exec()
	println(john.Name) // Output: John

	// Insert a row with a struct:
	// InsertPerson's argument is of type *Person, no room for run tme errors!
	// Again, Exec() returns a *Person and error objects.
	doug, err = person.Insert().InsertPerson(&tests.Person{Name: "Doug", Age: 3}).Exec()
	println(doug.Name, doug.Age) // Output: Doug 3

	// Select rows from the table:
	// The Where() modifier adds a WHERE statement to the SQL query, it's input is a 
	// generated function with generated modifiers. Name() accepts a constant that 
	// defines the operation, and the second argument is of type string, so only valid
	// comparisons can be made, and they are checked in compile time.
	// Query() returns the slice []Person, and an error object. Everything is typed!
	persons, err := person.Select().
		Where(person.Where().Name(orm.OpEq, "John")).
		Query() // returns []tests.Person, typed return value.
	println(persons[0].Age) // Output: 1
	
	// Get first matching row or "not found" error
	// Again, the first argument is of type *Person!
	p1, err := person.Select().First()
	
	// Delete a row:
	// Here, we are using the Age() function of the Where() modifier, and it's second
	// argument is of type int, so only valid comparisons can be made.
	_, err = person.Delete().Where(person.Where().Age(orm.OpGt, 12)).Exec()
	
	// I think you get the vibe by now, needless to say it again. Everything is ...
}

Command Line Tool

Install:

go get -u github.com/posener/orm/cmd/orm

Usage

Run orm -h to get detailed usage information.

A simple use-case is to run `orm -type MyStruct

go generate

By adding the comment aside to the type deceleration, as shown below, one could run go generate ./... to generate the ORM files for MyType.

//go:generate orm -type MyType

type MyType struct {
	...
}

Currently Supported Dialects:

  • mysql
  • postgres
  • sqlite

Compare with other Go ORMs

Here are the strengths of orm in comparison to other ORM libraries for Go.

GORM: is a mature, widely used, heavily debugged, favorable, and very recommended to use. orm advantages over GORM are: fully typed APIs, clear and explicit APIs, speed, support for Context, and support for custom logger.

Also, benchmarking results are available.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].