gormdb

package
v0.0.56 Latest Latest
Warning

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

Go to latest
Published: May 6, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetMysqlDsn added in v0.0.4

func GetMysqlDsn(databaseName string, config *Config) string

func GetPostgresqlDsn added in v0.0.4

func GetPostgresqlDsn(databaseName string, config *Config) string

Types

type Config

type Config struct {
	Host     string
	Port     int
	User     string
	Password string

	AutoMigrate bool
}

type DbTypeGetDatabase

type DbTypeGetDatabase interface {
	GetDatabase() string
}

type DbTypeGetDsn added in v0.0.4

type DbTypeGetDsn interface {
	GetDsn(config *Config) string
}

refer: https://gorm.io/zh_CN/docs/connecting_to_the_database.html

type DbTypeGetTables

type DbTypeGetTables interface {
	GetTables() []interface{}
}

type GormDb

type GormDb[T comparable] struct {
	// contains filtered or unexported fields
}

T is a type representing the database of mappings

func NewGormDb

func NewGormDb[T comparable](config *Config) *GormDb[T]

func (*GormDb[T]) Connect

func (o *GormDb[T]) Connect() error

func (*GormDb[T]) Get

func (o *GormDb[T]) Get() *gorm.DB

return a gorm.DB or panic

func (*GormDb[T]) GetDb

func (o *GormDb[T]) GetDb(dbType T) *gorm.DB

return a gorm.DB or panic

if o.config.AutoMigrate is true:

will create database if not exist. database name = T.GetDatabase()
will auto migrate tables if T.GetTables() is not empty

func (*GormDb[T]) Start

func (o *GormDb[T]) Start(ctx context.Context) error

func (*GormDb[T]) Stop

func (o *GormDb[T]) Stop(ctx context.Context) error

Jump to

Keyboard shortcuts

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