postgres

package module
Version: v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 3, 2022 License: MIT Imports: 8 Imported by: 2

README

postgres

GoDoc Tesst Go Report Card codecov Gitter chat

Postgres adapter for REL.

Example

package main

import (
	"context"

	_ "github.com/lib/pq"
	"github.com/go-rel/postgres"
	"github.com/go-rel/rel"
)

func main() {
	// open postgres connection.
	adapter, err := postgres.Open("postgres://postgres@localhost/rel_test?sslmode=disable")
	if err != nil {
		panic(err)
	}
	defer adapter.Close()

	// initialize REL's repo.
	repo := rel.New(adapter)
	repo.Ping(context.TODO())
}

Example Replication (Master/Standby)

package main

import (
	"context"

	"github.com/go-rel/primaryreplica"
	_ "github.com/lib/pq"
	"github.com/go-rel/postgres"
	"github.com/go-rel/rel"
)

func main() {
	// open postgres connections.
	adapter := primaryreplica.New(
		postgres.MustOpen("postgres://postgres@master/rel_test?sslmode=disable"),
		postgres.MustOpen("postgres://postgres@standby/rel_test?sslmode=disable"),
	)
	defer adapter.Close()

	// initialize REL's repo.
	repo := rel.New(adapter)
	repo.Ping(context.TODO())
}

Supported Driver

  • github.com/lib/pq
  • github.com/jackc/pgx/v4/stdlib

Supported Database

  • PostgreSQL 9.6, 10, 11, 12, 13 and 14

Testing

Start PostgreSQL server in Docker
docker run -it --rm -p 5433:5432 -e "POSTGRES_USER=rel" -e "POSTGRES_PASSWORD=test" -e "POSTGRES_DB=rel_test" postgres:14-alpine
Run tests
POSTGRESQL_DATABASE="postgres://rel:test@localhost:5433/rel_test" go test ./...

Documentation

Overview

Package postgres wraps postgres (pq) driver as an adapter for REL.

Usage:

// open postgres connection.
adapter, err := postgres.Open("postgres://postgres@localhost/rel_test?sslmode=disable")
if err != nil {
	panic(err)
}
defer adapter.Close()

// initialize REL's repo.
repo := rel.New(adapter)

Index

Constants

View Source
const TimeLayout = "2006-01-02 15:04:05.999999999Z07:00:00"

TimeLayout used by PostgreSQL adapter.

Variables

This section is empty.

Functions

func FormatTime added in v0.5.0

func FormatTime(t time.Time) string

FormatTime formats time to PostgreSQL format.

func MustOpen added in v0.4.0

func MustOpen(dsn string) rel.Adapter

MustOpen postgres connection using dsn.

func New

func New(database *db.DB) rel.Adapter

New postgres adapter using existing connection.

func Open

func Open(dsn string) (rel.Adapter, error)

Open postgres connection using dsn.

Types

type Postgres

type Postgres struct {
	sql.SQL
}

Postgres adapter.

func (Postgres) Begin

func (p Postgres) Begin(ctx context.Context) (rel.Adapter, error)

Begin begins a new transaction.

func (Postgres) Insert

func (p Postgres) Insert(ctx context.Context, query rel.Query, primaryField string, mutates map[string]rel.Mutate, onConflict rel.OnConflict) (interface{}, error)

Insert inserts a record to database and returns its id.

func (Postgres) InsertAll

func (p Postgres) InsertAll(ctx context.Context, query rel.Query, primaryField string, fields []string, bulkMutates []map[string]rel.Mutate, onConflict rel.OnConflict) ([]interface{}, error)

InsertAll inserts multiple records to database and returns its ids.

type Quote added in v0.2.0

type Quote struct{}

Quote PostgreSQL identifiers and literals.

func (Quote) ID added in v0.2.0

func (q Quote) ID(name string) string

func (Quote) Value added in v0.2.0

func (q Quote) Value(v interface{}) string

type ValueConvert added in v0.2.0

type ValueConvert struct{}

ValueConvert converts values to PostgreSQL literals.

func (ValueConvert) ConvertValue added in v0.2.0

func (c ValueConvert) ConvertValue(v interface{}) (driver.Value, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL