Documentation
¶
Index ¶
- Constants
- func HandleRestApi[T IEntity](config RestApiConfig[T]) func(w http.ResponseWriter, r *http.Request)
- func NewRef() string
- type Entity
- func (T *Entity) DataVersion() string
- func (T *Entity) Def() *EntityDef
- func (T *Entity) GetValues() map[string]IFieldValue
- func (T *Entity) IsDeleted() bool
- func (T *Entity) IsNew() bool
- func (T *Entity) LoadFrom(src IEntity, predefinedFields bool) error
- func (T *Entity) MarshalJSON() ([]byte, error)
- func (T *Entity) RefString() string
- func (T *Entity) Save(ctx context.Context) error
- func (T *Entity) SetIsDeleted(newValue bool)
- func (T *Entity) UnmarshalJSON(b []byte) error
- type EntityDef
- func (T *EntityDef) ActualDataVersionCheckMode() int
- func (T *EntityDef) AddBoolFieldDef(name string) (*FieldDef, error)
- func (T *EntityDef) AddDateTimeFieldDef(name string) (*FieldDef, error)
- func (T *EntityDef) AddIndex(Unique bool, fld ...*FieldDef) error
- func (T *EntityDef) AddIntFieldDef(name string) (*FieldDef, error)
- func (T *EntityDef) AddNumericFieldDef(name string, Precision int, Scale int) (*FieldDef, error)
- func (T *EntityDef) AddRefFieldDef(name string, refType *EntityDef) (*FieldDef, error)
- func (T *EntityDef) AddStringFieldDef(name string, size int) (*FieldDef, error)
- func (T *EntityDef) FieldDefByName(name string) *FieldDef
- func (T *EntityDef) SelectEntities(filters []*Filter, sorts []*SortItem, pageNo int, pageSize int) (result []*Entity, pagesCount int, err error)
- func (T *EntityDef) SqlTableName() (string, error)
- type EntityHandlerFunc
- type EntityHandlerFuncByRef
- type EntityHandlerFuncNoContext
- type Factory
- func (f *Factory) AddAfterSaveHandler(dest any, handler EntityHandlerFunc) error
- func (f *Factory) AddBeforeDeleteHandler(dest any, handler EntityHandlerFunc) error
- func (f *Factory) AddBeforeDeleteHandlerByRef(dest any, handler EntityHandlerFuncByRef) error
- func (f *Factory) AddBeforeSaveHandler(dest any, handler EntityHandlerFunc) error
- func (f *Factory) AddBeforeSaveHandlerByRef(dest any, handler EntityHandlerFuncByRef) error
- func (f *Factory) AddFillNewHandler(dest any, handler EntityHandlerFuncNoContext) error
- func (f *Factory) BeginTran() (*sql.Tx, error)
- func (f *Factory) CommitTran(tx *sql.Tx) error
- func (T *Factory) CreateEntity(def *EntityDef) (*Entity, error)
- func (T *Factory) CreateEntityDef(ObjectName string, TableName string) (*EntityDef, error)
- func (T *Factory) CreateEntityWrapped(def *EntityDef) (any, error)
- func (T *Factory) DbDialect() int
- func (T *Factory) DeleteEntity(ctx context.Context, ref string) error
- func (T *Factory) EnsureDBStructure() error
- func (f *Factory) Exec(query string, args ...any) (sql.Result, error)
- func (T *Factory) IsRef(s string) (bool, *EntityDef)
- func (T *Factory) LoadEntity(Ref string) (*Entity, error)
- func (T *Factory) LoadEntityWrapped(Ref string) (any, error)
- func (T *Factory) NewRef(def *EntityDef) string
- func (f *Factory) PrepareSql(query string, args ...any) string
- func (f *Factory) Query(query string, args ...any) (*sql.Rows, error)
- func (f *Factory) RollbackTran(tx *sql.Tx) error
- func (T *Factory) SetDataVersionCheckMode(mode int) error
- type FieldDef
- type FieldValueBool
- func (T *FieldValueBool) AsString() string
- func (T *FieldValueBool) Def() *FieldDef
- func (T *FieldValueBool) Entity() *Entity
- func (T *FieldValueBool) Get() bool
- func (T *FieldValueBool) Old() bool
- func (T *FieldValueBool) Scan(v any) error
- func (T *FieldValueBool) Set(newValue bool)
- func (T *FieldValueBool) SqlStringValue(v ...any) (string, error)
- type FieldValueDateTime
- func (T *FieldValueDateTime) AsString() string
- func (T *FieldValueDateTime) Def() *FieldDef
- func (T *FieldValueDateTime) Entity() *Entity
- func (T *FieldValueDateTime) Get() time.Time
- func (T *FieldValueDateTime) Old() time.Time
- func (T *FieldValueDateTime) Scan(v any) error
- func (T *FieldValueDateTime) Set(newValue time.Time)
- func (T *FieldValueDateTime) SqlStringValue(v ...any) (string, error)
- type FieldValueInt
- func (T *FieldValueInt) AsString() string
- func (T *FieldValueInt) Def() *FieldDef
- func (T *FieldValueInt) Entity() *Entity
- func (T *FieldValueInt) Get() int64
- func (T *FieldValueInt) Old() int64
- func (T *FieldValueInt) Scan(v any) error
- func (T *FieldValueInt) Set(newValue int64)
- func (T *FieldValueInt) SqlStringValue(v ...any) (string, error)
- type FieldValueNumeric
- func (T *FieldValueNumeric) AsString() string
- func (T *FieldValueNumeric) Def() *FieldDef
- func (T *FieldValueNumeric) Entity() *Entity
- func (T *FieldValueNumeric) Get() float64
- func (T *FieldValueNumeric) Old() float64
- func (T *FieldValueNumeric) Scan(v any) error
- func (T *FieldValueNumeric) Set(newValue float64)
- func (T *FieldValueNumeric) SqlStringValue(v ...any) (string, error)
- type FieldValueRef
- func (T *FieldValueRef) AsString() string
- func (T *FieldValueRef) Def() *FieldDef
- func (T *FieldValueRef) Entity() *Entity
- func (T *FieldValueRef) Get() (any, error)
- func (T *FieldValueRef) Old() (any, error)
- func (T *FieldValueRef) Scan(v any) error
- func (T *FieldValueRef) Set(newValue any) error
- func (T *FieldValueRef) SetFactory(newValue *Factory)
- func (T *FieldValueRef) SqlStringValue(v ...any) (string, error)
- type FieldValueString
- func (T *FieldValueString) AsString() string
- func (T *FieldValueString) Def() *FieldDef
- func (T *FieldValueString) Entity() *Entity
- func (T *FieldValueString) Get() string
- func (T *FieldValueString) Old() string
- func (T *FieldValueString) Scan(v any) error
- func (T *FieldValueString) Set(newValue string)
- func (T *FieldValueString) SqlStringValue(v ...any) (string, error)
- type Filter
- func AddAndGroup(childs ...*Filter) *Filter
- func AddFilterEQ(leftField *FieldDef, rightValue any) *Filter
- func AddFilterGE(leftField *FieldDef, rightValue any) *Filter
- func AddFilterGT(leftField *FieldDef, rightValue any) *Filter
- func AddFilterIN(leftField *FieldDef, rightValues ...any) *Filter
- func AddFilterIsNOTNULL(leftField *FieldDef) *Filter
- func AddFilterIsNULL(leftField *FieldDef) *Filter
- func AddFilterLE(leftField *FieldDef, rightValue any) *Filter
- func AddFilterLIKE(leftField *FieldDef, rightValue string) *Filter
- func AddFilterLT(leftField *FieldDef, rightValue any) *Filter
- func AddFilterNOEQ(leftField *FieldDef, rightValue any) *Filter
- func AddFilterNOTIN(leftField *FieldDef, rightValues ...any) *Filter
- func AddOrGroup(childs ...*Filter) *Filter
- type IEntity
- type IFieldValue
- type IndexDef
- type RestApiConfig
- type SortItem
Constants ¶
const ( RefFieldName = "Ref" IsDeletedFieldName = "IsDeleted" DataVersionFieldName = "DataVersion" )
Predefined field names for common fields in entities.
const ( DbDialectPostgres = 100 DbDialectMSSQL = 200 DbDialectMySQL = 300 DbDialectSQLite = 400 )
Database dialect constants for supported database types.
const ( DataVersionCheckNever = -1 DataVersionCheckDefault = 0 DataVersionCheckAlways = 1 )
Data version check mode constants for controlling version checking behavior.
const ( FieldDefTypeString = 100 FieldDefTypeInt = 200 FieldDefTypeBool = 300 FieldDefTypeRef = 400 FieldDefTypeNumeric = 500 FieldDefTypeDateTime = 600 )
Supported field types
const ( FilterEQ = 50 FilterLIKE = 55 FilterNOEQ = 60 FilterGT = 70 FilterGE = 80 FilterLT = 90 FilterLE = 100 FilterIN = 110 FilterNOTIN = 120 FilterIsNULL = 130 FilterIsNOTNULL = 140 FilterAndGroup = 200 FilterOrGroup = 210 )
Filter operation constants for entity filtering in SelectEntities.
const DefaultPageSize = 20
DefaultPageSize is the default number of items per page in REST API responses.
Variables ¶
This section is empty.
Functions ¶
func HandleRestApi ¶
func HandleRestApi[T IEntity](config RestApiConfig[T]) func(w http.ResponseWriter, r *http.Request)
HandleRestApi handles HTTP requests for the REST API based on the provided configuration.
Types ¶
type Entity ¶
type Entity struct {
Factory *Factory
Values map[string]IFieldValue
// contains filtered or unexported fields
}
Entity represents a database entity in elorm.
func (*Entity) DataVersion ¶
DataVersion returns the current data version of this entity.
func (*Entity) GetValues ¶ added in v0.4.0
func (T *Entity) GetValues() map[string]IFieldValue
GetValues returns a map of all field values for this entity.
func (*Entity) MarshalJSON ¶
MarshalJSON implements json.Marshaler interface for JSON serialization.
func (*Entity) Save ¶
Save persists the Entity to the database. It handles both insert and update operations depending on whether the entity is new or existing. The method performs the following steps:
- Executes the BeforeSaveHandler if defined.
- Begins a database transaction.
- If the entity is new, inserts a new record into the database.
- If the entity exists, updates the corresponding record, optionally performing data version checks to prevent concurrent modifications.
- Commits the transaction if all operations succeed, or rolls back on error.
- Executes the AfterSaveHandler if defined.
Returns an error if any step fails, including handler execution, SQL operations, or transaction management.
func (*Entity) SetIsDeleted ¶
SetIsDeleted sets the deletion status of this entity.
func (*Entity) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler interface for JSON deserialization.
type EntityDef ¶
type EntityDef struct {
Factory *Factory // Owning factory, used to access database and other resources
DataVersionCheckMode int // DataVersionCheckNever, DataVersionCheckDefault, DataVersionCheckAlways
ObjectName string // name of the object, elorm-gen created strongly typed structs based on this name
TableName string // SQL table name, if empty, it will be generated from ObjectName
Fragments []string // fragments are used to define reusable parts of entity definitions
FieldDefs []*FieldDef // defined fields. All predefined fields (Ref, IsDeleted, DataVersion) are automatically added to this list.
IndexDefs []*IndexDef // defined indexes. PK doesn't need to be defined here, it is always created automatically
RefField *FieldDef // Primary Key, ID
IsDeletedField *FieldDef // field for soft delete
DataVersionField *FieldDef // field for data versioning
Wrap func(source *Entity) any // optional function to wrap the entity type into custom struct (used by elorm-gen)
AutoExpandFieldsForJSON map[*FieldDef]bool // if specified, these fields will be automatically expanded when serializing to JSON
// UseSoftDelete=true leads to:
// 1) SeletecEntities() includes IsDeleted=false filter always unless developer specified it explicitly
// 2) HandleRestApi on DELETE requests will set IsDeleted=true instead of deleting the entity
// Note: entity has IsDelete field always, developer can use always
//
// UseSoftDelete=false leads to:
// 1) Save() raises error if IsDeleted=true and UseSoftDelete=false
UseSoftDelete bool
// contains filtered or unexported fields
}
EntityDef describes the definition of an entity.
func (*EntityDef) ActualDataVersionCheckMode ¶ added in v0.6.0
ActualDataVersionCheckMode returns the effective data version check mode for this entity.
func (*EntityDef) AddBoolFieldDef ¶
AddBoolFieldDef adds a boolean field definition to this entity def.
func (*EntityDef) AddDateTimeFieldDef ¶
AddDateTimeFieldDef adds a datetime field definition to this entity def.
func (*EntityDef) AddIndex ¶ added in v0.3.0
AddIndex adds a new index to the entity definition. The index can be unique or non-unique, and is defined over one or more fields. Index will be created or updated automatically in scope of ensureDatabaseIndexes() method. Parameters:
- Unique: specifies whether the index should enforce uniqueness.
- fld: variadic list of FieldDef representing the fields to include in the index.
Returns:
- error: describing the reason for failure, or nil if the index was added successfully.
func (*EntityDef) AddIntFieldDef ¶
AddIntFieldDef adds an integer field definition to this entity def.
func (*EntityDef) AddNumericFieldDef ¶
AddNumericFieldDef adds a numeric field definition to this entity def.
func (*EntityDef) AddRefFieldDef ¶
AddRefFieldDef adds a reference field definition to this entity def.
func (*EntityDef) AddStringFieldDef ¶
AddStringFieldDef adds a string field definition to this entity def.
func (*EntityDef) FieldDefByName ¶
FieldDefByName returns the field definition with the specified name.
func (*EntityDef) SelectEntities ¶
func (T *EntityDef) SelectEntities(filters []*Filter, sorts []*SortItem, pageNo int, pageSize int) (result []*Entity, pagesCount int, err error)
SelectEntities retrieves entities from the database with filtering, sorting, and pagination.
func (*EntityDef) SqlTableName ¶
SqlTableName returns the SQL table name for this entity definition.
type EntityHandlerFunc ¶
EntityHandlerFunc is a function type for handling entities with context. Used for beforeSave, afterSave, beforeDelete handlers.
type EntityHandlerFuncByRef ¶ added in v0.2.0
EntityHandlerFuncByRef is a function type for handling entities by reference with context. Entity is not instantiated; only reference is provided.
type EntityHandlerFuncNoContext ¶ added in v0.4.0
EntityHandlerFuncNoContext is a function type for handling entities without context. Used for fillNewHandlers.
type Factory ¶
type Factory struct {
AggressiveReadingCache bool // It assumes each database has only one factory instance, so it can cache entities aggressively.
EntityDefs []*EntityDef
// contains filtered or unexported fields
}
Factory manages entities, keeps database connections, entities cache, and handles transactions.
func CreateFactory ¶
CreateFactory creates a new Factory instance with the specified database dialect and connection string.
func (*Factory) AddAfterSaveHandler ¶ added in v0.2.0
func (f *Factory) AddAfterSaveHandler(dest any, handler EntityHandlerFunc) error
AddAfterSaveHandler adds a handler that will be called after saving entities. dest
func (*Factory) AddBeforeDeleteHandler ¶ added in v0.2.0
func (f *Factory) AddBeforeDeleteHandler(dest any, handler EntityHandlerFunc) error
AddBeforeDeleteHandler adds a handler that will be called before deleting entities. dest can be an EntityDef pointer or a fragment name.
func (*Factory) AddBeforeDeleteHandlerByRef ¶ added in v0.2.0
func (f *Factory) AddBeforeDeleteHandlerByRef(dest any, handler EntityHandlerFuncByRef) error
AddBeforeDeleteHandlerByRef adds a handler that will be called before deleting entities by reference. dest
func (*Factory) AddBeforeSaveHandler ¶ added in v0.2.0
func (f *Factory) AddBeforeSaveHandler(dest any, handler EntityHandlerFunc) error
AddBeforeSaveHandler adds a handler that will be called before saving entities. dest can be an EntityDef pointer or a fragment name.
func (*Factory) AddBeforeSaveHandlerByRef ¶ added in v0.2.0
func (f *Factory) AddBeforeSaveHandlerByRef(dest any, handler EntityHandlerFuncByRef) error
AddBeforeSaveHandlerByRef adds a handler that will be called before saving entities by reference. dest can be an EntityDef pointer or a fragment name.
func (*Factory) AddFillNewHandler ¶ added in v0.2.0
func (f *Factory) AddFillNewHandler(dest any, handler EntityHandlerFuncNoContext) error
AddFillNewHandler adds a handler that will be called when creating new entities. dest can be an EntityDef pointer or a fragment name.
func (*Factory) BeginTran ¶
BeginTran begins a database transaction or increases the nested transaction level if already in a transaction.
func (*Factory) CommitTran ¶
CommitTran decreases transaction level and commits the transaction if it was the last one.
func (*Factory) CreateEntity ¶
CreateEntity creates a new entity instance with the given entity definition.
func (*Factory) CreateEntityDef ¶
CreateEntityDef creates a new entity definition with the specified object and table names.
func (*Factory) CreateEntityWrapped ¶
CreateEntityWrapped creates a new entity instance wrapped in a custom struct if defined.
func (*Factory) DeleteEntity ¶
func (*Factory) EnsureDBStructure ¶
func (*Factory) IsRef ¶
IsRef checks if the given string is a valid reference and returns the associated entity definition.
func (*Factory) LoadEntity ¶
LoadEntity loads an entity from factory cache or from the database by its reference string.
func (*Factory) LoadEntityWrapped ¶
LoadEntityWrapped loads an entity from the database and wraps it in a custom struct if defined.
func (*Factory) NewRef ¶
NewRef generates a new reference string for the given entity definition. It will include def ObjectName if provided.
func (*Factory) PrepareSql ¶ added in v0.9.2
PrepareSql prepares a SQL query by replacing Postgres-style parameters ($1, $2, ...) with MySQL/SQLite-style (?) if needed.
func (*Factory) Query ¶
Query is a wrapper for sql.DB.Query(). It always accepts parameters in Postgres style ($1, $2, ...) and converts it to MySQL/SQLite style (?) if needed. And it uses database connection or active transaction depending on the dialect and transaction level.
func (*Factory) RollbackTran ¶
RollbackTran rolls back a database transaction and zeroes the transaction level.
func (*Factory) SetDataVersionCheckMode ¶
SetDataVersionCheckMode sets default data version checking mode for this factory. It can be overridden by EntityDef level.
type FieldDef ¶
type FieldDef struct {
Name string
Type int
EntityDef *EntityDef // for ref fields, the entity definition this field navigate
Len int //for string
Precision int //for numeric
Scale int //for numeric
DateTimeJSONFormat string //for date time, e.g. "2006-01-02T15:04:05Z07:00"
}
FieldDef describes a field in an entity.
func (*FieldDef) CreateFieldValue ¶
func (T *FieldDef) CreateFieldValue(entity *Entity) (IFieldValue, error)
func (*FieldDef) SqlColumnName ¶
func (*FieldDef) SqlColumnType ¶
type FieldValueBool ¶
type FieldValueBool struct {
// contains filtered or unexported fields
}
FieldValueBool is the bool field value implementation.
func (*FieldValueBool) AsString ¶
func (T *FieldValueBool) AsString() string
func (*FieldValueBool) Get ¶
func (T *FieldValueBool) Get() bool
func (*FieldValueBool) Old ¶ added in v0.10.0
func (T *FieldValueBool) Old() bool
func (*FieldValueBool) Scan ¶
func (T *FieldValueBool) Scan(v any) error
func (*FieldValueBool) Set ¶
func (T *FieldValueBool) Set(newValue bool)
func (*FieldValueBool) SqlStringValue ¶
func (T *FieldValueBool) SqlStringValue(v ...any) (string, error)
type FieldValueDateTime ¶
type FieldValueDateTime struct {
// contains filtered or unexported fields
}
FieldValueDateTime is the datetime field value implementation that stores date and time values without timezone.
func (*FieldValueDateTime) AsString ¶
func (T *FieldValueDateTime) AsString() string
func (*FieldValueDateTime) Get ¶
func (T *FieldValueDateTime) Get() time.Time
func (*FieldValueDateTime) Old ¶ added in v0.10.0
func (T *FieldValueDateTime) Old() time.Time
func (*FieldValueDateTime) Scan ¶
func (T *FieldValueDateTime) Scan(v any) error
func (*FieldValueDateTime) Set ¶
func (T *FieldValueDateTime) Set(newValue time.Time)
func (*FieldValueDateTime) SqlStringValue ¶
func (T *FieldValueDateTime) SqlStringValue(v ...any) (string, error)
type FieldValueInt ¶
type FieldValueInt struct {
// contains filtered or unexported fields
}
FieldValueInt is the int64 field value implementation.
func (*FieldValueInt) AsString ¶
func (T *FieldValueInt) AsString() string
func (*FieldValueInt) Get ¶
func (T *FieldValueInt) Get() int64
func (*FieldValueInt) Old ¶ added in v0.10.0
func (T *FieldValueInt) Old() int64
func (*FieldValueInt) Scan ¶
func (T *FieldValueInt) Scan(v any) error
func (*FieldValueInt) Set ¶
func (T *FieldValueInt) Set(newValue int64)
func (*FieldValueInt) SqlStringValue ¶
func (T *FieldValueInt) SqlStringValue(v ...any) (string, error)
type FieldValueNumeric ¶
type FieldValueNumeric struct {
// contains filtered or unexported fields
}
FieldValueNumeric is the float64 field value implementation.
func (*FieldValueNumeric) AsString ¶
func (T *FieldValueNumeric) AsString() string
func (*FieldValueNumeric) Get ¶
func (T *FieldValueNumeric) Get() float64
func (*FieldValueNumeric) Old ¶ added in v0.10.0
func (T *FieldValueNumeric) Old() float64
func (*FieldValueNumeric) Scan ¶
func (T *FieldValueNumeric) Scan(v any) error
func (*FieldValueNumeric) Set ¶
func (T *FieldValueNumeric) Set(newValue float64)
func (*FieldValueNumeric) SqlStringValue ¶
func (T *FieldValueNumeric) SqlStringValue(v ...any) (string, error)
type FieldValueRef ¶
type FieldValueRef struct {
// contains filtered or unexported fields
}
FieldValueRef is the reference (navigation) field value implementation. It supports lazy loading of referenced entity.
func (*FieldValueRef) AsString ¶
func (T *FieldValueRef) AsString() string
func (*FieldValueRef) Get ¶
func (T *FieldValueRef) Get() (any, error)
func (*FieldValueRef) Old ¶ added in v0.10.0
func (T *FieldValueRef) Old() (any, error)
func (*FieldValueRef) Scan ¶
func (T *FieldValueRef) Scan(v any) error
func (*FieldValueRef) Set ¶
func (T *FieldValueRef) Set(newValue any) error
func (*FieldValueRef) SetFactory ¶
func (T *FieldValueRef) SetFactory(newValue *Factory)
func (*FieldValueRef) SqlStringValue ¶
func (T *FieldValueRef) SqlStringValue(v ...any) (string, error)
type FieldValueString ¶
type FieldValueString struct {
// contains filtered or unexported fields
}
FieldValueString is the string field value implementation.
func (*FieldValueString) AsString ¶
func (T *FieldValueString) AsString() string
func (*FieldValueString) Get ¶
func (T *FieldValueString) Get() string
func (*FieldValueString) Old ¶ added in v0.10.0
func (T *FieldValueString) Old() string
func (*FieldValueString) Scan ¶
func (T *FieldValueString) Scan(v any) error
func (*FieldValueString) Set ¶
func (T *FieldValueString) Set(newValue string)
func (*FieldValueString) SqlStringValue ¶
func (T *FieldValueString) SqlStringValue(v ...any) (string, error)
type Filter ¶
Filter represents a filter condition for entity selection (SelectEntities).
func AddAndGroup ¶
AddAndGroup creates a filter group with AND logic for combining multiple filters.
func AddFilterEQ ¶
AddFilterEQ creates a filter for equality comparison.
func AddFilterGE ¶
AddFilterGE creates a filter for greater than or equal comparison.
func AddFilterGT ¶
AddFilterGT creates a filter for greater than comparison.
func AddFilterIN ¶ added in v0.6.0
AddFilterIN creates a filter for IN comparison with multiple values.
func AddFilterIsNOTNULL ¶ added in v0.9.0
AddFilterIsNOTNULL creates a filter for NOT NULL value check.
func AddFilterIsNULL ¶ added in v0.6.0
AddFilterIsNULL creates a filter for NULL value check.
func AddFilterLE ¶
AddFilterLE creates a filter for less than or equal comparison.
func AddFilterLIKE ¶
AddFilterLIKE creates a filter for LIKE string comparison.
func AddFilterLT ¶
AddFilterLT creates a filter for less than comparison.
func AddFilterNOEQ ¶
AddFilterNOEQ creates a filter for inequality comparison.
func AddFilterNOTIN ¶ added in v0.6.0
AddFilterNOTIN creates a filter for NOT IN comparison with multiple values.
func AddOrGroup ¶
AddOrGroup creates a filter group with OR logic for combining multiple filters.
type IEntity ¶
type IEntity interface {
RefString() string
IsNew() bool
IsDeleted() bool
Save(ctx context.Context) error
LoadFrom(src IEntity, predefinedFields bool) error
GetValues() map[string]IFieldValue
Def() *EntityDef
}
IEntity is the interface for entities in elorm.
type IFieldValue ¶
type IFieldValue interface {
Def() *FieldDef
Entity() *Entity
SqlStringValue(v ...any) (string, error)
Scan(v any) error
AsString() string
// contains filtered or unexported methods
}
IFieldValue is the interface for field values in elorm.
type RestApiConfig ¶
type RestApiConfig[T IEntity] struct { // Entity Definition Def *EntityDef // Typically, it's LoadXXX from generated code, e.g. LoadShop LoadEntityFunc func(ref string) (T, error) // Typically, it's SelectEntities from generated code, e.g. DB.ShopDef.SelectEntities SelectEntitiesFunc func(filters []*Filter, sorts []*SortItem, pageNo int, pageSize int) (result []T, pagesCount int, err error) // Typically, it's CreateXXX from generated code, e.g. DB.CreateShop CreateEntityFunc func() (T, error) // Additional headers to include into the response AdditionalHeaders map[string]string // Automatically merge filters based on query parameters AutoFilters bool // Default number of items per page in REST API responses DefaultPageSize int // Enable GET request for a single entity EnableGetOne bool // Enable GET request for a list of entities EnableGetList bool // Enable POST request to create a new entity EnablePost bool // Enable PUT request to update an existing entity EnablePut bool // Enable DELETE request to remove an entity EnableDelete bool // Query parameter name for entity reference, "ref" by default ParamRef string // Query parameter name for page number, "pageno" by default ParamPageNo string // Query parameter name for page size, "pagesize" by default ParamPageSize string // Query parameter name for sorting, "sortby" by default ParamSortBy string // Middleware function to execute before processing the request, returns true to continue or false to stop BeforeMiddleware func(http.ResponseWriter, *http.Request) bool // Function to get context from the request, can be used to pass additional values to event handlers Context func(r *http.Request) context.Context // Function to get additional filters based on the request, result should be merged with user-defined filters AdditionalFilter func(r *http.Request) ([]*Filter, error) // Function to get default sorting options based on the request, result is used when we have no user-defined sorts. Should be merged with user-defined sorts DefaultSorts func(r *http.Request) ([]*SortItem, error) }
RestApiConfig is a configuration for standard REST API operations.
func CreateStdRestApiConfig ¶
func CreateStdRestApiConfig[T IEntity]( def *EntityDef, loadEntityFunc func(ref string) (T, error), selectEntitiesFunc func(filters []*Filter, sorts []*SortItem, pageNo int, pageSize int) (result []T, pages int, err error), createEntityFunc func() (T, error)) RestApiConfig[T]
CreateStdRestApiConfig creates a new RestApiConfig for standard REST API operations.