sqlcredo

package module
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2025 License: MIT Imports: 9 Imported by: 0

README

SQLCredo

SQLCredo is a type-safe generic SQL CRUD operations wrapper for Go, built on top of sqlx and goqu.

The idea of the package to provide basic CRUD and pagination operations out of the box and simplify adding custom raw SQL-queries to extend functionality.

Features

  • Generic type-safe CRUD operations
  • Built-in pagination support
  • SQL query debugging capabilities
  • Support for multiple SQL drivers (tested on sqlite3 and postgres (pgx))
  • Transaction support
  • Prepared statements by default

Installation

go get github.com/Klojer/sqlcredo

Quick Start

import (
 "context"
 "database/sql"

 "github/Klojer/sqlcredo"
 "github/Klojer/sqlcredo/pkg/model"
)

type User struct {
 ID   int    `db:"id"`
 Name string `db:"name"`
}

func main() {
 ctx := context.Background()

 // Create a new SQLCredo instance
 db, _ := sql.Open("sqlite3", "test.db")
 repo := sqlcredo.NewSQLCredo[User, int](db, "sqlite3", "users", "id")

 // Create
 user := User{ID: 1, Name: "John"}
 _, err := repo.Create(ctx, &user)
 orPanic(err)

 // Read
 users, err := repo.GetAll(ctx)
 orPanic(err)

 // Read with pagination
 page, err := repo.GetPage(ctx,
  model.WithPageNumber(0),
  model.WithPageSize(10),
  model.WithSortBy("name"))
 orPanic(err)

 // Update
 _, err = repo.Update(ctx, 1, &user)
 orPanic(err)

 // Delete
 _, err = repo.Delete(ctx, 1)
 orPanic(err)
}

func orPanic(err error) {
 if err == nil {
  return
 }
 panic(err)
}

See example of repository with custom query: examples/users/users.go

Debug Support

Enable SQL query debugging:

repo.WithDebugFunc(func(sql string, args ...any) {
    log.Printf("SQL: %s Args: %v", sql, args)
})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SQLCredo

type SQLCredo[T any, I comparable] interface {
	model.SQLExecutor
	model.CRUD[T, I]
	model.PageResolver[T]

	// InitSchema executes a SQL query to initialize the database schema.
	// Typically used for creating tables and other database objects.
	InitSchema(ctx context.Context, sql string) (sql.Result, error)

	// WithDebugFunc sets a debug function for SQL query logging.
	// The debug function will be called before executing any SQL query.
	// Returns the modified SQLCredo instance for method chaining.
	WithDebugFunc(newDebugFunc model.DebugFunc) SQLCredo[T, I]

	// GetDebugFunc returns the currently set debug function.
	// Returns nil if no debug function is set.
	GetDebugFunc() model.DebugFunc
}

SQLCredo is a comprehensive interface that combines SQL execution, CRUD operations, and pagination capabilities for a specific entity type.

Type Parameters:

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

func NewSQLCredo

func NewSQLCredo[T any, I comparable](db *sql.DB, driver string, tableName string, idColumn string) SQLCredo[T, I]

NewSQLCredo creates a new instance of SQLCredo for the specified entity type and ID type.

Parameters:

  • db: A pointer to the underlying database connection
  • driver: The database driver name (e.g., "postgres", "mysql")
  • tableName: The name of the database table for the entity
  • idColumn: The name of the ID column in the table

Returns a fully initialized SQLCredo instance

Directories

Path Synopsis
examples
internal
pkg

Jump to

Keyboard shortcuts

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