All Projects → go-reform → Reform

go-reform / Reform

Licence: mit
A better ORM for Go, based on non-empty interfaces and code generation.

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Labels

Projects that are alternatives of or similar to Reform

Qb
The database toolkit for go
Stars: ✭ 524 (-52.49%)
Mutual labels:  sql, orm
Godb
A Go SQL query builder and struct mapper.
Stars: ✭ 651 (-40.98%)
Mutual labels:  sql, orm
Go Sqlbuilder
A flexible and powerful SQL string builder library plus a zero-config ORM.
Stars: ✭ 539 (-51.13%)
Mutual labels:  sql, orm
Openrecord
Make ORMs great again!
Stars: ✭ 474 (-57.03%)
Mutual labels:  sql, orm
Records
SQL for Humans™
Stars: ✭ 6,761 (+512.96%)
Mutual labels:  sql, orm
Pg
Golang ORM with focus on PostgreSQL features and performance
Stars: ✭ 4,918 (+345.87%)
Mutual labels:  sql, orm
Lucid
AdonisJS official SQL ORM. Supports PostgreSQL, MySQL, MSSQL, Redshift, SQLite and many more
Stars: ✭ 613 (-44.42%)
Mutual labels:  sql, orm
Beam
A type-safe, non-TH Haskell SQL library and ORM
Stars: ✭ 454 (-58.84%)
Mutual labels:  sql, orm
Smartsql
SmartSql = MyBatis in C# + .NET Core+ Cache(Memory | Redis) + R/W Splitting + PropertyChangedTrack +Dynamic Repository + InvokeSync + Diagnostics
Stars: ✭ 775 (-29.74%)
Mutual labels:  sql, orm
Nano Sql
Universal database layer for the client, server & mobile devices. It's like Lego for databases.
Stars: ✭ 717 (-35%)
Mutual labels:  sql, orm
Fluent
Vapor ORM (queries, models, and relations) for NoSQL and SQL databases
Stars: ✭ 1,071 (-2.9%)
Mutual labels:  sql, orm
Express Knex Objection
A simple API system on a pg database, using knex and objection to simplify connection and management
Stars: ✭ 20 (-98.19%)
Mutual labels:  sql, orm
Jooq
jOOQ is the best way to write SQL in Java
Stars: ✭ 4,695 (+325.66%)
Mutual labels:  sql, orm
Sagacity Sqltoy
基于java语言比mybatis更实用的orm框架,支持mysql、oracle、postgresql、sqlserver、db2、dm、mongodb、elasticsearch、tidb、guassdb、kingbase、oceanbase、greenplum
Stars: ✭ 496 (-55.03%)
Mutual labels:  sql, orm
Toucan
A classy high-level Clojure library for defining application models and retrieving them from a DB
Stars: ✭ 456 (-58.66%)
Mutual labels:  sql, orm
Exposed
Kotlin SQL Framework
Stars: ✭ 5,753 (+421.58%)
Mutual labels:  sql, orm
Gnorm
A database-first code generator for any language
Stars: ✭ 415 (-62.38%)
Mutual labels:  sql, orm
Android Orma
An ORM for Android with type-safety and painless smart migrations
Stars: ✭ 442 (-59.93%)
Mutual labels:  sql, orm
Sequelize
An easy-to-use and promise-based multi SQL dialects ORM tool for Node.js
Stars: ✭ 25,422 (+2204.81%)
Mutual labels:  sql, orm
Ktorm
A lightweight ORM framework for Kotlin with strong-typed SQL DSL and sequence APIs.
Stars: ✭ 843 (-23.57%)
Mutual labels:  sql, orm

reform

Release PkgGoDev CI AppVeyor Build status Coverage Report Go Report Card

Reform gopher logo

A better ORM for Go and database/sql.

It uses non-empty interfaces, code generation (go generate), and initialization-time reflection as opposed to interface{}, type system sidestepping, and runtime reflection. It will be kept simple.

Supported SQL dialects:

RDBMS Library and drivers Status
PostgreSQL github.com/lib/pq (postgres) Stable. Tested with all supported versions.
github.com/jackc/pgx/stdlib (pgx v3) Stable. Tested with all supported versions.
MySQL github.com/go-sql-driver/mysql (mysql) Stable. Tested with all supported versions.
SQLite3 github.com/mattn/go-sqlite3 (sqlite3) Stable.
Microsoft SQL Server github.com/denisenkom/go-mssqldb (sqlserver, mssql) Stable.
Tested on Windows with: SQL2008R2SP2, SQL2012SP1, SQL2014, SQL2016.
On Linux with: mcr.microsoft.com/mssql/server:2017-latest and mcr.microsoft.com/mssql/server:2019-latest Docker images.

Notes:

Quickstart

  1. Make sure you are using Go 1.13+, and Go modules support is enabled. Install or update reform package, reform and reform-db commands with:

    go get -v gopkg.in/reform.v1/...
    

    If you are not using Go modules yet, you can use dep to vendor desired version of reform, and then install commands with:

    go install -v ./vendor/gopkg.in/reform.v1/...
    

    You can also install the latest stable version of reform without using Go modules thanks to gopkg.in redirection, but please note that this will not use the stable versions of the database drivers:

    env GO111MODULE=off go get -u -v gopkg.in/reform.v1/...
    

    Canonical import path is gopkg.in/reform.v1; using github.com/go-reform/reform will not work.

    See note about versioning and branches below.

  2. Use reform-db command to generate models for your existing database schema. For example:

    reform-db -db-driver=sqlite3 -db-source=example.sqlite3 init
    
  3. Update generated models or write your own – struct representing a table or view row. For example, store this in file person.go:

    //go:generate reform
    
    //reform:people
    type Person struct {
    	ID        int32      `reform:"id,pk"`
    	Name      string     `reform:"name"`
    	Email     *string    `reform:"email"`
    	CreatedAt time.Time  `reform:"created_at"`
    	UpdatedAt *time.Time `reform:"updated_at"`
    }
    

    Magic comment //reform:people links this model to people table or view in SQL database. The first value in field's reform tag is a column name. pk marks primary key. Use value - or omit tag completely to skip a field. Use pointers (recommended) or sql.NullXXX types for nullable fields.

  4. Run reform [package or directory] or go generate [package or file]. This will create person_reform.go in the same package with type PersonTable and methods on Person.

  5. See documentation how to use it. Simple example:

    // Get *sql.DB as usual. PostgreSQL example:
    sqlDB, err := sql.Open("postgres", "postgres://127.0.0.1:5432/database")
    if err != nil {
    	log.Fatal(err)
    }
    defer sqlDB.Close()
    
    // Use new *log.Logger for logging.
    logger := log.New(os.Stderr, "SQL: ", log.Flags())
    
    // Create *reform.DB instance with simple logger.
    // Any Printf-like function (fmt.Printf, log.Printf, testing.T.Logf, etc) can be used with NewPrintfLogger.
    // Change dialect for other databases.
    db := reform.NewDB(sqlDB, postgresql.Dialect, reform.NewPrintfLogger(logger.Printf))
    
    // Save record (performs INSERT or UPDATE).
    person := &Person{
    	Name:  "Alexey Palazhchenko",
    	Email: pointer.ToString("[email protected]"),
    }
    if err := db.Save(person); err != nil {
    	log.Fatal(err)
    }
    
    // ID is filled by Save.
    person2, err := db.FindByPrimaryKeyFrom(PersonTable, person.ID)
    if err != nil {
    	log.Fatal(err)
    }
    fmt.Println(person2.(*Person).Name)
    
    // Delete record.
    if err = db.Delete(person); err != nil {
    	log.Fatal(err)
    }
    
    // Find records by IDs.
    persons, err := db.FindAllFrom(PersonTable, "id", 1, 2)
    if err != nil {
    	log.Fatal(err)
    }
    for _, p := range persons {
    	fmt.Println(p)
    }
    

Background

reform was born during summer 2014 out of frustrations with existing Go ORMs. All of them have a method Save(record interface{}) which can be used like this:

orm.Save(User{Name: "gopher"})
orm.Save(&User{Name: "gopher"})
orm.Save(nil)
orm.Save("Batman!!")

Now you can say that last invocation is obviously invalid, and that it's not hard to make an ORM to accept both first and second versions. But there are two problems:

  1. Compiler can't check it. Method's signature in godoc will not tell us how to use it. We are essentially working against those tools by sidestepping type system.
  2. First version is still invalid, since one would expect Save() method to set record's primary key after INSERT, but this change will be lost due to passing by value.

First proprietary version of reform was used in production even before go generate announcement. This free and open-source version is the fourth milestone on the road to better and idiomatic API.

Versioning and branching policy

We are following Semantic Versioning, using gopkg.in and filling a changelog. All v1 releases are SemVer-compatible; breaking changes will not be applied.

We use tags v1.M.m for releases, branch main (default on GitHub) for the next minor release development, and release/1.M branches for patch release development. (It was more complicated before 1.4.0 release.)

Major version 2 is currently not planned.

Additional packages

Caveats and limitations

  • There should be zero pk fields for Struct and exactly one pk field for Record. Composite primary keys are not supported (#114).
  • pk field can't be a pointer (== nil doesn't work).
  • Database row can't have a Go's zero value (0, empty string, etc.) in primary key column.

License

Code is covered by standard MIT-style license. Copyright (c) 2016-2020 Alexey Palazhchenko. See LICENSE for details. Note that generated code is covered by the terms of your choice.

The reform gopher was drawn by Natalya Glebova. Please use it only as reform logo. It is based on the original design by Renée French, released under Creative Commons Attribution 3.0 USA license.

Contributing

See Contributing Guidelines.

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].