Database

Getting Started #

gFly encourages the use of both Service for processing logic for a certain function and Repository to operate on each corresponding Model.

Model #

IMPORTANT!
The Model file should be placed in directory app/domain/models
// ================================================================================
//                                     Model User
// ================================================================================
// TableUser Table name
const TableUser = "users"

// User struct to describe a user object.
type User struct {
    ID           uuid.UUID    `db:"id" json:"id" validate:"required,uuid"`
    Email        string       `db:"email" json:"email" validate:"required,email,lte=255"`
    PasswordHash string       `db:"password_hash" json:"-" validate:"required,gte=6"`
    Fullname     string       `db:"fullname" json:"fullname" validate:"lte=255"`
    Phone        string       `db:"phone" json:"phone" validate:"lte=20"`
    Token        string       `db:"token" json:"-" validate:"lte=100"`
    UserStatus   int          `db:"user_status" json:"user_status" validate:"required,len=1"`
    CreatedAt    time.Time    `db:"created_at" json:"created_at" validate:"required"`
    UpdatedAt    time.Time    `db:"updated_at" json:"updated_at" validate:"required"`
    VerifiedAt   sql.NullTime `db:"verified_at" json:"verified_at"`
    BlockedAt    sql.NullTime `db:"blocked_at" json:"blocked_at"`
    DeletedAt    sql.NullTime `db:"deleted_at" json:"deleted_at"`
    LastAccessAt sql.NullTime `db:"last_access_at" json:"last_access_at"`
}

Repository #

IMPORTANT!
The Repository file should be placed in directory app/domain/repository and should use *db.DB

type UserRepository struct {
    DB *db.DB
}

Register repository in file app/domain/repository/repository_factory.go

package repository

import "app/core/db"

// Repositories struct for collect all app repositories.
type Repositories struct {
    *UserRepository
    *RoleRepository
}

// Pool a repository pool to store all
var Pool = &Repositories{
    &UserRepository{DB: db.Instance()},
    &RoleRepository{DB: db.Instance()},
}

Configuration #

Parameters

# NOTE: Database settings:
# DB_DRIVER:
#   - "" EMPTY is disable DB connection
#   - "mysql" Mysql driver
#   - "postgresql" PostgreSQL driver
DB_DRIVER="postgresql"
DB_HOST="localhost"
DB_PORT=5432
DB_NAME="gfly"
DB_USERNAME="user"
DB_PASSWORD="secret"
DB_SSL_MODE="disable"
DB_MAX_CONNECTION=100
DB_MAX_IDLE_CONNECTION=10
DB_MAX_LIFETIME_CONNECTION=2

Libraries #

gFly use below libraries to handle database

Database jmoiron/sqlx #

Library "github.com/jmoiron/sqlx"

sqlx is a library which provides a set of extensions on go’s standard database/sql library. The sqlx versions of sql.DB, sql.TX, sql.Stmt, et al. all leave the underlying interfaces untouched, so that their interfaces are a superset on the standard ones. This makes it relatively painless to integrate existing codebases using database/sql with sqlx.

MySQL Driver go-sql-driver/mysql #

MySQL "github.com/go-sql-driver/mysql"

Go MySQL Driver is a MySQL driver for Go’s (golang) database/sql package

PostgreSQL Driver jackc/pgx #

PostgreSQL "github.com/jackc/pgx"

pgx is a pure Go driver and toolkit for PostgreSQL.

The pgx driver is a low-level, high performance interface that exposes PostgreSQL-specific features such as LISTEN / NOTIFY and COPY. It also includes an adapter for the standard database/sql interface.

The toolkit component is a related set of packages that implement PostgreSQL functionality such as parsing the wire protocol and type mapping between PostgreSQL and Go. These underlying packages can be used to implement alternative drivers, proxies, load balancers, logical replication clients, etc.