gormer

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2023 License: MIT Imports: 2 Imported by: 0

README

gorm'er —— GORM Manager

Go Report Card tests GoDoc codecov MIT license

Installation

go get github.com/go-packagist/gormer

Usage

package main

import (
	"github.com/go-packagist/gormer"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

type User struct {
	Username string
	Password string
}

func main() {
	g := gormer.New(&gormer.Config{
		Default: "db1",
		Connections: map[string]gormer.ConnectionFunc{
			"db1": func() *gorm.DB {
				return nil
			},
			"db2": func() *gorm.DB {
				dsn := "user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local"

				db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
				if err != nil {
					panic(err)
				}

				return db
			},
		},
	})

	g.Connection().First(&User{}) // use default connection(Recommended usage)
	g.Connection("db1").First(&User{}) // use db1 connection
	g.Connection("db2").First(&User{})
}

License

The MIT License (MIT). Please see License File for more information.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Default     string
	Connections map[string]ConnectionFunc
}

type ConnectionFunc

type ConnectionFunc func() *gorm.DB

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

func New added in v1.1.0

func New(config *Config) *Manager

func NewManager

func NewManager(config *Config) *Manager

func (*Manager) Connection

func (m *Manager) Connection(name ...string) *gorm.DB

Jump to

Keyboard shortcuts

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