postgres

package module
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: May 25, 2024 License: MIT Imports: 8 Imported by: 12

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/v5/stdlib

Supported Database

  • PostgreSQL 11, 12, 13, 14, 15 and 16

Testing

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

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 Name string = "postgres"

Name of database type this adapter implements.

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.

func (Postgres) Name added in v0.11.0

func (Postgres) Name() string

Name of database adapter.

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