Documentation
¶
Index ¶
- func ApplyFilter(query any, filter QueryFilter) any
- func ForceDelete(ctx context.Context, deletion any) error
- func ForceDeleteBulk(ctx context.Context, deletion any) (int, error)
- func IsDeleted(entity any) bool
- func New(dsn string) (*sql.DB, error)
- func OnlyDeleted(query any) any
- func Restore(ctx context.Context, mutation any) error
- func RestoreBulk(ctx context.Context, query any) (int, error)
- func SoftDelete(ctx context.Context, mutation any, opts *SoftDeleteOptions) error
- func SoftDeleteBulk(ctx context.Context, query any, opts *SoftDeleteOptions) (int, error)
- func WithDeleted(query any) any
- type QueryFilter
- type SoftDeletable
- type SoftDeleteOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ApplyFilter ¶
func ApplyFilter(query any, filter QueryFilter) any
ApplyFilter applies the appropriate filter to a query based on QueryFilter option This is a helper function to be used in repository methods
Example:
query := database.ApplyFilter(client.User.Query(), database.FilterExcludeDeleted)
func ForceDelete ¶
ForceDelete permanently deletes an entity, bypassing soft delete Use with caution as this operation is irreversible
Example:
err := database.ForceDelete(ctx, client.User.DeleteOneID(userID))
func ForceDeleteBulk ¶
ForceDeleteBulk permanently deletes multiple entities matching the query Use with caution as this operation is irreversible
Example:
deleted, err := database.ForceDeleteBulk(ctx, client.User.Delete().Where(user.DeletedAtNotNil()))
func IsDeleted ¶
IsDeleted checks if an entity has been soft-deleted
Example:
if database.IsDeleted(user) {
// Handle deleted user
}
func OnlyDeleted ¶
OnlyDeleted returns a query that only includes soft-deleted records This is a convenience wrapper around ApplyFilter
Example:
query := database.OnlyDeleted(client.User.Query())
func Restore ¶
Restore restores a soft-deleted entity by clearing the deleted_at timestamp
Example:
err := database.Restore(ctx, client.User.UpdateOneID(userID))
func RestoreBulk ¶
RestoreBulk restores multiple soft-deleted entities matching the query
Example:
restored, err := database.RestoreBulk(ctx, client.User.Query().Where(user.DeletedAtNotNil()))
func SoftDelete ¶
func SoftDelete(ctx context.Context, mutation any, opts *SoftDeleteOptions) error
SoftDelete performs a soft delete on an entity by setting deleted_at timestamp This function works with any Ent entity that has a deleted_at field
Example:
err := database.SoftDelete(ctx, client.User.UpdateOneID(userID), nil)
func SoftDeleteBulk ¶
SoftDeleteBulk performs a soft delete on multiple entities matching the query
Example:
deleted, err := database.SoftDeleteBulk(ctx, client.User.Query().Where(user.StatusEQ("inactive")), nil)
func WithDeleted ¶
WithDeleted returns a query that includes soft-deleted records This is a convenience wrapper around ApplyFilter
Example:
query := database.WithDeleted(client.User.Query())
Types ¶
type QueryFilter ¶
type QueryFilter int
QueryFilter represents different query filtering modes for soft-deleted records
const ( // FilterExcludeDeleted excludes soft-deleted records (default behavior) FilterExcludeDeleted QueryFilter = iota // FilterIncludeDeleted includes both active and soft-deleted records FilterIncludeDeleted // FilterOnlyDeleted returns only soft-deleted records FilterOnlyDeleted )
type SoftDeletable ¶
type SoftDeletable interface {
// SetDeletedAt sets the deleted_at timestamp
SetDeletedAt(time.Time)
// GetDeletedAt returns the deleted_at timestamp
GetDeletedAt() *time.Time
}
SoftDeletable is an interface that entities with soft delete support must implement
type SoftDeleteOptions ¶
type SoftDeleteOptions struct {
// Filter determines which records to include in queries
Filter QueryFilter
// Timestamp allows setting a custom deletion timestamp (defaults to time.Now())
Timestamp *time.Time
}
SoftDeleteOptions contains options for soft delete operations
func DefaultSoftDeleteOptions ¶
func DefaultSoftDeleteOptions() *SoftDeleteOptions
DefaultSoftDeleteOptions returns default options for soft delete operations