datagate

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2026 License: MIT Imports: 9 Imported by: 0

README

Datagate

Datagate is a simple and type-safe Go library for CRUD operations with PostgreSQL. It is built on top of the pgx driver and uses Go generics to reduce boilerplate. You describe your models and filters as Go structs, and Datagate handles SQL generation for you.

Features

  • Generic CRUD interface for any struct
  • Works directly with pgx connection pool and transactions
  • Automatic mapping of struct fields to table columns using tags
  • Flexible filtering with filter structs
  • Easy integration with squirrel for SQL building

Installation

go get github.com/gippuss/datagate

Quick Start

1. Define your models
type User struct {
    ID        int64     `db:"id"`
    Name      string    `db:"name" insert:"name"`
    Email     string    `db:"email" insert:"email"`
    Age       int       `db:"age" insert:"age"`
    CreatedAt time.Time `db:"created_at" insert:"created_at"`
    UpdatedAt time.Time `db:"updated_at"`
}

type UserFilter struct {
    ID    *int64  `filter:"id"`
    Name  *string `filter:"name"`
    Email *string `filter:"email"`
    Age   *int    `filter:"age"`
}
  • Use db tag for reading from the database
  • Use insert tag for inserting new records
  • Use filter tag for filtering in queries
2. Initialize Datagate
import (
    "github.com/gippuss/datagate"
    "github.com/jackc/pgx/v5/pgxpool"
    "github.com/Masterminds/squirrel"
)

pool, _ := pgxpool.New(ctx, "postgres://user:pass@host:port/dbname")
sqBuilder := squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar)

dataGate, err := datagate.NewDataGate[User, UserFilter](
    "users",      // table name
    "id",         // primary key
    pool,
)
3. CRUD operations
Create
id, err := dataGate.Create(ctx, User{Name: "Alice", Email: "alice@example.com", Age: 25})
Read
users, err := dataGate.Get(ctx, UserFilter{Name: &name})
Update
err := dataGate.Update(ctx, UserFilter{ID: &id}, map[string]interface{}{"name": "Bob"})
Delete
err := dataGate.Delete(ctx, UserFilter{ID: &id})
Transactions
tx, _ := pool.Begin(ctx)
txDataGate := dataGate.GetWithTransaction(tx)
id, err := txDataGate.Create(ctx, user)
// ...
tx.Commit(ctx)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNoRowsAffected = errors.New("no rows affected")

Functions

This section is empty.

Types

type DataGate

type DataGate[T, F interface{}] interface {
	GetWithTransaction(tx pgx.Tx) DataGate[T, F]

	Create(ctx context.Context, data T) (int64, error)
	Get(ctx context.Context, filter F) ([]T, error)
	Update(ctx context.Context, filter F, data map[string]interface{}) error
	Delete(ctx context.Context, filter F) error
}

DataGate is a generic interface for the database queries T - struct of db table F - struct of filter

func NewDataGate

func NewDataGate[T, F interface{}](
	tableName string,
	primaryKey string,
	pool *pgxpool.Pool,
) (DataGate[T, F], error)

NewDataGate creates a new DataGate

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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