Categorygithub.com/proost/dbresolver
modulepackage
0.2.1
Repository: https://github.com/proost/dbresolver.git
Documentation: pkg.go.dev

# README

dbresolver

dbresolver is sqlx resolver and wrapper for database cluster.

CI Go Reference

Install

go get github.com/proost/dbresolver

Usage

package main

import (
	"context"
	"fmt"
	"log"
	
	"github.com/jmoiron/sqlx"
	"github.com/proost/dbresolver"
)

func main() {
    var (
        primaryHost       = "localhost"
        primaryPort       = 3306
        primaryUser       = "primary"
        primaryPassword   = "<password>"
        secondaryHost     = "localhost"
        secondaryPort     = 3307
        secondaryUser     = "secondary"
        secondaryPassword = "<password>"
        dbname            = "<dbname>"
    )
    // DSNs
    primaryDSN := fmt.Sprintf(
        "%s:%s@tcp(%s:%d)/%s",
        primaryUser,
        primaryPassword,
        primaryHost,
        primaryPort,
        dbname,
    )
    secondaryDSN := fmt.Sprintf(
        "%s:%s@tcp(%s:%d)/%s",
        secondaryUser,
        secondaryPassword,
        secondaryHost,
        secondaryPort,
        dbname,
    )

    // connect to primary
    primaryDB := sqlx.MustOpen("mysql", primaryDSN)
    // connect to secondary
    secondaryDB := sqlx.MustOpen("mysql", secondaryDSN)

    primaryDBsCfg := &dbresolver.PrimaryDBsConfig{
      DBs:             []*sqlx.DB{primaryDB},
      ReadWritePolicy: dbresolver.ReadWrite,
    }
    resolver := dbresolver.MustNewDBResolver(primaryDBsCfg, dbresolver.WithSecondaryDBs(secondaryDB))
    defer resolver.Close()

    resolver.MustExecContext(context.Background(), "INSERT INTO users (name) VALUES (?)", "foo")
    result, err := resolver.QueryxContext(context.Background(), `SELECT * FROM users WHERE name = "foo"`)
    if err != nil {
      log.Panic(err)
    }

    fmt.Println(result)
}

Important Notes

  • Primary Database will be used when you call these functions
    • Begin
    • BeginTx
    • BeginTxx
    • Beginx
    • Conn
    • Connx
    • Exec
    • ExecContext
    • MustBegin
    • MustBeginTx
    • MustExec
    • MustExecContext
    • NamedExec
    • NamedExecContext
  • Readable Database(Secondary Database or Primary Database depending on configuration) will be used when you call these functions
    • Get
    • GetContext
    • NamedQuery
    • NamedQueryContext
    • Query
    • QueryContext
    • QueryRow
    • QueryRowContext
    • QueryRowx
    • QueryRowxContext
    • Select
    • SelectContext

Contribution

To contribute to this project, you can open a PR or an issue.

# Functions

No description provided by the author
NewDBResolver creates a new DBResolver and returns it.
NewPrimaryDBsConfig creates a new PrimaryDBsConfig and returns it.
No description provided by the author
WithLoadBalancer sets the load balancer.
WithSecondaryDBs sets the secondary databases.

# Constants

ReadWritePolicies.
ReadWritePolicies.

# Structs

Options is the config for dbResolver.
PrimaryDBsConfig is the config of primary databases.
RandomLoadBalancer is a load balancer that chooses a database randomly.

# Interfaces

DBResolver chooses one of databases and then executes a query.
LoadBalancer chooses a database from the given databases.
NamedStmt is a wrapper around sqlx.NamedStmt.
Stmt is a wrapper around sqlx.Stmt.

# Type aliases

OptionFunc is a function that configures a Options.
ReadWritePolicy is the read/write policy for the primary databases.