orm

package
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2022 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SILENT = iota
	WARN
	INFO
	ERROR
)
View Source
const MemorySQLiteDB = "file::memory:"

Variables

View Source
var (
	// The model passed in is not a pointer
	ErrNotPointer = errors.New("not a valid pointer")

	// An update query affected 0 rows
	ErrNoRecordsUpdated = errors.New("no records updated")
)

Functions

func ConnectToPostgres

func ConnectToPostgres(config Config) (*gorm.DB, error)

Connect to the postgres database with the data source name.

func ConnectToSqlite3

func ConnectToSqlite3(dbname string, walMode bool) *gorm.DB

Connect to dbname. If dbname is nil, it connect to a memory sqlite database ForeignKey pragma is enabled by for all connections

func IsPointer

func IsPointer(v any) bool

validates that v is a pointer

func MigrateViewsFunctionsAndTriggers

func MigrateViewsFunctionsAndTriggers(db *gorm.DB, database, user string) (output []byte, err error)

Drops all views, functions, triggers Works only for postgres

WARNING: DO NOT run on in tests on a production database unless if doing actual migrations like dropping and re-creating all views, functions and triggers.

func NewLogger

func NewLogger(logLevel SqlLogLevel, w io.Writer) logger.Interface

Creates a new SQL logger for gorm and that will write to w

func ParseDSN

func ParseDSN(dsn string, params *DSNParamas)

parse postgres DSN into DSNParams struct

func Ping

func Ping(db *gorm.DB) error

func WriteDropFunctionsQueries

func WriteDropFunctionsQueries(db *gorm.DB, w *bufio.Writer) error

writes sql statements to drop all postgres functions/procedures to w Works only for postgres

func WriteDropTriggerQueries

func WriteDropTriggerQueries(db *gorm.DB, w *bufio.Writer) error

writes sql statements to drop all views to w.

Execute with psql since the postgres driver does not support multiple statements.

this is important for migrations

func WriteDropViewQueries

func WriteDropViewQueries(db *gorm.DB, w *bufio.Writer) error

writes sql statements to drop all views to w.

Works only for postgres

Types

type Condition

type Condition interface {
	Apply(db *gorm.DB) *gorm.DB
}

Interface that applies some operation to the GORM DB definition Multiple conditions are applied in the order specified. Select, Join, Where, Group, Having

type Config

type Config struct {
	// Connection data source name
	DSN string

	// gorm's sql logger.Interface. Create one with helper gowrap.NewLogger
	Logger logger.Interface

	// Use a connection pool.
	// SetMaxIdleConns(20), SetMaxOpenConns(200)
	UseConnPool bool
}

Configuration struct for connecting to postgres database

type DSNParamas

type DSNParamas struct {
	Database string // dbname
	User     string // user
	Password string // password, default ""
	Host     string // host, default: localhost
	Port     string // postgres port, default 5432
	SSLMode  string // ssl_mode, default=disabled
	Timezone string // Timezone
}

type Group

type Group struct {
	Name string // grouping condition e.g "category"
}

Add grouping. Group should apear after Join but before Where conditions

func (Group) Apply

func (g Group) Apply(db *gorm.DB) *gorm.DB

type Join

type Join struct {
	Query string
	Args  []any
}

func (Join) Apply

func (j Join) Apply(db *gorm.DB) *gorm.DB

type Limit

type Limit struct {
	L int
	O int
}

Add grouping. Group should apear after Join but before Where conditions

func (Limit) Apply

func (g Limit) Apply(db *gorm.DB) *gorm.DB

type ORM

type ORM interface {
	Insert(v any) error
	Update(v any) error
	PartialUpdate(model any, updates any, where Where) error
	Delete(v any, conditions ...Condition) error
	First(v any, id uint, conditions ...Condition) error
	FindOne(v any, where Where, conditions ...Condition) error
	FindAll(slicePtr any, conditions ...Condition) error
	DB() *gorm.DB
}

func New

func New(db *gorm.DB) ORM

type Order

type Order struct {
	Name string // grouping condition e.g "category DESC"
}

Add grouping. Group should apear after Join but before Where conditions

func (Order) Apply

func (g Order) Apply(db *gorm.DB) *gorm.DB

type PaginatedResult

type PaginatedResult[T any] struct {
	Page       int  // Current page
	Limit      int  // Page size
	HasNext    bool // if there is a next page
	HasPrev    bool // is there is a previous page
	Count      int  // total number of records in the database.
	TotalPages int  // total number of pages (based on Count)
	Results    []T  // Slice of the query results
}

Represents a paginated query result based of limit/offset pagination

func Paginate

func Paginate[T any](table *T, page int, limit int, db *gorm.DB, conditions ...Condition) (PaginatedResult[T], error)

type Preload

type Preload struct {
	// Main reload string e.g "Orders.Products" or "Users"
	Query string

	// condition for preloading e.g  []any{"state NOT IN (?)", "cancelled"}
	Args []any
}

Preload relationships.

if your struct has no nested relationships, you can simply pass clause.Associations as the query.

func (Preload) Apply

func (p Preload) Apply(db *gorm.DB) *gorm.DB

type Select

type Select struct {
	Fields []any
}

func (Select) Apply

func (j Select) Apply(db *gorm.DB) *gorm.DB

type SqlLogLevel

type SqlLogLevel int

type Where

type Where struct {
	// where add condition e.g "username=? and password=?"
	Query string

	// arguments to the query e.g []any{"johndoe", "passwordhash"}
	Args []any
}

Filter query results based on the where string

func (Where) Apply

func (p Where) Apply(db *gorm.DB) *gorm.DB

Jump to

Keyboard shortcuts

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