pgs

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2024 License: MIT Imports: 12 Imported by: 0

README

PGS - PostgreSQL Go with model query

Go PostgreSQL

pgs is easy-to-use library for interacting with PostgreSQL databases in Go, built on top of the robust foundations of goqu, pgxscan, and pgx. It provides a high-level abstraction for building SQL queries, handling transactions, and managing database connections with ease.

Features

  • Simple API: Intuitive and easy-to-use API for common database operations.
  • Transaction Support: Built-in support for managing database transactions.
  • Query Building: Flexible query building with support for complex queries and subqueries.
  • Connection Pooling: Efficient connection pooling using pgxpool.
  • Type Safety: Strong type safety for database interactions.

Docs

Installation

To install PGS, use the following command:

go get github.com/kvorange/pgs

Quick start

Quick Start Here's a quick example to get you started with pgs:

package main

import (
    "context"
    "github.com/jackc/pgx/v5/pgtype"
    "github.com/kvorange/pgs"
    "log"
)

// User simple table for example
type User struct {
    pgs.Model `table:"user"`
    
    Id       pgs.Field[pgtype.Int8]        `json:"id"`
    Login    pgs.Field[pgtype.Text]        `json:"login"`
    Name     pgs.Field[pgtype.Text]        `json:"name"`
    CreateAt pgs.Field[pgtype.Timestamptz] `json:"create_at"`
    IsAdmin  pgs.Field[pgtype.Bool]        `json:"is_admin"`
}

func main() {
    // Db config settings
    dbConfig := pgs.DbConfig{
        Host:      "localhost",
        Port:      5432,
        User:      "user",
        Password:  "password",
        Name:      "dbname",
        PollCount: 10,
    }
    
    // Init db connection
    dbClient := pgs.DbClient{}
    err := dbClient.Connect(context.Background(), dbConfig)
    if err != nil {
        log.Fatalf("Failed to connect to database: %v", err)
    }
    
    // Init user model. Use this model for all you operations with user table
    var userModel User
    err = userModel.Init(&dbClient, &userModel)
    if err != nil {
        log.Fatalf("Failed to init model User: %v", err)
    }
    
    // Select all users for example
    var users []User
    err = userModel.Select().Scan(&users)
    if err != nil {
        log.Fatalf("Failed to select users: %v", err)
    }
    
    // Your database operations here
}

Acknowledgments

Thanks to the Go community for their support and contributions.

Inspired by the need for a simple and powerful PostgreSQL library in Go, leveraging the strengths of goqu, pgxscan, and pgx.

License

pgs is released under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Condition

type Condition struct {
	Field fieldI
	Op    string
	Value interface{}
	// contains filtered or unexported fields
}

func (Condition) Condition

func (c Condition) Condition(inUpdate bool) (exp.Expression, error)

type Conditional

type Conditional interface {
	Condition(inUpdate bool) (goqu.Expression, error)
	// contains filtered or unexported methods
}

type CountExpression

type CountExpression struct {
	// contains filtered or unexported fields
}

func Count

func Count(field fieldI) CountExpression

func (CountExpression) As

type DbClient

type DbClient struct {
	Ctx  context.Context
	Pool *pgxpool.Pool
}

func (*DbClient) Connect

func (cli *DbClient) Connect(ctx context.Context, cfg DbConfig) error

type DbConfig

type DbConfig struct {
	Host      string
	Port      int
	User      string
	Password  string
	Name      string
	PollCount int32
}

type DeleteDataset

type DeleteDataset struct {
	// contains filtered or unexported fields
}

func (*DeleteDataset) Exec

func (d *DeleteDataset) Exec() error

func (*DeleteDataset) Query

func (d *DeleteDataset) Query() string

func (*DeleteDataset) Returning

func (d *DeleteDataset) Returning(fields ...fieldI) *DeleteDataset

func (*DeleteDataset) Scan

func (d *DeleteDataset) Scan(dst interface{}) error

func (*DeleteDataset) ScanOne

func (d *DeleteDataset) ScanOne(dst interface{}) error

func (*DeleteDataset) Where

func (d *DeleteDataset) Where(conditions ...Conditional) *DeleteDataset

func (*DeleteDataset) WithTx

func (d *DeleteDataset) WithTx(tx pgx.Tx) *DeleteDataset

type Field

type Field[T any] struct {
	Value T
	// contains filtered or unexported fields
}

func (*Field[T]) As

func (f *Field[T]) As(as string) *Field[T]

func (*Field[T]) Eq

func (f *Field[T]) Eq(value interface{}) Condition

func (*Field[T]) Gt

func (f *Field[T]) Gt(value interface{}) Condition

func (*Field[T]) Gte

func (f *Field[T]) Gte(value interface{}) Condition

func (*Field[T]) In

func (f *Field[T]) In(value interface{}) Condition

func (*Field[T]) IsNotNull

func (f *Field[T]) IsNotNull() Condition

func (*Field[T]) IsNull

func (f *Field[T]) IsNull() Condition

func (*Field[T]) Like

func (f *Field[T]) Like(value interface{}) Condition

func (*Field[T]) Lt

func (f *Field[T]) Lt(value interface{}) Condition

func (*Field[T]) Lte

func (f *Field[T]) Lte(value interface{}) Condition

func (Field[T]) MarshalJSON

func (f Field[T]) MarshalJSON() ([]byte, error)

func (*Field[T]) NotEq

func (f *Field[T]) NotEq(value interface{}) Condition

func (*Field[T]) NotIn

func (f *Field[T]) NotIn(value interface{}) Condition

func (*Field[T]) NotLike

func (f *Field[T]) NotLike(value interface{}) Condition

func (*Field[T]) NotRegex

func (f *Field[T]) NotRegex(value interface{}) Condition

func (*Field[T]) NotRegexI

func (f *Field[T]) NotRegexI(value interface{}) Condition

func (*Field[T]) Regex

func (f *Field[T]) Regex(value interface{}) Condition

func (*Field[T]) RegexI added in v0.0.4

func (f *Field[T]) RegexI(value interface{}) Condition

func (*Field[T]) Scan

func (f *Field[T]) Scan(src interface{}) error

type Identifiable

type Identifiable interface {
	exp.Inable
	exp.Comparable
	exp.Likeable
	exp.Isable
}

type InsertDataset

type InsertDataset struct {
	// contains filtered or unexported fields
}

func (*InsertDataset) Exec

func (d *InsertDataset) Exec() error

func (*InsertDataset) Query

func (d *InsertDataset) Query() string

func (*InsertDataset) Returning

func (d *InsertDataset) Returning(fields ...fieldI) *InsertDataset

func (*InsertDataset) Scan

func (d *InsertDataset) Scan(dst interface{}) error

func (*InsertDataset) ScanOne

func (d *InsertDataset) ScanOne(dst interface{}) error

func (*InsertDataset) WithTx

func (d *InsertDataset) WithTx(tx pgx.Tx) *InsertDataset

type LiteralExpression added in v0.0.6

type LiteralExpression struct {
	// contains filtered or unexported fields
}

func L added in v0.0.6

func L(sql string, values ...interface{}) LiteralExpression

func (LiteralExpression) As added in v0.0.6

func (LiteralExpression) Condition added in v0.0.6

func (l LiteralExpression) Condition(inUpdate bool) (goqu.Expression, error)

type Model

type Model struct {
	// contains filtered or unexported fields
}

func (*Model) Delete

func (m *Model) Delete() *DeleteDataset

func (*Model) Init

func (m *Model) Init(db *DbClient, model interface{}) error

func (*Model) Insert

func (m *Model) Insert(records ...Record) *InsertDataset

func (*Model) Select

func (m *Model) Select(fields ...Selectable) *SelectDataset

func (*Model) Update

func (m *Model) Update(record Record) *UpdateDataset

type OrCondition

type OrCondition struct {
	Conditions []Condition
}

func Or

func Or(conditions ...Condition) OrCondition

func (OrCondition) Condition

func (oe OrCondition) Condition(inUpdate bool) (exp.Expression, error)

type Ordered

type Ordered interface {
	// contains filtered or unexported methods
}

type Record

type Record map[fieldI]interface{}

type SelectDataset

type SelectDataset struct {
	// contains filtered or unexported fields
}

func (*SelectDataset) Limit

func (sd *SelectDataset) Limit(limit uint) *SelectDataset

func (*SelectDataset) Offset

func (sd *SelectDataset) Offset(offset uint) *SelectDataset

func (*SelectDataset) OrderAsc

func (sd *SelectDataset) OrderAsc(fields ...Ordered) *SelectDataset

func (*SelectDataset) OrderDesc

func (sd *SelectDataset) OrderDesc(fields ...Ordered) *SelectDataset

func (*SelectDataset) Query

func (sd *SelectDataset) Query() string

func (*SelectDataset) Scan

func (sd *SelectDataset) Scan(dst interface{}) error

func (*SelectDataset) ScanOne

func (sd *SelectDataset) ScanOne(dst interface{}) error

func (*SelectDataset) Where

func (sd *SelectDataset) Where(conditions ...Conditional) *SelectDataset

func (*SelectDataset) WithTx

func (sd *SelectDataset) WithTx(tx pgx.Tx) *SelectDataset

type Selectable

type Selectable interface {
	// contains filtered or unexported methods
}

type UpdateDataset

type UpdateDataset struct {
	// contains filtered or unexported fields
}

func (*UpdateDataset) Exec

func (d *UpdateDataset) Exec() error

func (*UpdateDataset) Query

func (d *UpdateDataset) Query() string

func (*UpdateDataset) Returning

func (d *UpdateDataset) Returning(fields ...fieldI) *UpdateDataset

func (*UpdateDataset) Scan

func (d *UpdateDataset) Scan(dst interface{}) error

func (*UpdateDataset) ScanOne

func (d *UpdateDataset) ScanOne(dst interface{}) error

func (*UpdateDataset) Where

func (d *UpdateDataset) Where(conditions ...Conditional) *UpdateDataset

func (*UpdateDataset) WithTx

func (d *UpdateDataset) WithTx(tx pgx.Tx) *UpdateDataset

Jump to

Keyboard shortcuts

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