Golang ORM with focus on PostgreSQL features and performance
vmihailenco Merge pull request #1002 from go-pg/fix/composite-all-fields
orm: use all fields to scan composites
Latest commit 84b1608 Oct 17, 2018
Permalink
Failed to load latest commit information.
internal internal/parser: fix substring Oct 17, 2018
orm orm: use all fields to scan composites Oct 17, 2018
types internal/parser: fix substring Oct 17, 2018
.travis.yml Use canonical sasl lib import path Sep 26, 2018
CHANGELOG.md Update changelog Sep 13, 2018
LICENSE Use full name in license. Sep 12, 2016
Makefile Add Options.MinIdleConns Aug 12, 2018
README.md readme: mention soft deletes Sep 4, 2018
bench_test.go orm: add ForEach May 9, 2018
conv_test.go Remove unused code Aug 13, 2018
db.go Retry too many connections error Sep 7, 2018
db_test.go Recognize array and hstore options in SQL tag Sep 5, 2018
doc.go Move types.Result to orm.Result. Mar 11, 2017
error.go More docs Apr 29, 2018
example_array_test.go Recognize array and hstore options in SQL tag Sep 5, 2018
example_composite_test.go Add support for soft deletes Aug 23, 2018
example_hstore_test.go Recognize array and hstore options in SQL tag Sep 5, 2018
example_many2many_self_test.go Better many to many example May 2, 2018
example_many2many_test.go More docs May 28, 2018
example_model_test.go orm: support models with dynamic table name Oct 5, 2018
example_placeholders_test.go Check for var shadowing Mar 28, 2018
example_soft_delete_test.go orm: update only deleted_at column Sep 5, 2018
example_test.go Rename example Oct 1, 2018
exampledb_model_test.go readme: update example Mar 26, 2018
exampledb_query_test.go Move types.Result to orm.Result. Mar 11, 2017
export_test.go Add OnConnect hook Aug 25, 2017
hook.go Add QueryProcessedEvent.Attempt Oct 17, 2017
hook_test.go Add BeforeSelectQuery hook Aug 16, 2018
listener.go Fix nil error in log message Sep 22, 2018
listener_test.go Add Options.MinIdleConns Aug 12, 2018
loader_test.go Add BeforeSelectQuery hook Aug 16, 2018
main_test.go Remove unused code Aug 13, 2018
messages.go Use canonical sasl lib import path Sep 26, 2018
options.go Add ApplicationName/application_name option Aug 30, 2018
options_test.go Add ApplicationName/application_name option Aug 30, 2018
pg.go Recognize array and hstore options in SQL tag Sep 5, 2018
pool_test.go Cleanup pool Jun 5, 2018
race_test.go Cleanup pool Jun 5, 2018
result.go Improve ErrNoRows tests Jun 20, 2017
stmt.go Add Conn.WithReader and Conn.WithWriter Aug 15, 2018
time.go orm: add Query.Value Feb 12, 2018
tx.go Add support for soft deletes Aug 23, 2018
tx_test.go Move types.Result to orm.Result. Mar 11, 2017

README.md

PostgreSQL client and ORM for Golang

Build Status GoDoc

Features:

Get Started

go get -u github.com/go-pg/pg

Look & Feel

package pg_test

import (
    "fmt"

    "github.com/go-pg/pg"
    "github.com/go-pg/pg/orm"
)

type User struct {
    Id     int64
    Name   string
    Emails []string
}

func (u User) String() string {
    return fmt.Sprintf("User<%d %s %v>", u.Id, u.Name, u.Emails)
}

type Story struct {
    Id       int64
    Title    string
    AuthorId int64
    Author   *User
}

func (s Story) String() string {
    return fmt.Sprintf("Story<%d %s %s>", s.Id, s.Title, s.Author)
}

func ExampleDB_Model() {
    db := pg.Connect(&pg.Options{
        User: "postgres",
    })
    defer db.Close()

    err := createSchema(db)
    if err != nil {
        panic(err)
    }

    user1 := &User{
        Name:   "admin",
        Emails: []string{"admin1@admin", "admin2@admin"},
    }
    err = db.Insert(user1)
    if err != nil {
        panic(err)
    }

    err = db.Insert(&User{
        Name:   "root",
        Emails: []string{"root1@root", "root2@root"},
    })
    if err != nil {
        panic(err)
    }

    story1 := &Story{
        Title:    "Cool story",
        AuthorId: user1.Id,
    }
    err = db.Insert(story1)
    if err != nil {
        panic(err)
    }

    // Select user by primary key.
    user := &User{Id: user1.Id}
    err = db.Select(user)
    if err != nil {
        panic(err)
    }

    // Select all users.
    var users []User
    err = db.Model(&users).Select()
    if err != nil {
        panic(err)
    }

    // Select story and associated author in one query.
    story := new(Story)
    err = db.Model(story).
        Relation("Author").
        Where("story.id = ?", story1.Id).
        Select()
    if err != nil {
        panic(err)
    }

    fmt.Println(user)
    fmt.Println(users)
    fmt.Println(story)
    // Output: User<1 admin [admin1@admin admin2@admin]>
    // [User<1 admin [admin1@admin admin2@admin]> User<2 root [root1@root root2@root]>]
    // Story<1 Cool story User<1 admin [admin1@admin admin2@admin]>>
}

func createSchema(db *pg.DB) error {
    for _, model := range []interface{}{(*User)(nil), (*Story)(nil)} {
        err := db.CreateTable(model, &orm.CreateTableOptions{
            Temp: true,
        })
        if err != nil {
            return err
        }
    }
    return nil
}

See also