Documentation
¶
Index ¶
- Constants
- func CloseAllPools()
- func IsInfiniteTTL(ttl time.Duration) bool
- type JoinBuilder
- func (jb *JoinBuilder) AddJoin(joinType JoinType, table string, on string) *JoinBuilder
- func (jb *JoinBuilder) Execute() *QueryResult
- func (jb *JoinBuilder) Limit(limit int) *JoinBuilder
- func (jb *JoinBuilder) Offset(offset int) *JoinBuilder
- func (jb *JoinBuilder) OrderBy(orderBy string) *JoinBuilder
- func (jb *JoinBuilder) Select(columns ...string) *JoinBuilder
- func (jb *JoinBuilder) Where(condition string, args ...any) *JoinBuilder
- type JoinType
- type MemoryCache
- type ORM
- func (o *ORM) Begin() (*Transaction, error)
- func (o *ORM) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Transaction, error)
- func (o *ORM) ClearCache(clearRedis bool) error
- func (o *ORM) Close()
- func (o *ORM) DeleteAll() (int64, error)
- func (o *ORM) DeleteByFieldCompare(fieldName string, value any, operator string) (int64, error)
- func (o *ORM) DeleteByFieldEquals(fieldName string, value any) (int64, error)
- func (o *ORM) DeleteByFieldIn(fieldName string, values []any) (int64, error)
- func (o *ORM) DeleteByPrimaryKey(pkValue any) (int64, error)
- func (o *ORM) ExecuteRaw(query string, args ...any) (sql.Result, error)
- func (o *ORM) FullJoin(table string, on string) *JoinBuilder
- func (o *ORM) GetAll() *QueryResult
- func (o *ORM) GetByFieldBetween(fieldName string, start any, end any) *QueryResult
- func (o *ORM) GetByFieldCompare(fieldName string, operator string, value any) *QueryResult
- func (o *ORM) GetByFieldEquals(fieldName string, value any) *QueryResult
- func (o *ORM) GetByFieldGreaterThan(fieldName string, value any) *QueryResult
- func (o *ORM) GetByFieldGreaterThanEquals(fieldName string, value any) *QueryResult
- func (o *ORM) GetByFieldIn(fieldName string, values ...any) *QueryResult
- func (o *ORM) GetByFieldIsNotNull(fieldName string) *QueryResult
- func (o *ORM) GetByFieldIsNull(fieldName string) *QueryResult
- func (o *ORM) GetByFieldLessThan(fieldName string, value any) *QueryResult
- func (o *ORM) GetByFieldLessThanEquals(fieldName string, value any) *QueryResult
- func (o *ORM) GetByFieldLike(fieldName string, pattern string) *QueryResult
- func (o *ORM) GetByFieldNotEquals(fieldName string, value any) *QueryResult
- func (o *ORM) GetByFieldsEquals(fieldValueMap map[string]any) *QueryResult
- func (o *ORM) GetByPrimaryKey(value any) *QueryResult
- func (o *ORM) GetCount(filters map[string]any) (int, error)
- func (o *ORM) GetTotalRowCount() (int, error)
- func (o *ORM) HasInfiniteTTL() bool
- func (o *ORM) InnerJoin(table string, on string) *JoinBuilder
- func (o *ORM) Insert(entity any) error
- func (o *ORM) InvalidateCache(query string, args ...any) error
- func (o *ORM) InvalidateCacheByPrefix(prefix string) error
- func (o *ORM) Join(joinType JoinType, table string, on string) *JoinBuilder
- func (o *ORM) JoinOnFields(joinType JoinType, table string, baseField string, joinField string) *JoinBuilder
- func (o *ORM) LeftJoin(table string, on string) *JoinBuilder
- func (o *ORM) OrderBy(fieldName string, direction OrderDirection) *QueryResult
- func (o *ORM) QueryRaw(query string, args ...any) *QueryResult
- func (o *ORM) RightJoin(table string, on string) *JoinBuilder
- func (o *ORM) SimpleJoin(joinType JoinType, table string, field string) *JoinBuilder
- func (o *ORM) Update(entity any, primaryKeyValue string) error
- func (o *ORM) WithTransaction(fn func(*ORM) error) error
- type Options
- func WithCacheKey(cacheKey string) Options
- func WithCacheMethod(cacheMethod string) Options
- func WithCacheOn(cacheOn bool) Options
- func WithCacheTTL(cacheTTL time.Duration) Options
- func WithDB(db *sqlx.DB) Options
- func WithDatabaseOptions(options database.PostgresConnOptions) Options
- func WithDatabasePrefix(prefix string) Options
- func WithInfiniteCacheTTL() Options
- func WithRedisClient(redisClient *redis.Client) Options
- type OrderDirection
- type QueryResult
- type Transaction
Constants ¶
const (
InfiniteTTL = time.Duration(-1)
)
Variables ¶
This section is empty.
Functions ¶
func CloseAllPools ¶ added in v1.16.68
func CloseAllPools()
CloseAllPools tears down every connection pool and the shared Redis client in the global registries. Call this once during graceful application shutdown.
func IsInfiniteTTL ¶ added in v1.12.111
IsInfiniteTTL checks if the given duration represents an infinite TTL
Types ¶
type JoinBuilder ¶
type JoinBuilder struct {
// contains filtered or unexported fields
}
JoinBuilder helps construct and execute join queries
func (*JoinBuilder) AddJoin ¶
func (jb *JoinBuilder) AddJoin(joinType JoinType, table string, on string) *JoinBuilder
AddJoin adds another join to the existing join query
func (*JoinBuilder) Execute ¶
func (jb *JoinBuilder) Execute() *QueryResult
Execute builds and executes the final join query
func (*JoinBuilder) Limit ¶
func (jb *JoinBuilder) Limit(limit int) *JoinBuilder
Limit adds a LIMIT clause to the join query
func (*JoinBuilder) Offset ¶
func (jb *JoinBuilder) Offset(offset int) *JoinBuilder
Offset adds an OFFSET clause to the join query
func (*JoinBuilder) OrderBy ¶
func (jb *JoinBuilder) OrderBy(orderBy string) *JoinBuilder
OrderBy adds an ORDER BY clause to the join query
func (*JoinBuilder) Select ¶
func (jb *JoinBuilder) Select(columns ...string) *JoinBuilder
Select specifies the columns to select
func (*JoinBuilder) Where ¶
func (jb *JoinBuilder) Where(condition string, args ...any) *JoinBuilder
Where adds a WHERE clause to the join query
type MemoryCache ¶ added in v1.12.105
type MemoryCache struct {
// contains filtered or unexported fields
}
MemoryCache provides in-memory caching capabilities
func (*MemoryCache) Delete ¶ added in v1.12.105
func (m *MemoryCache) Delete(key string)
Delete removes an item from the memory cache
type ORM ¶
type ORM struct {
CacheOn *bool
CacheMethod *string // "redis", "memory", or "both"
CacheKey *string
CacheTTL *time.Duration
RedisClient *redis.Client
// contains filtered or unexported fields
}
ORM struct encapsulates the metadata and methods for a table.
func (*ORM) Begin ¶
func (o *ORM) Begin() (*Transaction, error)
Begin starts a new database transaction
func (*ORM) ClearCache ¶ added in v1.12.105
ClearCache removes all items from the memory cache and optionally Redis
func (*ORM) DeleteByFieldCompare ¶
func (*ORM) DeleteByFieldEquals ¶
func (*ORM) DeleteByFieldIn ¶
func (*ORM) ExecuteRaw ¶
Add ExecuteRaw for non-query operations (INSERT, UPDATE, DELETE)
func (*ORM) GetAll ¶
func (o *ORM) GetAll() *QueryResult
func (*ORM) GetByFieldBetween ¶
func (o *ORM) GetByFieldBetween(fieldName string, start any, end any) *QueryResult
GetByFieldBetween returns records where the field is between two values
func (*ORM) GetByFieldCompare ¶
func (o *ORM) GetByFieldCompare(fieldName string, operator string, value any) *QueryResult
GetByFieldCompare returns records using a custom comparison operator
func (*ORM) GetByFieldEquals ¶
func (o *ORM) GetByFieldEquals(fieldName string, value any) *QueryResult
GetByFieldEquals returns records where the field equals the value
func (*ORM) GetByFieldGreaterThan ¶
func (o *ORM) GetByFieldGreaterThan(fieldName string, value any) *QueryResult
GetByFieldGreaterThan returns records where the field is greater than the value
func (*ORM) GetByFieldGreaterThanEquals ¶
func (o *ORM) GetByFieldGreaterThanEquals(fieldName string, value any) *QueryResult
GetByFieldGreaterThanEquals returns records where the field is greater than or equal to the value
func (*ORM) GetByFieldIn ¶
func (o *ORM) GetByFieldIn(fieldName string, values ...any) *QueryResult
GetByFieldIn to return a QueryResult for chaining
func (*ORM) GetByFieldIsNotNull ¶
func (o *ORM) GetByFieldIsNotNull(fieldName string) *QueryResult
GetByFieldIsNotNull returns records where the field is NOT NULL
func (*ORM) GetByFieldIsNull ¶
func (o *ORM) GetByFieldIsNull(fieldName string) *QueryResult
GetByFieldIsNull returns records where the field is NULL
func (*ORM) GetByFieldLessThan ¶
func (o *ORM) GetByFieldLessThan(fieldName string, value any) *QueryResult
GetByFieldLessThan returns records where the field is less than the value
func (*ORM) GetByFieldLessThanEquals ¶
func (o *ORM) GetByFieldLessThanEquals(fieldName string, value any) *QueryResult
GetByFieldLessThanEquals returns records where the field is less than or equal to the value
func (*ORM) GetByFieldLike ¶
func (o *ORM) GetByFieldLike(fieldName string, pattern string) *QueryResult
GetByFieldLike returns records where the field matches the LIKE pattern
func (*ORM) GetByFieldNotEquals ¶
func (o *ORM) GetByFieldNotEquals(fieldName string, value any) *QueryResult
GetByFieldNotEquals returns records where the field does not equal the value
func (*ORM) GetByFieldsEquals ¶
func (o *ORM) GetByFieldsEquals(fieldValueMap map[string]any) *QueryResult
GetByFieldsEquals returns records matching multiple field equality conditions
func (*ORM) GetByPrimaryKey ¶
func (o *ORM) GetByPrimaryKey(value any) *QueryResult
func (*ORM) GetTotalRowCount ¶ added in v1.12.100
GetTotalRowCount returns the total number of rows in the table
func (*ORM) HasInfiniteTTL ¶ added in v1.12.111
HasInfiniteTTL returns true if the ORM is configured to use infinite cache TTL
func (*ORM) InnerJoin ¶
func (o *ORM) InnerJoin(table string, on string) *JoinBuilder
Common shortcuts for different join types
func (*ORM) InvalidateCache ¶ added in v1.12.105
Invalidate removes an item from both memory and Redis caches
func (*ORM) InvalidateCacheByPrefix ¶ added in v1.12.110
func (*ORM) Join ¶
func (o *ORM) Join(joinType JoinType, table string, on string) *JoinBuilder
Join creates a join query with another table
func (*ORM) JoinOnFields ¶
func (o *ORM) JoinOnFields(joinType JoinType, table string, baseField string, joinField string) *JoinBuilder
JoinOnFields provides a convenient way to join tables when field names differ
func (*ORM) OrderBy ¶
func (o *ORM) OrderBy(fieldName string, direction OrderDirection) *QueryResult
OrderBy orders the results
func (*ORM) SimpleJoin ¶
func (o *ORM) SimpleJoin(joinType JoinType, table string, field string) *JoinBuilder
SimpleJoin provides a simpler syntax for the most common join case: joining two tables with matching field names
type Options ¶ added in v1.12.104
type Options func(*ORM)
func WithCacheKey ¶ added in v1.12.104
func WithCacheMethod ¶ added in v1.12.104
func WithCacheOn ¶ added in v1.12.104
func WithCacheTTL ¶ added in v1.12.104
WithCacheTTL sets the cache time-to-live duration for cached query results. Use InfiniteTTL or any negative duration for items that never expire.
func WithDB ¶ added in v1.16.68
WithDB lets you inject a pre-existing *sqlx.DB connection pool. The ORM will use this pool directly and will NOT close it when ORM.Close() is called — you are responsible for its lifecycle.
Example (create pool once at startup, share everywhere):
db, _ := database.PostgresConn(dbConfig)
userORM := orm.Load(&models.Users{}, orm.WithDB(db))
func WithDatabaseOptions ¶ added in v1.16.65
func WithDatabaseOptions(options database.PostgresConnOptions) Options
func WithDatabasePrefix ¶ added in v1.16.28
func WithInfiniteCacheTTL ¶ added in v1.12.111
func WithInfiniteCacheTTL() Options
WithInfiniteCacheTTL configures the ORM to cache query results indefinitely. Cached items will never expire automatically and must be manually invalidated using InvalidateCache, InvalidateCacheByPrefix, or ClearCache methods.
func WithRedisClient ¶ added in v1.12.104
type OrderDirection ¶
type OrderDirection string
const ( OrderAsc OrderDirection = "ASC" OrderDesc OrderDirection = "DESC" )
type QueryResult ¶
type QueryResult struct {
// contains filtered or unexported fields
}
func (*QueryResult) GetArgs ¶
func (qr *QueryResult) GetArgs() []any
GetArgs returns the arguments used in the query
func (*QueryResult) GetQuery ¶
func (qr *QueryResult) GetQuery() string
GetQuery returns the SQL query that produced this result
func (*QueryResult) Limit ¶
func (qr *QueryResult) Limit(limit int) *QueryResult
Limit limits the number of results returned
func (*QueryResult) Scan ¶
func (qr *QueryResult) Scan(dest any) error
Scan maps the query result to the provided destination pointer
type Transaction ¶
type Transaction struct {
// contains filtered or unexported fields
}
Transaction represents a database transaction
func (*Transaction) ORM ¶
func (t *Transaction) ORM() *ORM
ORM returns the ORM instance associated with this transaction
func (*Transaction) Rollback ¶
func (t *Transaction) Rollback() error
Rollback rolls back the transaction