gpostgres

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 31, 2020 License: MIT Imports: 6 Imported by: 0

README

gpostgres

A wrapper around GORM to easily connect to Postgres.

Types
  • JustID: Contains a UUID primary key.
  • Base: Base model type that contains a primary key and timestamps for create, update, and soft delete.
  • Database: Contains helper methods for running Postgres operations.
Methods
  • GetPostgres: Returns a Database.
  • Database.Create: Creates the given object.
  • Database.Read: Reads the given ID into the provided target.
  • Database.ReadWhere: Reads a single query result into the provided target.
  • Database.ReadAll: Reads all rows of a table into the provided target slice.
  • Database.ReadAllWhere: Reads all rows of a query into the provided target slice.
  • Database.PaginatedReadAllWhere: Reads a page of rows of a query into the provided target slice, also returns the total count.
  • Database.Update: Updates the given object.
  • Database.Delete: Deletes the given object ID.
  • Database.DeleteWhere: Deletes all rows for the given query.
  • Database.TransactionBegin: Returns a transaction scope.
  • Database.TransactionRollback: Cancels a transaction.
  • Database.TransactionCommit: Commits a transaction.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Base

type Base struct {
	JustID
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt *time.Time
}

Base Model that contains create, update, and soft delete times along with a UUID primary key.

func (*Base) BeforeCreate

func (base *Base) BeforeCreate(scope *gorm.Scope) error

BeforeCreate sets a UUID rather than numeric ID.

type Database

type Database struct {
	DB *gorm.DB
}

Database is a wrapper around GORM.

func GetPostgres

func GetPostgres(postgresURL string, sandbox bool, logger gsl.Log) (*Database, error)

GetPostgres will return a Database.

func (*Database) Create

func (database *Database) Create(transaction *gorm.DB, object interface{}) error

Create inserts a new row.

func (*Database) Delete

func (database *Database) Delete(transaction *gorm.DB, target interface{}, objectID uuid.UUID) error

Delete will delete an existing row by it's objectID.

func (*Database) DeleteWhere

func (database *Database) DeleteWhere(transaction *gorm.DB, target interface{}, where ...interface{}) error

DeleteWhere will delete all existing rows by a SQL search.

func (*Database) GetCursor

func (database *Database) GetCursor(transaction *gorm.DB) *gorm.DB

GetCursor will return the raw GORM cursor.

func (*Database) PaginatedReadAllWhere

func (database *Database) PaginatedReadAllWhere(transaction *gorm.DB, target interface{}, orderBy string, selectKeys string, preloads []string, page int, size int, where ...interface{}) (int, error)

PaginatedReadAllWhere finds all rows by a SQL search that is limited and offset. Also returns the row count.

func (*Database) Read

func (database *Database) Read(transaction *gorm.DB, target interface{}, orderBy string, selectKeys string, preloads []string, objectID uuid.UUID) error

Read finds a row by it's objectID.

func (*Database) ReadAll

func (database *Database) ReadAll(transaction *gorm.DB, target interface{}, orderBy string, selectKeys string, preloads []string) error

ReadAll finds all rows.

func (*Database) ReadAllWhere

func (database *Database) ReadAllWhere(transaction *gorm.DB, target interface{}, orderBy string, selectKeys string, preloads []string, where ...interface{}) error

ReadAllWhere finds all rows by a SQL search.

func (*Database) ReadWhere

func (database *Database) ReadWhere(transaction *gorm.DB, target interface{}, orderBy string, selectKeys string, preloads []string, where ...interface{}) error

ReadWhere finds a row by a SQL search.

func (*Database) TransactionBegin

func (database *Database) TransactionBegin() *gorm.DB

TransactionBegin will begin a transaction.

func (*Database) TransactionCommit

func (database *Database) TransactionCommit(transaction *gorm.DB) error

TransactionCommit will attempt to commit a transaction.

func (*Database) TransactionRollback

func (database *Database) TransactionRollback(transaction *gorm.DB)

TransactionRollback will rollback a transaction.

func (*Database) Update

func (database *Database) Update(transaction *gorm.DB, object interface{}) error

Update will update an existing row.

type JustID

type JustID struct {
	ID uuid.UUID `sql:"type:uuid;primary_key;"`
}

JustID contains only a UUID primary key, ID.

Jump to

Keyboard shortcuts

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