pgdb

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2021 License: MIT Imports: 5 Imported by: 0

README

PGDB

PostgresSQL as a database while using go-pg as an ORM to manage it.

Usage

Create a new database:

db, err := pgdb.New("localhost:5432", "vanclief", "", "postgres")
if err != nil {
    panic("Could not create the database")
}

Query:

// First argument is an array of the Model you are attempting to obtain
// Second argument is an empty instance of the Model you are attempting to obtain
// Third argument is the SQL Query, which is inserted after a "WHERE" statement
// Optional: Fourth argument is the Limit of Rows to return
// Optional: Fifth argument is the Offset
pgdb.Query(&res, &user.User{}, `name = 'Franco' ORDER BY email DESC`, []string{"10", "5"})

Documentation

Index

Constants

View Source
const (
	// ETABLEEXISTS happens when a table already exists
	ETABLEEXISTS = "ERROR #42P07"
	// ENOROWS happens when no result was found
	ENOROWS = "pg: no rows in result set"
	// EMULTIPLEROWS happens when multiple results where found with QueryOne
	EMULTIPLEROWS = "pg: multiple rows in result set"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

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

DB defines a PostgreSQL database that will use go-pg as an ORM

func New

func New(address string, user string, password string, database string) (*DB, error)

New returns a new PG Database instance

func NewFromURL added in v0.4.1

func NewFromURL(url string) (*DB, error)

NewFromURL returns a new PG Database instance

func (*DB) CreateSchema

func (db *DB) CreateSchema(modelsList []interface{}, dropExisting bool) error

CreateSchema creates the database tables if dropExisting is set to true it will drop the current schema

func (*DB) CreateTable

func (db *DB) CreateTable(model interface{}) error

CreateTable creates a new table in the database

func (*DB) Delete

func (db *DB) Delete(m interfaces.Model) error

Delete removes an existing model from the database

func (*DB) DropTable

func (db *DB) DropTable(model interface{}) error

DropTable deletes the existing tables

func (*DB) Get

func (db *DB) Get(m interfaces.Model, ID interface{}) error

Get returns a single model from the database using its primary key

func (*DB) Insert

func (db *DB) Insert(m interfaces.Model) error

Insert adds a model into the database

func (*DB) Query

func (db *DB) Query(mList interface{}, model interfaces.Model, query []string) error

Query returns a list of models from the database that satisfy a Query, extra parameters in the Query allow for Limit and Offset

func (*DB) QueryOne

func (db *DB) QueryOne(m interfaces.Model, query string) error

QueryOne returns a single model from the database that satisfies a Query. The method will return an error if there is more than one result from the query

func (*DB) RawQuery added in v0.4.2

func (db *DB) RawQuery(mList interface{}, model interfaces.Model, rawQuery []string) error

RawQuery returns a list of models from the database that satisfy a Raw Query

func (*DB) Update

func (db *DB) Update(m interfaces.Model) error

Update changes an existing model from the database

Jump to

Keyboard shortcuts

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