gormx

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2023 License: MIT Imports: 10 Imported by: 0

README

GormX - Golang Gorm Utility

Go Report Card GoDoc

GormX is a useful Gorm utility for Golang, which makes it easy to connect to MySQL and PostgreSQL databases.

Features

  • Easily create and manage connections to MySQL and PostgreSQL databases
  • Create, check, and drop databases
  • Create, check, and drop tables
  • Define table structures using gormx.Model and gormx.RelatedModel
  • Preset scopes for sorting and pagination

Installation

go get -u github.com/goliajp/gormx

Usage

Creating a Connection

You can create connections to MySQL and PostgreSQL databases using the gormx.NewMysql() and gormx.NewPg() functions respectively. These functions return a *gorm.DB instance, which you can use for Gorm operations.

MySQL Connection
m := gormx.NewMysql(nil) // nil is the default config: localhost:3306, root, root, mysql
db := m.DB() // db is *gorm.DB, then you can use Gorm normally
PostgreSQL Connection
p := gormx.NewPg(nil) // nil is the default config: localhost:5432, postgres, postgres, postgres, Asia/Shanghai
db := p.DB() // db is *gorm.DB, then you can use Gorm normally
Custom Connection Configuration

You can also provide a custom configuration for both MySQL and PostgreSQL connections.

MySQL
cfg := gormx.MysqlConfig{
    User:     "your username",
    Password: "your password",
    Addr:     "host:port",
    Dbname:   "connect dbname",
}
m := gormx.NewMysql(&cfg)
db := m.Open()
PostgreSQL
cfg := gormx.PgConfig{
    User:   "your username",
    Password: "your password",
    Host:   "your host",
    Port:   9999, // int, your host port
    Dbname: "connect dbname",
    Tz:     "Asia/Shanghai", // timezone
}
p := gormx.NewPg(&cfg)
db := p.Open()
Database Operations

Using the gormx utility, you can perform various database operations such as creating, checking, and dropping databases.

Creating a New Database
if err := gormx.CreateDatabase(db, "testdb"); err != nil {
    // error handler
}
Checking if a Database Exists
hasTdb, err := gormx.HasDatabase(db, "testdb")
if err != nil {
    // error handler
}
Opening a Database
tdb := m.Open("testdb")
Dropping a Database
if err := gormx.DropDatabase(db, "testdb"); err != nil {
    // error handler
}
Table Operations

Using the gormx utility, you can perform various table operations such as creating, checking, and dropping tables.

Dropping Tables
if err := gormx.DropTables(db, "table1", "table2"); err != nil {
    // error handler
}
Checking if a Table Exists
hasTable, err := gormx.HasTable(db, "table1")
if err != nil {
    // error handler
}
Defining Table Structures

gormx provides gormx.Model and gormx.RelatedModel to help you define table structures.

type One struct {
    gormx.Model
    // attrs...
}
type Two struct {
    gormx.Model
    // attrs...
}
type OneTwo struct { // define the relationship of "one" and "two"
    gormx.RelatedModel
    OneId int
    TwoId int
    One *One
    Two *Two
}
Using Preset Scopes

gormx provides preset scopes that can be helpful when you need to retrieve list results.

Sorting Results by created_at in Descending Order
var rs []Foo
db.Scopes(gormx.ScopeOrderByCreatedAtDesc).Find(&rs)
Sorting Results by updated_at in Descending Order
var rs []Foo
db.Scopes(gormx.ScopeOrderByUpdatedAtDesc).Find(&rs)
Pagination
var rs []Foo
db.Scopes(gormx.ScopePagination(1, 10)).Find(&rs)

Contributing

We welcome contributions to GormX! Feel free to submit issues, feature requests, or pull requests on GitHub.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultMysqlConfig = &MysqlConfig{
	User:     "root",
	Password: "root",
	Addr:     "127.0.0.1",
	Dbname:   "mysql",
}
View Source
var DefaultPgConfig = &PgConfig{
	User:     "postgres",
	Password: "postgres",
	Host:     "localhost",
	Port:     5432,
	Dbname:   "postgres",
	Tz:       "Asia/Shanghai",
}

Functions

func CreateDatabase

func CreateDatabase(db *gorm.DB, dbname string) error

func DropDatabase

func DropDatabase(db *gorm.DB, dbname string) error

func DropTables

func DropTables(db *gorm.DB, tables ...string) error

func HasDatabase

func HasDatabase(db *gorm.DB, dbname string) (bool, error)

func HasTable

func HasTable(db *gorm.DB, table string) (bool, error)

func ScopeOrderByCreatedAtDesc

func ScopeOrderByCreatedAtDesc(db *gorm.DB) *gorm.DB

func ScopeOrderByUpdatedAtDesc

func ScopeOrderByUpdatedAtDesc(db *gorm.DB) *gorm.DB

Types

type Model

type Model struct {
	Id        int            `json:"id" gorm:"primaryKey"`
	CreatedAt time.Time      `json:"createdAt" gorm:"index"`
	UpdatedAt time.Time      `json:"updatedAt" gorm:"index"`
	DeletedAt gorm.DeletedAt `json:"deletedAt,omitempty" gorm:"index"`
}

type Mysql

type Mysql struct {
	// contains filtered or unexported fields
}

func NewMysql

func NewMysql(cfg *MysqlConfig) *Mysql

func (*Mysql) DB

func (m *Mysql) DB(args ...string) *gorm.DB

func (*Mysql) Open

func (m *Mysql) Open(args ...string) *gorm.DB

type MysqlConfig

type MysqlConfig struct {
	User     string
	Password string
	Addr     string
	Dbname   string
}

type Pg

type Pg struct {
	// contains filtered or unexported fields
}

func NewPg

func NewPg(cfg *PgConfig) *Pg

func (*Pg) DB

func (p *Pg) DB(args ...string) *gorm.DB

func (*Pg) Open

func (p *Pg) Open(args ...string) *gorm.DB

type PgConfig

type PgConfig struct {
	User     string
	Password string
	Host     string
	Port     int
	Dbname   string
	Tz       string
}

type RelateModel

type RelateModel struct {
	Id        int       `json:"id" gorm:"uniqueIndex;primaryKey"`
	CreatedAt time.Time `json:"createdAt" gorm:"index"`
	UpdatedAt time.Time `json:"updatedAt"`
}

type Scope

type Scope = func(db *gorm.DB) *gorm.DB

func ScopePagination

func ScopePagination(page, pageSize int) Scope

Jump to

Keyboard shortcuts

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