pgorm

package module
v0.0.0-...-65cef84 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2019 License: BSD-2-Clause Imports: 22 Imported by: 0

README

go-pg-orm

Wrapper that simplifies use of Golang ORM with focus on PostgreSQL found at https://github.com/go-pg/pg

NOTE: Work in progress!

Installation

To use this tool, install it from code with make install, go install directly, or go get -u github.com/AndrewDonelson/go-pg-orm

Example:
type User struct {
    Id     int64
    Name   string
    Emails []string
}

func (u User) String() string {
    return fmt.Sprintf("User<%d %s %v>", u.Id, u.Name, u.Emails)
}

type Story struct {
    Id       int64
    Title    string
    AuthorId int64
    Author   *User
}

func (s Story) String() string {
    return fmt.Sprintf("Story<%d %s %s>", s.Id, s.Title, s.Author)
}


func main() {
	// Create models object
	models := *pgorm.Model.NewModel()
	err = models.Open()
	if err != nil {
		return err
	}
	
	// Register models
	models.Register(
		&models.User{},
		&models.Story{},
		// ... Register More models here ...
	)
	
  	//Do something with models
}

Documentation

Overview

Wrapper that simplifies use of Golang ORM with focus on PostgreSQL

Package pgorm implements Golang ORM with focus on PostgreSQL features and performance. See https://github.com/go-pg/pg

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateCertificate

func GenerateCertificate(host, destDir, organization string) error

Generate a self-signed X.509 certificate for a TLS server. Outputs to 'pgorm-cert.pem' and 'pgorm-key.pem' and will overwrite existing files.

host       = "", "Comma-separated hostnames and IPs to generate a certificate for"
validFrom  = "start-date", "", "Creation date formatted as Jan 1 15:04:05 2011"
validFor   = "duration", 365*24*time.Hour, "Duration that certificate is valid for"
isCA       = "ca", false, "whether this cert should be its own Certificate Authority"
rsaBits    = "rsa-bits", 2048, "Size of RSA key to generate. Ignored if --ecdsa-curve is set"
ecdsaCurve = "ecdsa-curve", "", "ECDSA curve to use to generate a key. Valid values are P224, P256 (recommended), P384, P521"

Types

type Database

type Database struct {
	DB  *pg.DB
	Log *log.Logger
}

Database implements the PostgreSQL ORM

func (*Database) Close

func (d *Database) Close()

Close closes the database client

func (*Database) CreateModel

func (d *Database) CreateModel(model interface{}) error

CreateModel attempts add the given model to database.

func (*Database) DeleteModel

func (d *Database) DeleteModel(model interface{}) error

DeleteModel attempts update the given model in the database.

func (*Database) DropTable

func (d *Database) DropTable(model interface{}) error

DropTable drop table from db

func (*Database) Error

func (d *Database) Error(where string, message string, err error)

Error logs a error event then continues execution

func (*Database) Fatal

func (d *Database) Fatal(where string, message string, err error)

Fatal logs a fatal error event then stops execution

func (*Database) GetAllModels

func (d *Database) GetAllModels(model interface{}) error

GetAllModels attempts retrieve all models based on the given model from the database.

func (*Database) GetAllWithCondition

func (d *Database) GetAllWithCondition(model interface{}, condition interface{}, args ...interface{}) error

GetAllWithCondition attempts retrieve all models based on the given model and condition from the database.

func (*Database) GetModel

func (d *Database) GetModel(model interface{}) error

GetModel attempts retrieve the given model from the database.

func (*Database) GetWithCondition

func (d *Database) GetWithCondition(model interface{}, condition interface{}, args ...interface{}) error

GetWithCondition attempts retrieve a model based on the given model and condition from the database.

func (*Database) Info

func (d *Database) Info(where string, message string)

Info logs a general information event then continues execution

func (*Database) SaveModel

func (d *Database) SaveModel(model interface{}) error

SaveModel attempts add the given model to database.

func (*Database) Success

func (d *Database) Success(where string, message string)

Success logs a successful event then continues execution

func (*Database) UpdateModel

func (d *Database) UpdateModel(model interface{}) error

UpdateModel attempts update the given model in the database.

func (*Database) Warn

func (d *Database) Warn(where string, message string)

Warn logs a warning event then continues execution

type IChange

type IChange interface {
	SaveModel(model interface{}) error
	UpdateModel(model interface{}) error
	DeleteModel(model interface{}) error
	CreateModel(model interface{}) error
	DropTable(model interface{}) error
}

IChange defines the change related methods

type IClose

type IClose interface {
	Close()
}

IConn defines the connection related methods

type IDatabase

type IDatabase interface {
	IClose
	IGet
	IChange
	ILogger
}

IDatabase defines the top level Database methods

func NewDatabase

func NewDatabase(db *pg.DB, log *log.Logger) IDatabase

NewDatabase - create instance of Database

type IGet

type IGet interface {
	GetModel(model interface{}) error
	GetAllModels(model interface{}) error
	GetWithCondition(model interface{}, condition interface{}, args ...interface{}) error
	GetAllWithCondition(model interface{}, condition interface{}, args ...interface{}) error
}

IGet defines the get related methods

type ILogger

type ILogger interface {
	Success(where string, message string)
	Info(where string, message string)
	Warn(where string, message string)
	Error(where string, message string, err error)
	Fatal(where string, message string, err error)
}

ILogger defines the logging related methods

type Model

type Model struct {
	IDatabase

	Migrate bool
	Drop    bool
	// contains filtered or unexported fields
}

Model facilitate database interactions, supports postgres, mysql and foundation

func NewDB

func NewDB(dbHost, dbUser, dbPass, dbName string, secured, migrate, droptables bool, models ...interface{}) (*Model, error)

func NewModel

func NewModel(migrate, dropTable bool) *Model

NewModel returns a new Model without opening database connection

func (*Model) AutoMigrateAll

func (m *Model) AutoMigrateAll() error

AutoMigrateAll runs migrations for all the registered models

func (*Model) Count

func (m *Model) Count() int

Count returns the number of registered models

func (*Model) DropTables

func (m *Model) DropTables() error

DropTables Drops All Model Database Tables

func (*Model) IsOpen

func (m *Model) IsOpen() bool

IsOpen returns true if the Model has already established connection to the database

func (*Model) OpenWithConfig

func (m *Model) OpenWithConfig(user, database, password string, cfg []byte) error

OpenWithConfig - opens database connection with the incoming settings, if bad cfg income - use default cfg

func (*Model) OpenWithDefault

func (m *Model) OpenWithDefault(user, database, password string) error

OpenWithConfig - opens database connection with the incoming settings, if bad cfg income - use default cfg

func (*Model) Register

func (m *Model) Register(values ...interface{}) error

Register adds the values to the models registry

Directories

Path Synopsis
web

Jump to

Keyboard shortcuts

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