model

package
v0.0.0-...-573f856 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CtxAudit

func CtxAudit(ctx context.Context, auditors []any) error

CtxAudit fills operator and tenant/app fields for a batch of auditors.

func CtxCreateAudit

func CtxCreateAudit(ctx context.Context, entity any) error

CtxCreateAudit fills creator and tenant/app audit fields from context.

func CtxDeleteAudit

func CtxDeleteAudit(ctx context.Context, entity any) (bool, error)

CtxDeleteAudit fills deleter audit fields from context when supported.

func CtxUpdateAudit

func CtxUpdateAudit(ctx context.Context, entity any) error

CtxUpdateAudit fills updater audit fields from context.

func CtxUpdateAuditAndEnv

func CtxUpdateAuditAndEnv(ctx context.Context, entity any) error

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

type ByteReqInfo struct {
	UID       string
	TID       string
	AID       string
	TraceID   string
	RequestID string
}

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.

func (*Entity[P]) GetID

func (e *Entity[P]) GetID() P

GetID returns the entity id.

func (*Entity[P]) SetID

func (e *Entity[P]) SetID(id P)

SetID sets the entity id.

type EntityConstraint

type EntityConstraint[P IDType] interface {
	GetID() P
	SetID(P)
}

EntityConstraint describes entities that expose GetID/SetID.

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 IDType

type IDType interface {
	string | int | int64 | uint64
}

IDType defines supported identifier types.

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

func (p *Pager) GetPageInfo() (pageSize, pageNumber int, pageToken string)

GetPageInfo returns page size, number, and token.

func (*Pager) InitialDefaultVal

func (p *Pager) InitialDefaultVal()

InitialDefaultVal sets default paging values when empty.

func (*Pager) SetPageNumber

func (p *Pager) SetPageNumber(pageNumber int)

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 请注意该接口不保证事务性
	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 RepoOp

type RepoOp func(opt *RepoOpt)

RepoOp is a functional option for RepoOpt.

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

func CtxMergeRepoOpt(ctx context.Context, merge *RepoOpt) *RepoOpt

CtxMergeRepoOpt merges context repo options with explicit options.

func CtxRepoOptOrNew

func CtxRepoOptOrNew(ctx context.Context) RepoOpt

CtxRepoOptOrNew gets RepoOpt from context or returns default.

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

func (c *TenantAudit) GetAppID() string

GetAppID returns app id.

func (*TenantAudit) GetTenantID

func (c *TenantAudit) GetTenantID() string

GetTenantID returns tenant id.

func (*TenantAudit) SetAppID

func (c *TenantAudit) SetAppID(id string)

SetAppID sets app 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

func UpdateLockAndAudit[T any](ctx context.Context, doc T, opt *RepoOpt) (*UpdatesResult, error)

UpdateLockAndAudit updates audit fields and returns previous update info if present.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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