Bulk upserts to PostgreSQL, MySQL, Cassandra
go get -u github.com/csmadhu/gob
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)
}
}
All options are optional. Options not applicable to Database provider is ignored.
option | description | type | default | DB providers |
---|---|---|---|---|
WithBatchSize | Transaction Batch Size | int | 10000 |
|
WithDBProvider | Database provider | gob.DBProvider | DBProviderPg |
|
WithDBConnStr | DB URL/DSN
|
string | postgres://postgres:postgres@localhost:5432/gob?pool_max_conns=1 |
|
WithConnIdleTime | Maximum amount of time conn may be idle | time.Duration | 3 second |
|
WithConnLifeTime | Maximum amount of time conn may be reused | time.Duration | 3 second |
|
WithIdleConns | Maximum number of connections idle in pool | int | 2 |
|
WithOpenConns | Maximum number of connections open to database | int | 10 |
|
Examples for supported Database provider can be found here