mirror of
1
Fork 0
gotosocial/vendor/github.com/go-pg/pg/v10
Tobi Smethurst 98263a7de6
Grand test fixup (#138)
* start fixing up tests

* fix up tests + automate with drone

* fiddle with linting

* messing about with drone.yml

* some more fiddling

* hmmm

* add cache

* add vendor directory

* verbose

* ci updates

* update some little things

* update sig
2021-08-12 21:03:24 +02:00
..
internal Grand test fixup (#138) 2021-08-12 21:03:24 +02:00
orm Grand test fixup (#138) 2021-08-12 21:03:24 +02:00
pgjson Grand test fixup (#138) 2021-08-12 21:03:24 +02:00
types Grand test fixup (#138) 2021-08-12 21:03:24 +02:00
.golangci.yml Grand test fixup (#138) 2021-08-12 21:03:24 +02:00
.prettierrc Grand test fixup (#138) 2021-08-12 21:03:24 +02:00
.travis.yml Grand test fixup (#138) 2021-08-12 21:03:24 +02:00
CHANGELOG.md Grand test fixup (#138) 2021-08-12 21:03:24 +02:00
LICENSE Grand test fixup (#138) 2021-08-12 21:03:24 +02:00
Makefile Grand test fixup (#138) 2021-08-12 21:03:24 +02:00
README.md Grand test fixup (#138) 2021-08-12 21:03:24 +02:00
base.go Grand test fixup (#138) 2021-08-12 21:03:24 +02:00
db.go Grand test fixup (#138) 2021-08-12 21:03:24 +02:00
doc.go Grand test fixup (#138) 2021-08-12 21:03:24 +02:00
error.go Grand test fixup (#138) 2021-08-12 21:03:24 +02:00
go.mod Grand test fixup (#138) 2021-08-12 21:03:24 +02:00
go.sum Grand test fixup (#138) 2021-08-12 21:03:24 +02:00
hook.go Grand test fixup (#138) 2021-08-12 21:03:24 +02:00
listener.go Grand test fixup (#138) 2021-08-12 21:03:24 +02:00
messages.go Grand test fixup (#138) 2021-08-12 21:03:24 +02:00
options.go Grand test fixup (#138) 2021-08-12 21:03:24 +02:00
pg.go Grand test fixup (#138) 2021-08-12 21:03:24 +02:00
result.go Grand test fixup (#138) 2021-08-12 21:03:24 +02:00
stmt.go Grand test fixup (#138) 2021-08-12 21:03:24 +02:00
tx.go Grand test fixup (#138) 2021-08-12 21:03:24 +02:00

README.md

PostgreSQL client and ORM for Golang

Build Status PkgGoDev Documentation Chat

Important. Please check Bun - the next iteration of go-pg built on top of sql.DB.

Ecosystem

Features

Installation

go-pg supports 2 last Go versions and requires a Go version with modules support. So make sure to initialize a Go module:

go mod init github.com/my/repo

And then install go-pg (note v10 in the import; omitting it is a popular mistake):

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

Quickstart

package pg_test

import (
    "fmt"

    "github.com/go-pg/pg/v10"
    "github.com/go-pg/pg/v10/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 `pg:"rel:has-one"`
}

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.Model(user1).Insert()
    if err != nil {
        panic(err)
    }

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

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

    // Select user by primary key.
    user := &User{Id: user1.Id}
    err = db.Model(user).WherePK().Select()
    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]>>
}

// createSchema creates database schema for User and Story models.
func createSchema(db *pg.DB) error {
    models := []interface{}{
        (*User)(nil),
        (*Story)(nil),
    }

    for _, model := range models {
        err := db.Model(model).CreateTable(&orm.CreateTableOptions{
            Temp: true,
        })
        if err != nil {
            return err
        }
    }
    return nil
}

See also