db

package
v2.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2023 License: MIT Imports: 17 Imported by: 0

README

= Database configuration

== Environments


|===
|Env name |Required|Default value

|`DB_DIALECT`
|no
|pgx

|`DB_HOST`
|*yes*
|

|`DB_PORT`
|no
|5432

|`DB_NAME`
|*yes*
|

|`DB_SSL_MODE`
|no
|disable

|`DB_MAX_IDLE_CONNECTIONS`
|no
|5

|`DB_MAX_OPEN_CONNECTIONS`
|no
|15

|`DB_MAX_LIFETIME_IN_MINUTES`
|no
|15

|`DB_USER`
|yes
|

|`DB_PASSWORD`
|*yes*
|

|`SCHEMA_DB_USER`
|no
|env value `DB_USER`

|`SCHEMA_DB_PASSWORD`
|no
|env value `DB_PASSWORD`

|`GOOSE_MIGRATION_DIR`
|no
|/db/migrations
|===

== Конвенция работы с БД из сервисов
. For entities, it is recommended to use the method link:https://gorm.io/docs/conventions.html#TableName[`TableName`] and extend from link:base-entity.go[BaseEntity]
+
[sourse,go]
====
type MyEntity struct {
	BaseEntity
}

func (MyEntity) TableName() string {
	return "db-table-name"
}
====

include::naming-policy.adoc[]

include::TODO.adoc[]

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCreateInstance = errors.New("DB instance creation error")
	ErrDbMigration    = errors.New("database migration error")
	ErrConnectDB      = errors.New("database connection error")
)

Functions

This section is empty.

Types

type BaseEntity

type BaseEntity struct {
	UID *uuid.UUID `gorm:"column:uid; type:uuid; primaryKey; default:uuid_generate_v4()"`

	CreatedAt time.Time      `gorm:"column:created_at"`
	UpdatedAt time.Time      `gorm:"column:updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"column:deleted_at"`
}

type Config

type Config struct {
	Dialect string `env:"DB_DIALECT" envDefault:"pgx"`
	Host    string `env:"DB_HOST,notEmpty"`
	Port    int    `env:"DB_PORT" envDefault:"5432"`
	Name    string `env:"DB_NAME,notEmpty"`
	SslMode string `env:"DB_SSL_MODE" envDefault:"disable"`

	MaxIdleConnections   int `env:"DB_MAX_IDLE_CONNECTIONS" envDefault:"5"`
	MaxOpenConnections   int `env:"DB_MAX_OPEN_CONNECTIONS" envDefault:"15"`
	MaxLifetimeInMinutes int `env:"DB_MAX_LIFETIME_IN_MINUTES" envDefault:"15"`

	GooseMigrationDir string `env:"GOOSE_MIGRATION_DIR" envDefault:"/db/migrations"`
}

type Credential

type Credential struct {
	User     string `env:"DB_USER"`
	Password string `env:"DB_PASSWORD"`
}

type DB

type DB struct {
	Config *Config
	// contains filtered or unexported fields
}

func NewDB

func NewDB(ctx context.Context, dbEmbedMigrations *embed.FS) (*DB, error)

func (*DB) GetGorm

func (receiver *DB) GetGorm() *gorm.DB

Jump to

Keyboard shortcuts

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