db

package
v0.0.0-...-221f2c5 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const Default = "default"

Variables

This section is empty.

Functions

func DB

func DB(name ...string) *sql.DB

DB returns a db.

func EntDriver

func EntDriver(name ...string) *entsql.Driver

EntDriver returns an ent dialect.Driver.

func New

func New(cfg *DBConfig)

Types

type DBConfig

type DBConfig struct {
	Name   string `json:"name" yaml:"name"`
	Driver string `json:"driver" yaml:"driver"`
	// DSN data source name
	//
	// [MySQL] username:password@tcp(localhost:3306)/dbname?timeout=10s&charset=utf8mb4&collation=utf8mb4_general_ci&parseTime=True&loc=Local
	//
	// [Postgres] host=localhost port=5432 user=root password=secret dbname=test connect_timeout=10 sslmode=disable
	//
	// [SQLite] file::memory:?cache=shared
	DSN string `json:"dsn" yaml:"dsn"`

	// Options optional settings to setup db connection.
	Options *DBOptions `json:"options" yaml:"options"`
}

DBConfig keeps the settings to setup db connection.

type DBDriver

type DBDriver string

DBDriver db driver

const (
	MySQL    DBDriver = "mysql"
	Postgres DBDriver = "postgres"
	SQLite   DBDriver = "sqlite3"
)

type DBOptions

type DBOptions struct {
	// MaxOpenConns is the maximum number of open connections to the database.
	// Use value -1 for no timeout and 0 for default.
	// Default is 20.
	MaxOpenConns int `json:"max_open_conns" yaml:"maxOpenConns"`

	// MaxIdleConns is the maximum number of connections in the idle connection pool.
	// Use value -1 for no timeout and 0 for default.
	// Default is 10.
	MaxIdleConns int `json:"max_idle_conns" yaml:"maxIdleConns"`

	// ConnMaxLifetime is the maximum amount of time a connection may be reused.
	// Use value -1 for no timeout and 0 for default.
	// Default is 10 minutes.
	ConnMaxLifetime time.Duration `json:"conn_max_lifetime" yaml:"connMaxLifetime"`

	// ConnMaxIdleTime is the maximum amount of time a connection may be idle.
	// Use value -1 for no timeout and 0 for default.
	// Default is 5 minutes.
	ConnMaxIdleTime time.Duration `json:"conn_max_idle_time" yaml:"connMaxIdleTime"`
}

DBOptions optional settings to setup db connection.

Jump to

Keyboard shortcuts

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