database
v1.2.3
Documentation
pkg.go.dev ↗Functions
Count
func Count(db *sqlx.DB, driver Driver, query string, args ...any) (int64, error)
Count get count of records
Find
func Find[T any](db *sqlx.DB, driver Driver, query string, resolver func(*T), args ...any) ([]T, error)
Find get multiple entity (resolve entity from db struct tag)
You can pass resolver to manipulate record after read you can use `q` struct for advanced field select query
FindOne
func FindOne[T any](db *sqlx.DB, driver Driver, query string, resolver func(*T), args ...any) (*T, error)
FindOne get single entity
You can pass resolver to manipulate record after read you can use `q` or `db` struct tag to map field to database column
Insert
func Insert(db *sqlx.DB, entity any, table string, driver Driver) (sql.Result, error)
Insert struct to database
NewMySQLConnector
func NewMySQLConnector(host string, username string, password string, database string) (*sqlx.DB, error)
NewMySQLConnector create new mysql connection
NewPostgresConnector
func NewPostgresConnector(host string, port string, user string, password string, database string) (*sqlx.DB, error)
NewPostgresConnector create new POSTGRES connection
ResolveInsert
func ResolveInsert[T any](entity T, table string, driver Driver) (string, []any)
ResolveInsert create insert cmd for table
@returns insert command and params as result
ResolveQuery
func ResolveQuery[T any](query string, driver Driver) string
ResolveQuery get list of fields from struct `q` and `db` tag and replace with `SELECT ...` keyword in query
ResolveUpdate
func ResolveUpdate[T any](entity T, table string, driver Driver, condition string, args ...any) (string, []any)
ResolveUpdate create update cmd for table and
You must pass condition argument with ? @returns query and params as result
Update
func Update(db *sqlx.DB, entity any, table string, driver Driver, condition string, args ...any) (sql.Result, error)
Update update struct in database
Types
Driver
type Driver string
DriverMySQL
const DriverMySQL Driver = "mysql"
DriverPostgres
const DriverPostgres Driver = "postgres"
IDecoder
type IDecoder interface {
Decode() error
}
Query
type Query struct {
Type string
Query string
Params []any
Closure bool
}
Query object
QueryBuilder
type QueryBuilder interface {
// And add new simple condition to query with AND
And(cond string, args ...any)
// Or add new simple condition to query with OR
Or(cond string, args ...any)
// AndClosure add new condition to query with AND in nested ()
AndClosure(cond string, args ...any)
// OrClosure add new condition to query with OR in nested ()
OrClosure(cond string, args ...any)
// ToSQL generate query with placeholder based on counter
ToSQL(counter int) string
// Params get list of query parameters
Params() []any
}
NewQuery
func NewQuery(driver Driver) QueryBuilder
NewQuery generate new query builder