Categorygithub.com/skiphead/pgx-pgbouncer-connect
repository
1.0.10
Repository: https://github.com/skiphead/pgx-pgbouncer-connect.git
Documentation: pkg.go.dev

# Packages

No description provided by the author

# README

PGX-PGBOUNCER-CONNECT - add-on for pgx driver.

It allows you to make an automatic choice between two nodes in a PostgreSQL cluster, which is always necessary in high availability systems. It also combines database migration using the goose tool, doing everything in a single package. It is very comfortable. ORM functionality is added as needed, but raw queries are often used.

Install

go get "github.com/skiphead/pgx-pgbouncer-connect

Example Usage

package main

import (
	"context"
	"fmt"
	"github.com/skiphead/pgx-pgbouncer-connect/storage"
	"log"
)

func main() {

	config := storage.Config{
		User:        "db_user",
		Password:    "par0l",
		DbName:      "my_db",
		MasterHost:  "192.168.0.1",
		MasterPort:  "6544",
		ReplicaHost: "192.168.0.2",
		ReplicaPort: "6544",
		Schema:      "db_schema",
	}

	orm := storage.Connection{
		Columns:       []string{"field1", "field2", "field3"},
		StorageConfig: &config,
		TableName:     "table",
	}

	//Migrate table to db
	migrate := config.Migrate("./PathToSqlMigrateFiles")
	migrate.Up()



	//Make SQL query
	sql := "SELECT * FROM my_db"
	var value string
	//Raw query 
	pool, _ := config.ReliableConn(context.Background())
	err := pool.QueryRow(context.Background(), sql).Scan(&value)
	if err != nil {
		log.Fatal(err)
	}
	//Result select
	fmt.Println(value)

	//ORM query INSERT data to table
	db, err := orm.New()
	q, err := db.Pool.Exec(context.Background(), db.Insert(), "1", "2")

	//Result true or false
	fmt.Println(q.Insert())
}