# 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
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 databaseUser
- user for connectionPassword
- password of userHost
- host of databaseSchema
- scheme of database, if not set default postgres schema usage - publicMaxOpenConn
- sets the maximum number of open connections to the databaseMaxIdleConn
- maximum number of connections in the idle poolOptions
- 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 proceedTrxRollback(ctx *context.Context)
- roll back a transaction by contextTrxCommit(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 docCreateModel
- create record by model. gorm docCreateMap
- create record by map. gorm docDelete
- deletes value matching given conditions. gorm docFind
- retrieving object(s) with conditions. gorm docSave
- updates value in database. If value doesn't contain a matching primary key, value is inserted. gorm docUpdate
- updates column with value using callbacks. gorm docUpdates
- 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 docOrder(values ...string)
- specify order when retrieving records from database. gorm docWhere(query any, args ...any)
- add where conditions. gorm docLimit(limit int, offset int)
- specify the [limit] number of records to be retrieved and skip [offset] before starting. gorm docJoins(query string, args ...any)
- specify Joins conditions. gorm doc1 gorm doc2GroupBy(query string)
- specify the group method on the find. gorm docHaving(query string, args ...any)
- specify HAVING conditions for GROUP BY. gorm docDistinct(values ...string)
- specify distinct fields that you want querying. gorm docSelect(query any, args ...any)
- specify fields that you want when querying, creating, updating. gorm docOmit(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