data

package
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2026 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PostgresScheme = "postgres"
	MysqlScheme    = "mysql://"
	MemScheme      = "mem://"
	RedisScheme    = "redis://"
	NatsScheme     = "nats://"
)

Constants for database drivers.

Variables

This section is empty.

Functions

func ErrorConvertToAPI

func ErrorConvertToAPI(err error) *connect.Error

ErrorConvertToAPI converts GORM errors to appropriate Connect RPC errors efficiently.

func ErrorIsDuplicateKey

func ErrorIsDuplicateKey(err error) bool

ErrorIsDuplicateKey validate if supplied error is because of duplicate key in DB.

func ErrorIsNoRows

func ErrorIsNoRows(err error) bool

ErrorIsNoRows validate if supplied error is because of record missing in DB.

func SearchFunc added in v2.0.2

func SearchFunc[T any](
	ctx context.Context,
	dbConnection *gorm.DB,
	query *SearchQuery,
	validateColumn func(string) error,
) ([]T, error)

SearchFunc performs a complex search with pagination and filtering. It accepts a validation function to check column names, allowing flexible validation strategies.

func StableSearch

func StableSearch[T any](
	ctx context.Context, workMan workerpool.Manager,
	query *SearchQuery, searchFunc func(ctx context.Context, query *SearchQuery) ([]T, error),
) (workerpool.JobResultPipe[[]T], error)

StableSearch executes the provided `searchFunc` until the paginator reports that all results have been consumed, streaming batches through the workerpool. It is safe to call from multiple goroutines; the provided `workerpool.Manager` coordinates execution and buffering of results.

Types

type BaseModel

type BaseModel struct {
	ID string `gorm:"type:varchar(50);primary_key"`
	// CreatedAt and ModifiedAt are NOT NULL because BeforeCreate always
	// populates them through GORM. The default CURRENT_TIMESTAMP keeps
	// raw SQL seed INSERTs that omit these columns working, and keeps
	// AutoMigrate from trying to relax the columns on tables that promote
	// CreatedAt into a composite primary key (e.g. TimescaleDB hypertables).
	CreatedAt   time.Time      `gorm:"not null;default:CURRENT_TIMESTAMP"`
	ModifiedAt  time.Time      `gorm:"not null;default:CURRENT_TIMESTAMP"`
	CreatedBy   string         `gorm:"type:varchar(50)"`
	ModifiedBy  string         `gorm:"type:varchar(50)"`
	Version     uint           `gorm:"DEFAULT 0"`
	TenantID    string         `gorm:"type:varchar(50);index:,composite:base_tenancy"`
	PartitionID string         `gorm:"type:varchar(50);index:,composite:base_tenancy"`
	AccessID    string         `gorm:"type:varchar(50);index:,composite:base_tenancy"`
	DeletedAt   gorm.DeletedAt `sql:"index"`
}

BaseModel base table struct to be extended by other models.

func (*BaseModel) BeforeCreate

func (model *BaseModel) BeforeCreate(db *gorm.DB) error

func (*BaseModel) BeforeSave

func (model *BaseModel) BeforeSave(db *gorm.DB) error

BeforeSave Ensures we update a migrations time stamps.

func (*BaseModel) BeforeUpdate

func (model *BaseModel) BeforeUpdate(db *gorm.DB) error

BeforeUpdate Updates time stamp every time we update status of a migration.

func (*BaseModel) CopyPartitionInfo

func (model *BaseModel) CopyPartitionInfo(parent *BaseModel)

func (*BaseModel) GenID

func (model *BaseModel) GenID(ctx context.Context)

GenID creates a new id for model if its not existent.

func (*BaseModel) GetAccessID

func (model *BaseModel) GetAccessID() string

func (*BaseModel) GetID

func (model *BaseModel) GetID() string

func (*BaseModel) GetPartitionID

func (model *BaseModel) GetPartitionID() string

func (*BaseModel) GetTenantID

func (model *BaseModel) GetTenantID() string

func (*BaseModel) GetVersion

func (model *BaseModel) GetVersion() uint

func (*BaseModel) SetAccessID

func (model *BaseModel) SetAccessID(v string)

SetAccessID assigns the access identifier. Satisfies tenancy.Tenanted.

func (*BaseModel) SetPartitionID

func (model *BaseModel) SetPartitionID(v string)

SetPartitionID assigns the partition identifier. Satisfies tenancy.Tenanted.

func (*BaseModel) SetTenantID

func (model *BaseModel) SetTenantID(v string)

SetTenantID assigns the tenant identifier. Satisfies tenancy.Tenanted.

func (*BaseModel) ValidXID

func (model *BaseModel) ValidXID(id string) bool

ValidXID Validates that the supplied string is an xid.

type BaseModelI

type BaseModelI interface {
	GetID() string
	GetVersion() uint
}

type DSN

type DSN string

A DSN for conveniently handling a URI connection string.

func (DSN) ChangePort

func (d DSN) ChangePort(newPort string) (DSN, error)

func (DSN) DelPath

func (d DSN) DelPath() DSN

func (DSN) ExtendPath

func (d DSN) ExtendPath(epath ...string) DSN

func (DSN) ExtendQuery

func (d DSN) ExtendQuery(key, value string) DSN

func (DSN) GetQuery

func (d DSN) GetQuery(key string) string

func (DSN) IsCache

func (d DSN) IsCache() bool

func (DSN) IsDB

func (d DSN) IsDB() bool

func (DSN) IsMem

func (d DSN) IsMem() bool

func (DSN) IsMySQL

func (d DSN) IsMySQL() bool

func (DSN) IsNats

func (d DSN) IsNats() bool

func (DSN) IsPostgres

func (d DSN) IsPostgres() bool

func (DSN) IsQueue

func (d DSN) IsQueue() bool

func (DSN) IsRedis

func (d DSN) IsRedis() bool

func (DSN) PrefixPath

func (d DSN) PrefixPath(prefix string) DSN

func (DSN) RemoveQuery

func (d DSN) RemoveQuery(key ...string) DSN

func (DSN) String

func (d DSN) String() string

func (DSN) SuffixPath

func (d DSN) SuffixPath(suffix string) DSN

func (DSN) ToArray

func (d DSN) ToArray() []DSN

func (DSN) ToURI

func (d DSN) ToURI() (*url.URL, error)

func (DSN) Valid

func (d DSN) Valid() bool

func (DSN) WithPassword

func (d DSN) WithPassword(password string) (DSN, error)

func (DSN) WithPath

func (d DSN) WithPath(path string) (DSN, error)

func (DSN) WithPathSuffix

func (d DSN) WithPathSuffix(suffix string) (DSN, error)

func (DSN) WithQuery

func (d DSN) WithQuery(query string) (DSN, error)

func (DSN) WithUser

func (d DSN) WithUser(user string) (DSN, error)

func (DSN) WithUserAndPassword

func (d DSN) WithUserAndPassword(userName, password string) (DSN, error)

type JSONMap

type JSONMap map[string]any

JSONMap is a GORM-compatible map[string]any that stores JSONB/JSON in a DB.

func (*JSONMap) Copy

func (m *JSONMap) Copy() JSONMap

Copy returns a deep copy of the JSONMap. Nested maps and slices are recursively copied so that the returned JSONMap can be modified without affecting the original.

func (*JSONMap) FromProtoStruct

func (m *JSONMap) FromProtoStruct(s *structpb.Struct) JSONMap

FromProtoStruct populates the JSONMap with data from a protocol buffer Struct. If the receiver is nil, a new JSONMap will be created and returned. If the input struct is nil, the receiver is returned unchanged. Returns the receiver (or a new JSONMap if receiver was nil) for method chaining.

func (*JSONMap) GetFloat

func (m *JSONMap) GetFloat(key string) float64

GetFloat retrieves a string value from the JSONMap by key. It returns the string and a boolean indicating if the value was found and is a string.

func (*JSONMap) GetString

func (m *JSONMap) GetString(key string) string

GetString retrieves a string value from the JSONMap by key. It returns the string and a boolean indicating if the value was found and is a string.

func (*JSONMap) GormDBDataType

func (m *JSONMap) GormDBDataType(db *gorm.DB, _ *schema.Field) string

GormDBDataType returns the dialect-specific database column type.

func (*JSONMap) GormDataType

func (m *JSONMap) GormDataType() string

GormDataType returns the common GORM data type.

func (*JSONMap) GormValue

func (m *JSONMap) GormValue(_ context.Context, db *gorm.DB) clause.Expr

GormValue optimizes how values are rendered in SQL for specific dialects.

func (*JSONMap) MarshalJSON

func (m *JSONMap) MarshalJSON() ([]byte, error)

MarshalJSON customizes the JSON encoding.

func (*JSONMap) Scan

func (m *JSONMap) Scan(value any) error

Scan implements the sql.Scanner interface for database deserialization.

func (*JSONMap) ToProtoStruct

func (m *JSONMap) ToProtoStruct() *structpb.Struct

ToProtoStruct converts a JSONMap into a structpb.Struct safely and efficiently.

func (*JSONMap) UnmarshalJSON

func (m *JSONMap) UnmarshalJSON(data []byte) error

UnmarshalJSON deserializes JSON into the map.

func (*JSONMap) Update

func (m *JSONMap) Update(update JSONMap) JSONMap

Update merges all key-value pairs from update into the receiver. If the receiver is nil, a new JSONMap is created. Keys in update overwrite existing keys in the receiver.

func (*JSONMap) Value

func (m *JSONMap) Value() (driver.Value, error)

Value implements the driver.Valuer interface for database serialization.

type Paginator

type Paginator struct {
	Offset int
	Limit  int

	BatchSize int
}

Paginator tracks the current pagination state for a query. It keeps the desired `Limit` alongside the currently loaded `Offset` and the `BatchSize` used when streaming batched results through the worker pool.

func (*Paginator) CanLoad

func (p *Paginator) CanLoad() bool

CanLoad reports whether the paginator is still within the requested limit.

func (*Paginator) SetBatchSize

func (p *Paginator) SetBatchSize(batchSize int)

SetBatchSize overrides the streaming batch size. It is primarily used by tests to exercise edge cases.

func (*Paginator) Stop

func (p *Paginator) Stop(loadedCount int) bool

Stop updates the offset after a batch is processed and indicates whether the search should stop because fewer rows than requested were returned.

type SearchOption

type SearchOption func(*SearchQuery)

SearchOption mutates a `SearchQuery` during construction. Options are provided to `NewSearchQuery()` to override pagination defaults, attach filters, or scope execution to a time period.

func WithSearchBatchSize

func WithSearchBatchSize(batchSize int) SearchOption

WithSearchBatchSize allows callers to request a specific streaming batch size. Values larger than `defaultBatchSize` are capped when the query is constructed.

func WithSearchByTimePeriod

func WithSearchByTimePeriod(period *TimePeriod) SearchOption

WithSearchByTimePeriod constrains the search to the provided date range.

func WithSearchFiltersAndByValue

func WithSearchFiltersAndByValue(filters map[string]any) SearchOption

WithSearchFiltersAndByValue supplies AND filters whose values are compared for equality in repository implementations. The keys should have query selection e.g. `id LIKE ?`.

func WithSearchFiltersOrByValue

func WithSearchFiltersOrByValue(filters map[string]any) SearchOption

WithSearchFiltersOrByValue supplies OR filters whose values are map entries the keys should have query selection, e.g. `LIKE ?`.

func WithSearchLimit

func WithSearchLimit(limit int) SearchOption

WithSearchLimit sets the total number of rows the paginator should attempt to load before terminating.

func WithSearchOffset

func WithSearchOffset(offset int) SearchOption

WithSearchOffset configures the paginator to start from the provided page number. The actual row offset is derived inside `NewSearchQuery()`.

type SearchQuery

type SearchQuery struct {

	// We query with the value key is in the form key = ? and value whatever should be substituted
	FiltersOrByValue  map[string]any
	FiltersAndByValue map[string]any

	TimePeriod *TimePeriod
	Pagination *Paginator

	OrderBy string
}

SearchQuery captures the minimal information a repository needs to translate a high-level search request into storage-specific operations. The query is constructed via functional options and is treated as immutable after creation so that the worker-pool driven search pipeline can safely share it between goroutines.

Fields:

  • `Query` holds the raw text or identifier provided by the caller. Repository implementations are free to interpret it (for example, applying full-text search) but should avoid mutating it.
  • `FiltersOrByQuery` stores OR filters whose values indicate how the `Query` should be interpolated into storage-level predicates (e.g. `LIKE ?`).
  • `FiltersAndByValue` stores exact-match filters that are combined using AND semantics.
  • `TimePeriod` optionally restricts results to a date range on a specific column.
  • `Pagination` tracks the intended result window and streaming batch size.

func NewSearchQuery

func NewSearchQuery(opts ...SearchOption) *SearchQuery

NewSearchQuery builds a `SearchQuery` with sensible pagination defaults and applies the provided options. The paginator stores offsets in terms of page numbers; once options are applied we convert the supplied offset into the absolute row offset expected by downstream code.

type TimePeriod

type TimePeriod struct {
	Field     string
	StartDate *time.Time
	StopDate  *time.Time
}

TimePeriod is an inclusive date range constraint that can be applied to a search query. The repository-layer decides which column is filtered via `Field`.

type UnitMeasure

type UnitMeasure int64

func (*UnitMeasure) UnmarshalText

func (d *UnitMeasure) UnmarshalText(text []byte) error

Jump to

Keyboard shortcuts

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