Categorygithub.com/igordth/database-simplify
module
0.0.0-20250217200315-567792bd6a1e
Repository: https://github.com/igordth/database-simplify.git
Documentation: pkg.go.dev

# README

database-simplify

Package for simplify working with databases.

Install

Install with

go get github.com/igordth/database-simplify

Or import

import "github.com/igordth/database-simplify"

Database list

Basic ORM package

pggorm

Package for work with PostgreSQL using gorm.

Creating logger for connection with zap.Logger

log := NewLog(log, cfg)

where

  • log is *zap.Logger hear
  • cfg is config from gorm.io/gorm/logger hear

If log is nil returned logger.Discard - print any log to io.Discard

PS: If you don`t want use logger put logger.Discard to NewConnection or nil to NewLog - don`t use zap.NewNope because it is more resource-intensive

Example in: https://github.com/igordth/database-simplify/tree/master/examples/pggorm/connection

Config

  • Name - name of database
  • User - user for connection
  • Password - password of user
  • Host - host of database
  • Schema - scheme of database, if not set default postgres schema usage - public
  • MaxOpenConn - sets the maximum number of open connections to the database
  • MaxIdleConn - maximum number of connections in the idle pool
  • Options - other options for connection string

Example in: https://github.com/igordth/database-simplify/tree/master/examples/pggorm/connection

Creating connection

cnn, df, err := pggorm.NewConnection(cfg, log)
if err != {
	// todo
	return
}
defer df()

Example in: https://github.com/igordth/database-simplify/tree/master/examples/pggorm/connection

Base connection methods

  • DB()
  • Gorm(ctx context.Context)

Example in: https://github.com/igordth/database-simplify/tree/master/examples/pggorm/connection

Connection methods for transaction

  • TrxBegin(ctx context.Context, opts ...*sql.TxOptions) - creates a context through which the transaction will proceed
  • TrxRollback(ctx *context.Context) - roll back a transaction by context
  • TrxCommit(ctx *context.Context) - commit transaction by context

Example in: https://github.com/igordth/database-simplify/tree/master/examples/pggorm/transaction

Usage

Append default methods to gateways [db->schema->table-gateway].

  • Count - get count of records. gorm doc
  • CreateModel - create record by model. gorm doc
  • CreateMap - create record by map. gorm doc
  • Delete - deletes value matching given conditions. gorm doc
  • Find - retrieving object(s) with conditions. gorm doc
  • Save - updates value in database. If value doesn't contain a matching primary key, value is inserted. gorm doc
  • Update - updates column with value using callbacks. gorm doc
  • Updates - updates attributes using callbacks. values must be a struct or map. gorm doc

Add needed methods to your gateway like:

type Model struct {
    ID     uint   `gorm:"primary_key;column:id"`
    Name   string `gorm:"column:name"`
}

func (Model) TableName() string {
    return "some_table_name"
}

type MySomeTableName struct {
	pggorm.Connect
	usage.FindCompare[Model]
}

func New(cnn pggorm.Connect) *MySomeTableName {
	return &MySomeTableName{
		cnn,
		usage.NewFindCompare[Model](cnn),
	}
}

// todo my extra methods of MySomeTableName

Link:

And use like:

gate := New(cnn)
result, err := gate.Find.One.Execute(ctx) // result is *Model of firs row from table some_table_name
if err != nil {
    return panic(err)
}
fmt.Printf("%+v", result)

Example in: https://github.com/igordth/database-simplify/tree/master/examples/pggorm/usage

With

Conditions for usage. To create more complex queries.

  • Preload(query string, args ...any) - preload associations with given conditions. gorm doc
  • Order(values ...string) - specify order when retrieving records from database. gorm doc
  • Where(query any, args ...any) - add where conditions. gorm doc
  • Limit(limit int, offset int) - specify the [limit] number of records to be retrieved and skip [offset] before starting. gorm doc
  • Joins(query string, args ...any) - specify Joins conditions. gorm doc1 gorm doc2
  • GroupBy(query string) - specify the group method on the find. gorm doc
  • Having(query string, args ...any) - specify HAVING conditions for GROUP BY. gorm doc
  • Distinct(values ...string) - specify distinct fields that you want querying. gorm doc
  • Select(query any, args ...any) - specify fields that you want when querying, creating, updating. gorm doc
  • Omit(columns ...string) - specify fields that you want to ignore when creating, updating and querying. gorm doc

Use like this:

result, err := gate.Find.Many.
    With(
        with.Where("name like ?", "Bi%"),
        with.Order("name desc"),
    ).
    With(with.Limit(5, 0)).
    Execute(ctx)
if err != nil {
    return panic(err)
}
fmt.Printf("%+v", result)

Example in: https://github.com/igordth/database-simplify/tree/master/examples/pggorm/usage

# Packages

No description provided by the author
No description provided by the author