Skip to content
/ gormx Public

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

License

Notifications You must be signed in to change notification settings

goliajp/gormx

Repository files navigation

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.

About

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

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages