Skip to content

csmadhu/gob

Repository files navigation

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 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