database

package
v1.1.46 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnknownDBType = errors.New("unknown database type")
	ErrDBNameEmpty   = errors.New("database name is empty")
	ErrDBHostEmpty   = errors.New("database host is empty")
)

Functions

func LoadGenericDBConfig added in v1.1.34

func LoadGenericDBConfig(fileName string, genericDBConfigMap *GenericDBConfigMap) error

LoadGenericDBConfig reads a config yaml file Sapple config file : databases:

  • type: mysql name: my-mysql-db host: myMysqlHost database: myMysqlDatabase user: myMysqlUser password: myMysqlPassword port: 3306
  • type: postgre name: my-postgres-db host: myPostgresHost database: myPostgresDatabase user: myPostgresUser password: myPostgresPassword port: 5432

func MySQLConnect

func MySQLConnect(config *DBConfig) (*gorm.DB, error)

MySQLConnect connects to a MySQL database

Types

type DBConfig

type DBConfig struct {
	Host     string `default:"127.0.0.1" json:"host"`
	Database string `required:"true" json:"database"`
	User     string `default:"root" json:"user"`
	Password string `required:"true" env:"MysqlPassword"`
	Port     uint   `default:"3306" json:"port"`
}

func LoadMySQLConfig

func LoadMySQLConfig(configFiles ...string) (*DBConfig, error)

LoadMySQLConfig returns the mysql connection config

func (*DBConfig) MySqlDSN

func (dc *DBConfig) MySqlDSN() string

MySqlDSN returns the connection string for MySQL

type DBNameType added in v1.1.34

type DBNameType string

type GenericDBConfig added in v1.1.34

type GenericDBConfig struct {
	Name     DBNameType `required:"true" json:"name"` // database connection name, ex : orders-mysql
	Type     string     `required:"true" json:"type"` // mysql/postgre
	Host     string     `required:"true" json:"host"`
	Database string     `required:"true" json:"database"`
	User     string     `required:"true" json:"user"`
	Password string     `required:"true"`
	Port     uint       `required:"true" json:"port"`
}

GenericDBConfig holds database config for MySQL/Postgre/...

func (*GenericDBConfig) Connect added in v1.1.34

func (dbConfig *GenericDBConfig) Connect() (*gorm.DB, error)

Connect connects to the database

func (*GenericDBConfig) DSN added in v1.1.34

func (dbConfig *GenericDBConfig) DSN() (string, error)

DSN returns the connection string corresponding to the database type

func (*GenericDBConfig) Validate added in v1.1.34

func (dbConfig *GenericDBConfig) Validate() error

type GenericDBConfigMap added in v1.1.34

type GenericDBConfigMap map[DBNameType]GenericDBConfig

GenericDBConfigMap holds the database configs by name

Jump to

Keyboard shortcuts

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