Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/csmadhu/gob

Bulk Inserts to Postgres, MySQL, Cassandra
https://github.com/csmadhu/gob

cassandra golang mysql postgresql

Last synced: 12 days ago
JSON representation

Bulk Inserts to Postgres, MySQL, Cassandra

Awesome Lists containing this project

README

        

# gob
Bulk upserts to PostgreSQL, MySQL, Cassandra

[![Go Report Card](https://goreportcard.com/badge/github.com/csmadhu/gob)](https://goreportcard.com/report/github.com/csmadhu/gob)
[![GoDoc](https://godoc.org/github.com/csmadhu/gob?status.svg)](https://pkg.go.dev/github.com/csmadhu/gob?tab=doc)
[![Conventional Commit](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org)
[![Gitmoji](https://img.shields.io/badge/gitmoji-%20😜%20😍-FFDD67.svg?style=flat-square)](https://gitmoji.carloscuesta.me)
[![Go](https://github.com/csmadhu/gob/workflows/Go/badge.svg)](https://github.com/csmadhu/gob/actions)
![Supported Go Versions](https://img.shields.io/badge/Go-1.15+-lightgrey.svg)
[![GitHub Release](https://img.shields.io/github/release/csmadhu/gob.svg)](https://github.com/csmadhu/gob/releases)
[![License](https://img.shields.io/badge/license-GPL%20(%3E%3D%202)-blue)](https://github.com/csmadhu/gob/blob/master/LICENSE)

---------------------------------------
* [Installation](#installation)
* [Usage](#usage)
* [Options](#options)
* [Examples](#examples)
---------------------------------------

## Installation
```bash
go get -u github.com/csmadhu/gob
```

# Usage
```go
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.go.dev/github.com/jackc/pgconn?tab=doc#ParseConfig

    • https://pkg.go.dev/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](https://github.com/csmadhu/gob/tree/master/examples)