gormer

package module
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2023 License: MIT Imports: 6 Imported by: 0

README

Gorm'er —— GORM Manager

Go Version GoDoc codecov Go Report Card tests MIT license

Installation

go get github.com/go-packagist/gormer/v2

Usage

package main

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

func main() {
	m := gormer.NewManager(&gormer.Config{
		Default: "db1",
		Connections: map[string]gormer.Connection{
			"db1": &gormer.MySQLConfig{
				DSN: gormer.DSN{
					Host: "127.0.0.1",
					Port: 3306,
					User: "root",
					Pass: "123456",
					DB:   "test",
					Options: map[string]string{
						"charset":   "utf8mb4",
						"parseTime": "True",
					},
				}.String(),
				GormConfig: &gorm.Config{
					SkipDefaultTransaction: true,
				},
			},
			"db2": &gormer.MySQLConfig{
				DSN: gormer.DSN{
					Host: "...",
				}.String(),
			},
		},
	}, gormer.WithInstance) // Instance mode

	var user = struct {
		Name string
	}{}
	m.Connect("db1").DB.First(&user)
	gormer.Factory().Connect("db1").DB.First(&user) // use factory mode
	gormer.Gormer().Connect("db2").DB.First(&user)  // use instance mode
	gormer.Connect("db1").DB.First(&user)           // use instance mode
	gormer.Connect().DB.First(&user)                // use default connection
	gormer.Connect().First(&user)                   // use default connection and ignore `DB`

	println(gormer.Connect("db2").Err) // if db2 is err
}

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

func SetInstance

func SetInstance(m *Manager)

Types

type Config

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

type Connection

type Connection interface{}

type DB

type DB struct {
	*gorm.DB
	Err error
}

func Connect

func Connect(name ...string) *DB

type DSN

type DSN struct {
	Host    string
	Port    int
	User    string
	Pass    string
	DB      string
	Options map[string]string
}

func (DSN) String

func (d DSN) String() string

type Manager

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

func Factory

func Factory() *Manager

func Gormer

func Gormer() *Manager

func NewManager

func NewManager(config *Config, opts ...Option) *Manager

func WithInstance

func WithInstance(m *Manager) *Manager

func (*Manager) Connect

func (m *Manager) Connect(name ...string) *DB

type MySQLConfig

type MySQLConfig struct {
	DSN        string
	GormConfig *gorm.Config
	GormUsees  []gorm.Plugin
}

type Option

type Option func(*Manager) *Manager

Jump to

Keyboard shortcuts

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