orm

package
v1.27.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 19, 2026 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
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

func IsInfiniteTTL(ttl time.Duration) bool

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 JoinType

type JoinType string

JoinType represents the type of SQL join

const (
	InnerJoin JoinType = "INNER JOIN"
	LeftJoin  JoinType = "LEFT JOIN"
	RightJoin JoinType = "RIGHT JOIN"
	FullJoin  JoinType = "FULL JOIN"
)

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

func (*MemoryCache) Get added in v1.12.105

func (m *MemoryCache) Get(key string) ([]byte, bool)

Get retrieves an item from the memory cache

func (*MemoryCache) Set added in v1.12.105

func (m *MemoryCache) Set(key string, data []byte, ttl time.Duration)

Set stores an item in the memory cache with the specified TTL. If ttl is InfiniteTTL or negative, the item will never expire automatically.

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 Load

func Load(entity any, opts ...Options) *ORM

Load initializes the ORM with the given struct.

func (*ORM) Begin

func (o *ORM) Begin() (*Transaction, error)

Begin starts a new database transaction

func (*ORM) BeginTx

func (o *ORM) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Transaction, error)

BeginTx starts a new database transaction with options

func (*ORM) ClearCache added in v1.12.105

func (o *ORM) ClearCache(clearRedis bool) error

ClearCache removes all items from the memory cache and optionally Redis

func (*ORM) Close added in v1.11.95

func (o *ORM) Close()

func (*ORM) DeleteAll

func (o *ORM) DeleteAll() (int64, error)

func (*ORM) DeleteByFieldCompare

func (o *ORM) DeleteByFieldCompare(fieldName string, value any, operator string) (int64, error)

func (*ORM) DeleteByFieldEquals

func (o *ORM) DeleteByFieldEquals(fieldName string, value any) (int64, error)

func (*ORM) DeleteByFieldIn

func (o *ORM) DeleteByFieldIn(fieldName string, values []any) (int64, error)

func (*ORM) DeleteByPrimaryKey

func (o *ORM) DeleteByPrimaryKey(pkValue any) (int64, error)

func (*ORM) ExecuteRaw

func (o *ORM) ExecuteRaw(query string, args ...any) (sql.Result, error)

Add ExecuteRaw for non-query operations (INSERT, UPDATE, DELETE)

func (*ORM) FullJoin

func (o *ORM) FullJoin(table string, on string) *JoinBuilder

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) GetCount

func (o *ORM) GetCount(filters map[string]any) (int, error)

GetCount returns the count of records matching the given condition

func (*ORM) GetTotalRowCount added in v1.12.100

func (o *ORM) GetTotalRowCount() (int, error)

GetTotalRowCount returns the total number of rows in the table

func (*ORM) HasInfiniteTTL added in v1.12.111

func (o *ORM) HasInfiniteTTL() bool

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) Insert

func (o *ORM) Insert(entity any) error

Insert inserts a new row into the table.

func (*ORM) InvalidateCache added in v1.12.105

func (o *ORM) InvalidateCache(query string, args ...any) error

Invalidate removes an item from both memory and Redis caches

func (*ORM) InvalidateCacheByPrefix added in v1.12.110

func (o *ORM) InvalidateCacheByPrefix(prefix string) error

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) LeftJoin

func (o *ORM) LeftJoin(table string, on string) *JoinBuilder

func (*ORM) OrderBy

func (o *ORM) OrderBy(fieldName string, direction OrderDirection) *QueryResult

OrderBy orders the results

func (*ORM) QueryRaw

func (o *ORM) QueryRaw(query string, args ...any) *QueryResult

func (*ORM) RightJoin

func (o *ORM) RightJoin(table string, on string) *JoinBuilder

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

func (*ORM) Update

func (o *ORM) Update(entity any, primaryKeyValue string) error

Update updates an existing row in the table.

func (*ORM) WithTransaction

func (o *ORM) WithTransaction(fn func(*ORM) error) error

Add a helper function for transaction execution with automatic rollback on error

type Options added in v1.12.104

type Options func(*ORM)

func WithCacheKey added in v1.12.104

func WithCacheKey(cacheKey string) Options

func WithCacheMethod added in v1.12.104

func WithCacheMethod(cacheMethod string) Options

func WithCacheOn added in v1.12.104

func WithCacheOn(cacheOn bool) Options

func WithCacheTTL added in v1.12.104

func WithCacheTTL(cacheTTL time.Duration) Options

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

func WithDB(db *sqlx.DB) Options

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 WithDatabasePrefix(prefix string) Options

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

func WithRedisClient(redisClient *redis.Client) Options

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) Commit

func (t *Transaction) Commit() error

Commit commits the 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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址