Documentation
¶
Index ¶
- Constants
- func Connect(connURL, driver string) (*sqlx.DB, error)
- func CreateModel[T any](m *T) error
- func DeleteModel[T any](m *T) error
- func FindModels[T any](page, limit int, sortField string, sortDir qb.OrderByDir, ...) ([]T, int, error)
- func GetModel[T any](conditions ...qb.Condition) (*T, error)
- func GetModelBy[T any](field string, value any) (*T, error)
- func GetModelByID[T any](value any) (*T, error)
- func GetModelWhereEq[T any](field string, value any) (*T, error)
- func Load()
- func Register(driver IDatabase)
- func UpdateModel[T any](m *T) error
- type Column
- type DB
- type DBModel
- func (db *DBModel) Begin() *DBModel
- func (db *DBModel) Commit() error
- func (db *DBModel) Create(model any) (err error)
- func (db *DBModel) Delete(model any) error
- func (db *DBModel) Fetch(offset, fetch int) *DBModel
- func (db *DBModel) Find(model any) (total int, err error)
- func (db *DBModel) First(model any) (err error)
- func (db *DBModel) Get(model any, getType GetOne) (err error)
- func (db *DBModel) GroupBy(fields ...string) *DBModel
- func (db *DBModel) Having(field any, opt fluentsql.WhereOpt, value any) *DBModel
- func (db *DBModel) Join(join fluentsql.JoinType, table string, condition fluentsql.Condition) *DBModel
- func (db *DBModel) Last(model any) (err error)
- func (db *DBModel) Limit(limit, offset int) *DBModel
- func (db *DBModel) Model(model any) *DBModel
- func (db *DBModel) Omit(columns ...any) *DBModel
- func (db *DBModel) OrderBy(field string, dir fluentsql.OrderByDir) *DBModel
- func (db *DBModel) Raw(sqlStr string, args ...any) *DBModel
- func (db *DBModel) RemoveFetch() fluentsql.Fetch
- func (db *DBModel) RemoveLimit() fluentsql.Limit
- func (db *DBModel) Rollback() error
- func (db *DBModel) Select(columns ...any) *DBModel
- func (db *DBModel) Update(model any) (err error)
- func (db *DBModel) When(condition bool, groupCondition fluentsql.FnWhereBuilder) *DBModel
- func (db *DBModel) Where(field any, opt fluentsql.WhereOpt, value any) *DBModel
- func (db *DBModel) WhereGroup(groupCondition fluentsql.FnWhereBuilder) *DBModel
- func (db *DBModel) WhereOr(field any, opt fluentsql.WhereOpt, value any) *DBModel
- type GetOne
- type IDatabase
- type MetaData
- type Raw
- type Table
Constants ¶
const ( MODEL = "model" // Tag `model` TABLE = "table" // Table name TYPE = "type" // Column types REFERENCE = "ref" // Column reference CASCADE = "cascade" // Column cascade DELETE, UPDATE RELATION = "rel" // Column relationship NAME = "name" // Column name )
Variables ¶
This section is empty.
Functions ¶
func CreateModel ¶
CreateModel creates a new record of type T in the database. It begins a transaction, attempts to create the record, and commits the transaction. If an error occurs, the transaction is rolled back and the error is returned.
Parameters:
- m: A pointer to the model to be created.
Returns:
- error: An error object if an error occurs during the creation process.
func DeleteModel ¶
DeleteModel deletes a record of type T from the database. It begins a transaction, deletes the record, and commits the transaction. If an error occurs, the transaction is rolled back and the error is returned.
Parameters:
- m: A pointer to the model to be deleted.
Returns:
- error: An error object if an error occurs during the deletion process.
func FindModels ¶ added in v1.5.0
func FindModels[T any](page, limit int, sortField string, sortDir qb.OrderByDir, conditions ...qb.Condition) ([]T, int, error)
FindModels retrieves a paginated list of records of type T from the database that match the provided conditions.
Parameters:
- page (int): The current page number (1-based). Defaults to 0 if not provided.
- limit (int): The number of records to retrieve per page.
- conditions (...qb.Condition): Variadic list of qb.Condition specifying the field, operator, and value to filter the query.
Returns:
- ([]T): A slice of records of type T.
- (int): The total number of records that match the conditions.
- (error): An error object if an error occurs during the retrieval process.
func GetModel ¶
GetModel retrieves the first record of type T from the database that matches the provided conditions.
Parameters:
- m: A pointer to the model where the result will be stored.
- conditions: Variadic list of qb.Condition specifying the field, operator, and value to filter the query.
Returns:
- error: An error object if an error occurs during the retrieval process. Returns nil if the query succeeds. Logs unexpected errors.
func GetModelBy ¶ added in v1.5.0
GetModelBy allows filtering records of type T from the database by specifying a field and its required value.
This is a helper function that makes use of the GetModelWhereEq function to apply an equality condition.
Generic Type:
- T: The type of the model.
Parameters:
- field (string): The name of the database field to filter on.
- value (any): The value the specified field is required to equal.
Returns:
- *T: A pointer to the first matching record of type T retrieved from the database, or nil if no record is found.
- error: An error object if an error occurs during the retrieval process.
func GetModelByID ¶ added in v1.5.0
GetModelByID retrieves the first record of type T from the database where the "id" field matches the specified value.
This is a convenience wrapper around GetModelBy for querying records by their unique identifier.
Generic Type:
- T: The type of the model.
Parameters:
- value (any): The value of the "id" field to match against.
Returns:
- *T: A pointer to the retrieved model of type T, or nil if no matching record is found.
- error: An error object if an error occurs during the retrieval process.
func GetModelWhereEq ¶ added in v1.5.0
GetModelWhereEq retrieves the first record of type T from the database where the specified field equals the given value.
Parameters:
- m: A pointer to the model where the result will be stored.
- field: The name of the database field to filter by.
- value: The value to match the field against.
Returns:
- error: An error object if an error occurs during the retrieval process. Returns nil if the query succeeds.
func Register ¶
func Register(driver IDatabase)
Register assign DB provider type fluentsql.PostgreSQL, fluentsql.MySQL,...
func UpdateModel ¶
UpdateModel updates a record of type T in the database. It begins a transaction, updates the record, and commits the transaction. If an error occurs, the transaction is rolled back and the error is returned.
Parameters:
- m: A pointer to the model to be updated.
Returns:
- error: An error object if an error occurs during the update process.
Types ¶
type Column ¶
type Column struct {
Key string
Name string
Primary bool
Types string
Ref string // Reference id to table
Relation string // Relation to table
IsZero bool // Keep Zero value of type
HasValue bool
}
Column structure
type DBModel ¶
type DBModel struct {
// contains filtered or unexported fields
}
func (*DBModel) Join ¶
func (db *DBModel) Join(join fluentsql.JoinType, table string, condition fluentsql.Condition) *DBModel
Join builder
func (*DBModel) OrderBy ¶
func (db *DBModel) OrderBy(field string, dir fluentsql.OrderByDir) *DBModel
OrderBy builder
func (*DBModel) When ¶
func (db *DBModel) When(condition bool, groupCondition fluentsql.FnWhereBuilder) *DBModel
When checking TRUE to build Where condition.
func (*DBModel) WhereGroup ¶
func (db *DBModel) WhereGroup(groupCondition fluentsql.FnWhereBuilder) *DBModel
WhereGroup combine multi where conditions into a group.