gormconnect

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 23, 2021 License: GPL-3.0 Imports: 4 Imported by: 0

README

GORM Connect

GORM wrapper to connect to read and write databases

Install

go get github.com/ekyfauzi/gorm-connect

Usages

Connection
import "github.com/ekyfauzi/gormconnect"

func main() {
  host := "YOUR_DB_WRITE_HOST"
  hostRead := "YOUR_DB_READ_HOST"
  port := "YOUR_DB_PORT"
  user := "YOUR_DB_USER"
  passwd := "YOUR_DB_PASSWORD"
  dbName := "YOUR_DB_NAME"
  
  // Set default connection
  conn := gormconnect.Init("mysql")
  conn.SetWrite(host, port, user, passwd, dbName)
  
  // Add read db connection
  // You can skip this if your read and write database in the same host
  // You also can set multiple read connection
  conn.SetRead(hostRead, port, user, passwd, dbName)
  
}
Functions

Because this library is a wrapper of GORM so basically usages of functions are same.

conn.Where()
conn.Save()
conn.Create()
conn.Exec()

If you want to get instance of connection to database, you can use:

// Get write connection
// Return *gorm.DB
conn.Instance("write")

// Get read connection
// Return *gorm.DB
conn.Instance("read")

then you can use as usual GORM

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GormConnection

type GormConnection struct {
	Driver        string
	ReadDatabases []*gorm.DB
	WriteDatabase *gorm.DB
}

func Init

func Init(driver string) *GormConnection

func (*GormConnection) Conn

func (s *GormConnection) Conn() *gorm.DB

func (*GormConnection) Create

func (s *GormConnection) Create(value interface{}) *gorm.DB

func (*GormConnection) Exec

func (s *GormConnection) Exec(sql string, values ...interface{}) *gorm.DB

func (*GormConnection) Instance

func (s *GormConnection) Instance(conn string) *gorm.DB

func (*GormConnection) Save

func (s *GormConnection) Save(value interface{}) *gorm.DB

func (*GormConnection) SetRead

func (s *GormConnection) SetRead(host string, port string, user string, password string, database string)

func (*GormConnection) SetWrite

func (s *GormConnection) SetWrite(host string, port string, user string, password string, database string)

func (*GormConnection) Where

func (s *GormConnection) Where(query interface{}, args ...interface{}) *gorm.DB

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL