gob

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2020 License: GPL-3.0 Imports: 16 Imported by: 0

README

gob

Bulk upserts to PostgreSQL, MySQL, Cassandra

Go Report Card GoDoc Conventional Commit Gitmoji Go Supported Go Versions GitHub Release License



Installation

go get -u github.com/csmadhu/gob

Usage

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/csmadhu/gob"
)

func main() {
	g, err := gob.New(gob.WithDBProvider(gob.DBProviderPg),
		gob.WithDBConnStr("postgres://postgres:postgres@localhost:5432/gob?pool_max_conns=1"))
	if err != nil {
		log.Fatalf("init gob; err: %v", err)
	}
	defer g.Close()

	// upsert records to table student
	var rows []gob.Row
	for i := 0; i < 10; i++ {
		row := gob.NewRow()
		row.Add("name", fmt.Sprintf("foo-%d", i))
		row.Add("age", 20)

		rows = append(rows, row)
	}

	if err := g.Upsert(context.Background(), gob.UpsertArgs{
		Model:          "students",
		Keys:           []string{"name"},
		ConflictAction: gob.ConflictActionUpdate,
		Rows:           rows}); err != nil {
		log.Fatalf("upsert students; err: %v", err)
	}
}

Options

All options are optional. Options not applicable to Database provider is ignored.

option description type default DB providers
WithBatchSize Transaction Batch Size int 10000
  • PostgreSQL
  • MySQL
WithDBProvider Database provider gob.DBProvider DBProviderPg
  • PostgreSQL
  • MySQL
  • Cassandra
WithDBConnStr DB URL/DSN
  • PostgreSQL postgres://username:password@host:port/batabase
    References
    • https://pkg.golang.ir/github.com/jackc/pgconn?tab=doc#ParseConfig
    • https://pkg.golang.ir/github.com/jackc/pgx/v4?tab=doc#ParseConfig
  • MySQL username:password@(host:port)/database
  • Cassandra cassandra://username:password@host1--host2--host3:port/keyspace?consistency=quorum&compressor=snappy&tokenAware=true
    References
    • https://godoc.org/github.com/gocql/gocql#Consistency
    • https://godoc.org/github.com/gocql/gocql#Compressor
    • https://godoc.org/github.com/gocql/gocql#PoolConfig
    • https://godoc.org/github.com/gocql/gocql#HostSelectionPolicy
    • https://godoc.org/github.com/gocql/gocql#TokenAwareHostPolicy
string postgres://postgres:postgres@localhost:5432/gob?pool_max_conns=1
  • PostgreSQL
  • MySQL
  • Cassandra
WithConnIdleTime Maximum amount of time conn may be idle time.Duration 3 second
  • PostgreSQL
  • MySQL
WithConnLifeTime Maximum amount of time conn may be reused time.Duration 3 second
  • PostgreSQL
  • MySQL
  • Cassandra
WithIdleConns Maximum number of connections idle in pool int 2
  • PostgreSQL
  • MySQL
WithOpenConns Maximum number of connections open to database int 10
  • PostgreSQL
  • MySQL
  • Cassandra

Examples

Examples for supported Database provider can be found here

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrConnClosed when Gob.Upsert is called after closing the connection
	ErrConnClosed = errors.New("gob: conn closed;")

	// ErrEmptyModel when Gob.Upsert is called with empty model
	ErrEmptyModel = errors.New("gob: empty model;")

	// ErrEmptykeys when Gob.Upsert is called with empty keys
	ErrEmptykeys = errors.New("gob: empty keys;")

	// ErrEmptyKeyspace when keyspace is not provided in DB URL
	ErrEmptyKeyspace = errors.New("gob: empty keyspace;")

	// ErrEmptyConnStr when connection string is empty
	ErrEmptyConnStr = errors.New("gob: empty connection string;")

	// ErrEmptyDBProvider when db provider is empty
	ErrEmptyDBProvider = errors.New("gob: empty db provider;")

	// ErrEmptyConflictAction when conflict action not specified
	ErrEmptyConflictAction = errors.New("gob: empty conflict action;")
)

Functions

This section is empty.

Types

type ConflictAction

type ConflictAction string

ConflictAction specifies alternatives ON CONFLICT

const (
	// ConflictActionNothing ignores conflict during INSERT
	ConflictActionNothing ConflictAction = "nothing"
	// ConflictActionUpdate resolves conflict by updating the row
	ConflictActionUpdate ConflictAction = "update"
)

type DBProvider

type DBProvider string

DBProvider for storage

const (
	// DBProviderPg indicates relational database prvoided by PostgreSQL
	DBProviderPg DBProvider = "pg"
	// DBProviderMySQL indicates relational database provided by MySQL
	DBProviderMySQL DBProvider = "mysql"
	// DBProviderCassandra indicates no-sql database provided by Cassandra
	DBProviderCassandra DBProvider = "cassandra"
)

type Gob

type Gob struct {
	// contains filtered or unexported fields
}

Gob provides APIs to upsert data in bulk

func New

func New(options ...Option) (*Gob, error)

New returns Gob instance customized with options

func (*Gob) Close

func (gob *Gob) Close()

Close the resources

func (*Gob) Upsert

func (gob *Gob) Upsert(ctx context.Context, args UpsertArgs) error

Upsert rows to model

type Option

type Option func(gob *Gob) error

Option to customize Gob

func WithBatchSize

func WithBatchSize(size int) Option

WithBatchSize sets batchSize of Gob to size

func WithConnIdleTime

func WithConnIdleTime(d time.Duration) Option

WithConnIdleTime sets maximum amount of time conn may be idle

func WithConnLifeTime

func WithConnLifeTime(d time.Duration) Option

WithConnLifeTime sets maximum amount of time conn may be reused

func WithDBConnStr

func WithDBConnStr(connStr string) Option

WithDBConnStr sets database conn string

func WithDBProvider

func WithDBProvider(provider DBProvider) Option

WithDBProvider sets provider of database to upsert rows

func WithIdleConns

func WithIdleConns(n int) Option

WithIdleConns sets maximum number of connections idle in pool

func WithOpenConns

func WithOpenConns(n int) Option

WithOpenConns sets maximum number of connections open to database

type Row

type Row map[string]interface{}

Row of model

func NewRow

func NewRow() Row

NewRow return empty row

func (Row) Add

func (row Row) Add(column string, value interface{})

Add column and value to row

func (Row) Columns

func (row Row) Columns() (cols []string)

Columns in sorted order

func (Row) Len

func (row Row) Len() int

Len returns number of columns in row

func (Row) Value

func (row Row) Value(col string) interface{}

Value of col nil if not found

type UpsertArgs

type UpsertArgs struct {
	ConflictAction          // ON CONFLICT action
	Keys           []string // indicate index column names

	Model string // table name
	Rows  []Row  // rows to be upserted
	// contains filtered or unexported fields
}

UpsertArgs to upsert rows

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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