Documentation
¶
Index ¶
- func CtxAudit(ctx context.Context, auditors []any) error
- func CtxCreateAudit(ctx context.Context, entity any) error
- func CtxDeleteAudit(ctx context.Context, entity any) (bool, error)
- func CtxUpdateAudit(ctx context.Context, entity any) error
- func CtxUpdateAuditAndEnv(ctx context.Context, entity any) error
- func GenUserInfoContext(info *ByteReqInfo) context.Context
- func SetCtxFixedStrategy(ctx context.Context, fs FixedStrategy) context.Context
- func SetCtxRepoDataIsolation(ctx context.Context, di DataIsolation) context.Context
- func SetCtxRepoDeployIsolation(ctx context.Context, di DeployIsolation) context.Context
- func SetCtxRepoUpdateRunContext(ctx context.Context, urc UpdateRunContext) context.Context
- func SetCtxSoftDelete(ctx context.Context, sd SoftDelete) context.Context
- type ByteReqInfo
- type CreateAudit
- type CreateAuditMs
- type CreateAuditor
- type CtxKey
- type DataIsolation
- type DeleteAudit
- type DeleteAuditMs
- type DeleteAuditor
- type DeployIsolation
- type Entity
- type EntityConstraint
- type FeedResult
- type FixedStrategy
- type IDType
- type OperatorCarrier
- type PagedResult
- type Pager
- type Repo
- type RepoOp
- type RepoOpt
- type SnowflakeID
- func (id SnowflakeID) Int64() (int64, error)
- func (id SnowflakeID) MarshalBSONValue() (byte, []byte, error)
- func (id SnowflakeID) MarshalJSON() ([]byte, error)
- func (id *SnowflakeID) Scan(src any) error
- func (id SnowflakeID) String() string
- func (id SnowflakeID) Uint64() (uint64, error)
- func (id *SnowflakeID) UnmarshalBSONValue(typ byte, data []byte) error
- func (id *SnowflakeID) UnmarshalJSON(data []byte) error
- func (id SnowflakeID) Value() (driver.Value, error)
- type SoftDelete
- type TenantAudit
- type TenantCarrier
- type UpdateAudit
- type UpdateAuditMs
- type UpdateAuditor
- type UpdateRunContext
- type UpdatesResult
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CtxCreateAudit ¶
CtxCreateAudit fills creator and tenant/app audit fields from context.
func CtxDeleteAudit ¶
CtxDeleteAudit fills deleter audit fields from context when supported.
func CtxUpdateAudit ¶
CtxUpdateAudit fills updater audit fields from context.
func CtxUpdateAuditAndEnv ¶
CtxUpdateAuditAndEnv updates audit fields and keeps a hook for env sync.
func GenUserInfoContext ¶
func GenUserInfoContext(info *ByteReqInfo) context.Context
func SetCtxFixedStrategy ¶
func SetCtxFixedStrategy(ctx context.Context, fs FixedStrategy) context.Context
SetCtxFixedStrategy sets the sync-delay fix strategy in context.
func SetCtxRepoDataIsolation ¶
func SetCtxRepoDataIsolation(ctx context.Context, di DataIsolation) context.Context
SetCtxRepoDataIsolation sets data isolation in context.
func SetCtxRepoDeployIsolation ¶
func SetCtxRepoDeployIsolation(ctx context.Context, di DeployIsolation) context.Context
SetCtxRepoDeployIsolation sets deploy isolation in context.
func SetCtxRepoUpdateRunContext ¶
func SetCtxRepoUpdateRunContext(ctx context.Context, urc UpdateRunContext) context.Context
SetCtxRepoUpdateRunContext sets update run context in context.
func SetCtxSoftDelete ¶
func SetCtxSoftDelete(ctx context.Context, sd SoftDelete) context.Context
SetCtxSoftDelete sets soft delete behavior in context.
Types ¶
type ByteReqInfo ¶
func NewByteReqInfo ¶
func NewByteReqInfo() *ByteReqInfo
type CreateAudit ¶
type CreateAudit struct {
// CreatedAt is the creation time.
CreatedAt time.Time `bson:"created_at,omitempty" json:"created_at,omitempty" gorm:"created_at;type:datetime(3);autoUpdateTime:false"`
// CreatedBy is the creator id.
CreatedBy string `bson:"created_by,omitempty" json:"created_by,omitempty" gorm:"created_by;type:varchar(255)"`
}
CreateAudit stores creator and creation time in time.Time.
func (*CreateAudit) GetCreateTimeRaw ¶
func (c *CreateAudit) GetCreateTimeRaw() interface{}
GetCreateTimeRaw returns the raw creation time value.
func (*CreateAudit) GetCreatorInfo ¶
func (c *CreateAudit) GetCreatorInfo() (string, time.Time)
GetCreatorInfo returns creator id and creation time.
func (*CreateAudit) SetCreator ¶
func (c *CreateAudit) SetCreator(user string)
SetCreator sets creator id and time.
func (*CreateAudit) SetUser ¶
func (c *CreateAudit) SetUser(user string)
SetUser sets creator id and time.
type CreateAuditMs ¶
type CreateAuditMs struct {
// CreatedAt is the creation time in milliseconds.
CreatedAt int64 `bson:"created_at,omitempty" json:"created_at,omitempty" gorm:"created_at;type:bigint unsigned;autoUpdateTime:false"`
// CreatedBy is the creator id.
CreatedBy string `bson:"created_by,omitempty" json:"created_by,omitempty" gorm:"created_by;type:varchar(255)"`
}
CreateAuditMs stores creator and creation time in milliseconds.
func (*CreateAuditMs) GetCreateTimeRaw ¶
func (c *CreateAuditMs) GetCreateTimeRaw() interface{}
GetCreateTimeRaw returns the raw creation time value.
func (*CreateAuditMs) GetCreatorInfo ¶
func (c *CreateAuditMs) GetCreatorInfo() (string, time.Time)
GetCreatorInfo returns creator id and creation time.
func (*CreateAuditMs) SetCreator ¶
func (c *CreateAuditMs) SetCreator(user string)
SetCreator sets creator id and time.
func (*CreateAuditMs) SetUser ¶
func (c *CreateAuditMs) SetUser(user string)
SetUser sets creator id and time.
type CreateAuditor ¶
type CreateAuditor interface {
GetCreatorInfo() (string, time.Time)
SetCreator(user string)
GetCreateTimeRaw() interface{}
}
CreateAuditor exposes creation audit behavior.
type CtxKey ¶
type CtxKey string
CtxKey is the context key type for repo options.
const ( // CtxKeyRepoOpt stores RepoOpt in context. CtxKeyRepoOpt CtxKey = "repo-opt" )
type DataIsolation ¶
type DataIsolation string
DataIsolation defines data isolation levels.
const ( // DataIsolationNone disables data isolation. DataIsolationNone DataIsolation = "none" // DataIsolationTenant isolates by tenant. DataIsolationTenant DataIsolation = "tenant" // DataIsolationUser isolates by user. DataIsolationUser DataIsolation = "user" // DataIsolationApp isolates by app. DataIsolationApp DataIsolation = "app" )
type DeleteAudit ¶
type DeleteAudit struct {
// DeletedAt is the delete time.
DeletedAt time.Time `bson:"deleted_at,omitempty" json:"deleted_at,omitempty" gorm:"deleted_at;type:datetime(3);autoUpdateTime:false"`
// DeletedBy is the deleter id.
DeletedBy string `bson:"deleted_by,omitempty" json:"deleted_by,omitempty" gorm:"deleted_by;type:varchar(255)"`
}
DeleteAudit stores deleter and delete time in time.Time.
func (*DeleteAudit) GetDeleteInfo ¶
func (c *DeleteAudit) GetDeleteInfo() (string, time.Time)
GetDeleteInfo returns deleter id and delete time.
func (*DeleteAudit) GetDeleteTimeRaw ¶
func (c *DeleteAudit) GetDeleteTimeRaw() interface{}
GetDeleteTimeRaw returns the raw delete time value.
func (*DeleteAudit) SetDeleter ¶
func (c *DeleteAudit) SetDeleter(user string)
SetDeleter sets deleter id and time.
func (*DeleteAudit) SetUser ¶
func (c *DeleteAudit) SetUser(user string)
SetUser sets deleter id and time.
type DeleteAuditMs ¶
type DeleteAuditMs struct {
// DeletedAt is the delete time in milliseconds.
DeletedAt int64 `bson:"deleted_at,omitempty" json:"deleted_at,omitempty" gorm:"deleted_at;type:bigint unsigned;autoUpdateTime:false"`
// DeletedBy is the deleter id.
DeletedBy string `bson:"deleted_by,omitempty" json:"deleted_by,omitempty" gorm:"deleted_by;type:varchar(255)"`
}
DeleteAuditMs stores deleter and delete time in milliseconds.
func (*DeleteAuditMs) GetDeleteInfo ¶
func (c *DeleteAuditMs) GetDeleteInfo() (string, time.Time)
GetDeleteInfo returns deleter id and delete time.
func (*DeleteAuditMs) GetDeleteTimeRaw ¶
func (c *DeleteAuditMs) GetDeleteTimeRaw() interface{}
GetDeleteTimeRaw returns the raw delete time value.
func (*DeleteAuditMs) SetDeleter ¶
func (c *DeleteAuditMs) SetDeleter(user string)
SetDeleter sets deleter id and time.
func (*DeleteAuditMs) SetUser ¶
func (c *DeleteAuditMs) SetUser(user string)
SetUser sets deleter id and time.
type DeleteAuditor ¶
type DeleteAuditor interface {
GetDeleteInfo() (string, time.Time)
SetDeleter(user string)
GetDeleteTimeRaw() interface{}
}
DeleteAuditor exposes delete audit behavior.
type DeployIsolation ¶
type DeployIsolation string
DeployIsolation defines deployment isolation levels.
const ( // DeployIsolationNone disables deployment isolation. DeployIsolationNone DeployIsolation = "none" // DeployIsolationCluster isolates by cluster. DeployIsolationCluster DeployIsolation = "cluster" // DeployIsolationEnv isolates by environment. DeployIsolationEnv DeployIsolation = "env" )
type Entity ¶
type Entity[P IDType] struct { // ID is the primary identifier. ID P `bson:"_id,omitempty" json:"id" gorm:"primaryKey"` // CreateAudit stores creation metadata. CreateAudit `bson:"inline" gorm:"embedded"` }
Entity is a base model with ID and create audit fields.
type EntityConstraint ¶
type EntityConstraint[P IDType] interface { GetID() P SetID(P) }
EntityConstraint describes entities that expose GetID/SetID.
type FeedResult ¶
type FeedResult[T any] struct { // Rows is the data slice. Rows []T `json:"rows"` // NextPageToken is the server-generated token for the next feed page. NextPageToken string `json:"next_page_token,omitempty"` }
FeedResult wraps cursor-paginated rows and the continuation token.
type FixedStrategy ¶
type FixedStrategy string
FixedStrategy defines the sync delay fix strategy.
const ( // FixedStrategyNone disables fix behavior. FixedStrategyNone FixedStrategy = "none" // FixedStrategyBackoff uses backoff to fix sync delay. FixedStrategyBackoff FixedStrategy = "backoff" )
type OperatorCarrier ¶
type OperatorCarrier interface {
SetUser(user string)
}
OperatorCarrier allows setting operator for auditing.
type PagedResult ¶
type PagedResult[T any] struct { // Rows is the data slice. Rows []T `json:"rows"` // TotalCount is the total number of rows. TotalCount int `json:"total_count"` }
PagedResult wraps paginated rows and total count.
type Pager ¶
type Pager struct {
// PageSize is the number of items per page.
PageSize int `json:"page_size,omitempty" auto_read:"page_size,query"`
// PageNum is the current page number.
PageNum int `json:"page_num,omitempty" auto_read:"page_num,query"`
// PageToken is the feed pagination token when using cursor-based paging.
PageToken string `json:"page_token,omitempty" auto_read:"page_token,query"`
}
Pager carries pagination input.
func (*Pager) GetPageInfo ¶
GetPageInfo returns page size, number, and token.
func (*Pager) InitialDefaultVal ¶
func (p *Pager) InitialDefaultVal()
InitialDefaultVal sets default paging values when empty.
func (*Pager) SetPageNumber ¶
SetPageNumber updates the page number.
type Repo ¶
type Repo[P IDType, T EntityConstraint[P]] interface { Get(ctx context.Context, id P) (ret T, err error) Create(ctx context.Context, doc T) (T, error) Update(ctx context.Context, doc T) (T, error) Upsert(ctx context.Context, doc T) (T, error) Patch(ctx context.Context, doc T) error Delete(ctx context.Context, doc T) error // BatchCreate does not guarantee transactional behavior. BatchCreate(ctx context.Context, docs []T) error BatchUpdate(ctx context.Context, docs []T) error BatchDelete(ctx context.Context, docs []T) (int, error) BatchDeleteByIDs(ctx context.Context, ids []P) (int, error) }
Repo defines the repository interface for CRUD and batch operations.
type RepoOpt ¶
type RepoOpt struct {
// DeployIsolation controls deployment isolation scope.
DeployIsolation DeployIsolation
// DataIsolation controls data isolation scope.
DataIsolation DataIsolation
// UpdateRunContext controls audit update timing.
UpdateRunContext UpdateRunContext
// TryFixSyncDelay controls sync delay fix strategy.
TryFixSyncDelay FixedStrategy
// SoftDelete controls soft delete behavior.
SoftDelete SoftDelete
}
RepoOpt carries repository behavior flags.
func CtxMergeRepoOpt ¶
CtxMergeRepoOpt merges context repo options with explicit options.
func CtxRepoOptOrNew ¶
CtxRepoOptOrNew gets RepoOpt from context or returns default.
type SnowflakeID ¶
type SnowflakeID string
SnowflakeID stores a snowflake-style ID as a decimal string in Go/API paths, while serializing to databases as a numeric value so database ordering follows numeric ordering.
This type is useful when callers need API-safe strings but still want numeric database storage. It has a higher integration cost than plain fixed-width string IDs because every persistence layer must honor the custom codec. When a project can choose its ID format freely, prefer fixed-width lexicographic IDs so application code does not need numeric/string conversion at every boundary.
func NewSnowflakeID ¶
func NewSnowflakeID(id uint64) SnowflakeID
NewSnowflakeID converts a uint64 ID into SnowflakeID.
func ParseSnowflakeID ¶
func ParseSnowflakeID(s string) (SnowflakeID, error)
ParseSnowflakeID parses a decimal string into SnowflakeID.
func (SnowflakeID) Int64 ¶
func (id SnowflakeID) Int64() (int64, error)
Int64 returns the numeric ID value as int64 when it fits common database integer storage.
func (SnowflakeID) MarshalBSONValue ¶
func (id SnowflakeID) MarshalBSONValue() (byte, []byte, error)
MarshalBSONValue stores the ID as BSON int64 so database ordering follows numeric ordering.
func (SnowflakeID) MarshalJSON ¶
func (id SnowflakeID) MarshalJSON() ([]byte, error)
MarshalJSON serializes the ID as a JSON string.
func (*SnowflakeID) Scan ¶
func (id *SnowflakeID) Scan(src any) error
Scan decodes SnowflakeID from SQL driver values.
func (SnowflakeID) String ¶
func (id SnowflakeID) String() string
String returns the decimal string form.
func (SnowflakeID) Uint64 ¶
func (id SnowflakeID) Uint64() (uint64, error)
Uint64 returns the numeric ID value.
func (*SnowflakeID) UnmarshalBSONValue ¶
func (id *SnowflakeID) UnmarshalBSONValue(typ byte, data []byte) error
UnmarshalBSONValue decodes BSON int64/int32/string values into SnowflakeID.
func (*SnowflakeID) UnmarshalJSON ¶
func (id *SnowflakeID) UnmarshalJSON(data []byte) error
UnmarshalJSON accepts a JSON string, and also accepts a JSON number for controlled server-side use.
type SoftDelete ¶
type SoftDelete string
SoftDelete defines soft delete behavior.
const ( // SoftDeleteEnable enables soft delete by default. SoftDeleteEnable SoftDelete = "" // SoftDeleteDisable disables soft delete. SoftDeleteDisable SoftDelete = "disable" )
type TenantAudit ¶
type TenantAudit struct {
// TenantID is the tenant identifier.
TenantID string `bson:"tenant_id,omitempty" json:"tenant_id" gorm:"tenant_id;type:varchar(255)"`
// AppID is the application identifier.
AppID string `bson:"app_id,omitempty" json:"app_id" gorm:"app_id;type:varchar(255)"`
}
TenantAudit stores tenant and app identifiers.
func (*TenantAudit) GetTenantID ¶
func (c *TenantAudit) GetTenantID() string
GetTenantID returns tenant id.
func (*TenantAudit) SetTenantID ¶
func (c *TenantAudit) SetTenantID(id string)
SetTenantID sets tenant id.
type TenantCarrier ¶
type TenantCarrier interface {
GetTenantID() string
SetTenantID(id string)
GetAppID() string
SetAppID(id string)
}
TenantCarrier exposes tenant and app identifiers.
type UpdateAudit ¶
type UpdateAudit struct {
// UpdatedAt is the update time.
UpdatedAt time.Time `bson:"updated_at,omitempty" json:"updated_at,omitempty" gorm:"updated_at;type:datetime(3);autoUpdateTime:false"`
// UpdatedBy is the updater id.
UpdatedBy string `bson:"updated_by,omitempty" json:"updated_by,omitempty" gorm:"updated_by;type:varchar(255)"`
}
UpdateAudit stores updater and update time in time.Time.
func (*UpdateAudit) GetUpdateInfo ¶
func (c *UpdateAudit) GetUpdateInfo() (string, time.Time)
GetUpdateInfo returns updater id and update time.
func (*UpdateAudit) GetUpdateTimeRaw ¶
func (c *UpdateAudit) GetUpdateTimeRaw() interface{}
GetUpdateTimeRaw returns the raw update time value.
func (*UpdateAudit) SetUpdater ¶
func (c *UpdateAudit) SetUpdater(user string)
SetUpdater sets updater id and time.
func (*UpdateAudit) SetUser ¶
func (c *UpdateAudit) SetUser(user string)
SetUser sets updater id and time.
type UpdateAuditMs ¶
type UpdateAuditMs struct {
// UpdatedAt is the update time in milliseconds.
UpdatedAt int64 `bson:"updated_at,omitempty" json:"updated_at,omitempty" gorm:"updated_at;type:bigint unsigned;autoUpdateTime:false"`
// UpdatedBy is the updater id.
UpdatedBy string `bson:"updated_by,omitempty" json:"updated_by,omitempty" gorm:"updated_by;type:varchar(255)"`
}
UpdateAuditMs stores updater and update time in milliseconds.
func (*UpdateAuditMs) GetUpdateInfo ¶
func (c *UpdateAuditMs) GetUpdateInfo() (string, time.Time)
GetUpdateInfo returns updater id and update time.
func (*UpdateAuditMs) GetUpdateTimeRaw ¶
func (c *UpdateAuditMs) GetUpdateTimeRaw() interface{}
GetUpdateTimeRaw returns the raw update time value.
func (*UpdateAuditMs) SetUpdater ¶
func (c *UpdateAuditMs) SetUpdater(user string)
SetUpdater sets updater id and time.
func (*UpdateAuditMs) SetUser ¶
func (c *UpdateAuditMs) SetUser(user string)
SetUser sets updater id and time.
type UpdateAuditor ¶
type UpdateAuditor interface {
GetUpdateInfo() (string, time.Time)
SetUpdater(user string)
GetUpdateTimeRaw() interface{}
}
UpdateAuditor exposes update audit behavior.
type UpdateRunContext ¶
type UpdateRunContext string
UpdateRunContext defines when audit updates are applied.
const ( // UpdateRunContextAtCreating applies updates only at creation. UpdateRunContextAtCreating UpdateRunContext = "at-creating" // UpdateRunContextAlways applies updates on every update. UpdateRunContextAlways UpdateRunContext = "always" )
type UpdatesResult ¶
type UpdatesResult struct {
// HasOriginalUpdate indicates whether original updated time existed.
HasOriginalUpdate bool
// OriginalUpdatedAt is the original update time, time.Time or int64.
OriginalUpdatedAt any
}
UpdatesResult records original update metadata for optimistic checks.
func UpdateLockAndAudit ¶
UpdateLockAndAudit updates audit fields and returns previous update info if present.