domain

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package domain defines the core interfaces and types for sqlcredo. It provides the foundational contracts for CRUD operations, SQL execution, pagination, transactions, and debugging functionality.

Index

Constants

View Source
const (
	DefaultPageNumber = 0
	DefaultPageSize   = 10
	DefaultSortDesc   = false

	DefaultContentInitSize = 10
)

Variables

View Source
var ErrInvalidPageSize = errors.New("invalid page size")

ErrInvalidPageSize is returned when a page size parameter is not a positive number. This error indicates that the requested page size is invalid for pagination operations.

Functions

This section is empty.

Types

type CRUD

type CRUD[T any, I comparable] interface {
	// GetAll retrieves all entities of type T from the database.
	GetAll(ctx context.Context, dest *[]T) error

	// GetByID retrieves a single entity by its ID.
	// Returns an error if the entity is not found.
	GetByID(ctx context.Context, dest *T, id I) error

	// GetByIDs retrieves multiple entities by their IDs.
	// The returned slice maintains the same order as the input IDs.
	GetByIDs(ctx context.Context, dest *[]T, ids []I) error

	// Create inserts a new entity into the database.
	// The entity pointer must not be nil.
	Create(ctx context.Context, e *T) (sql.Result, error)

	// DeleteAll removes all entities of type T from the database.
	DeleteAll(ctx context.Context) (sql.Result, error)

	// Delete removes a single entity by its ID.
	Delete(ctx context.Context, id I) (sql.Result, error)

	// Update modifies an existing entity identified by its ID.
	// The entity pointer must not be nil.
	Update(ctx context.Context, id I, e *T) (sql.Result, error)
}

CRUD defines a generic interface for basic database operations. Type parameters:

  • T: the entity type being managed
  • I: the type of the entity's ID field (must be comparable)

type DebugFunc

type DebugFunc func(sql string, args ...any)

DebugFunc is a function type used for SQL query debugging and logging. It receives the SQL query string and its arguments before execution.

Parameters:

  • sql: The SQL query string to be executed
  • args: Variable number of query arguments of any type

type NewPageOpt

type NewPageOpt[T any] func(*NewPageOptsObj[T])

type NewPageOptsObj

type NewPageOptsObj[T any] struct {
	Content         *[]T
	ContentInitSize *int
}

type Page

type Page[T any] struct {
	Number     uint   // Current page number (0-based)
	Size       uint   // Number of items in the current page
	Total      uint64 // Total number of items across all pages
	TotalPages uint   // Total number of pages
	Content    []T    // Slice containing the page's items
}

Page represents a paginated result set containing items of type T. It includes metadata about the current page, total items, and the actual content.

type PageOpt

type PageOpt func(*PageOpts)

PageOpt is a function type that modifies PageParams. It follows the functional options pattern for configuring pagination parameters.

type PageOpts

type PageOpts struct {
	PageNumber uint     // The current page number (0-based)
	PageSize   uint     // Number of items per page
	SortBy     []string // List of columns to sort by
	SortDesc   bool     // If true, sort in descending order
}

PageOpts defines the parameters for pagination and sorting.

func NewPageOpts

func NewPageOpts(idColumn string, opts ...PageOpt) (PageOpts, error)

NewPageOpts creates a new PageOpts with the given options and validate it. If no SortBy specified the idColumn will be used for sorting.

func (PageOpts) Validate

func (p PageOpts) Validate() error

type PageResolver

type PageResolver[T any] interface {
	// GetPage retrieves a single page of results based on the provided pagination options.
	GetPage(ctx context.Context, dest *Page[T], opts ...PageOpt) error

	// Count returns the total number of items available across all pages.
	// This is useful for calculating total pages and displaying pagination metadata.
	Count(ctx context.Context) (uint64, error)
}

PageResolver is an interface for retrieving paginated results of type T.

type SQLExecutor

type SQLExecutor interface {
	// SelectOne executes a query that is expected to return at most one row.
	// It scans the resulting row into the dest parameter, which must be a pointer.
	// Returns an error if the query fails or if scanning into dest fails.
	SelectOne(ctx context.Context, dest any, query string, args ...any) error

	// SelectMany executes a query that can return multiple rows.
	// It scans all resulting rows into the dest parameter, which must be a pointer to a slice.
	// Returns an error if the query fails or if scanning into dest fails.
	SelectMany(ctx context.Context, dest any, query string, args ...any) error

	// Exec executes a query that doesn't return rows (like INSERT, UPDATE, DELETE).
	// Returns a Result object summarizing the effect of the query and any error encountered.
	Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
}

SQLExecutor defines an interface for executing SQL operations. It provides a high-level abstraction for common database operations like selecting single/multiple rows, executing statements, and managing transactions.

type Transaction

type Transaction[T any, I comparable] interface {
	SQLExecutor
	CRUD[T, I]
	PageResolver[T]

	// Commit commits the transaction
	Commit() error

	// Rollback aborts the transaction
	Rollback() error
}

Transaction is a comprehensive interface that combines major SQLCredo capabilities, but wrap them in transaction.

type TransactionExecutor

type TransactionExecutor[T any, I comparable] interface {
	// BeginTx creates and starts new transaction.
	// Returns the transaction object and any error encountered during transaction creation.
	BeginTx(ctx context.Context, opts *sql.TxOptions) (Transaction[T, I], error)
}

TransactionExecutor deifnes an interface to execute transactions.

Jump to

Keyboard shortcuts

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