database

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

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

func ForceDelete(ctx context.Context, deletion any) error

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

func ForceDeleteBulk(ctx context.Context, deletion any) (int, error)

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

func IsDeleted(entity any) bool

IsDeleted checks if an entity has been soft-deleted

Example:

if database.IsDeleted(user) {
    // Handle deleted user
}

func New

func New(dsn string) (*sql.DB, error)

func OnlyDeleted

func OnlyDeleted(query any) any

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

func Restore(ctx context.Context, mutation any) error

Restore restores a soft-deleted entity by clearing the deleted_at timestamp

Example:

err := database.Restore(ctx, client.User.UpdateOneID(userID))

func RestoreBulk

func RestoreBulk(ctx context.Context, query any) (int, error)

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

func SoftDeleteBulk(ctx context.Context, query any, opts *SoftDeleteOptions) (int, error)

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

func WithDeleted(query any) any

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

Jump to

Keyboard shortcuts

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