ipop

package module
v0.0.0-...-dcb4996 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2018 License: Apache-2.0 Imports: 2 Imported by: 0

README

ipop

This project allows you to abstract away to struct types used in github.com/gobuffalo/pop.

Motivation

This allows you to abstract away the real pop package with your own implementation, whether for some new backend or just for mocking in your unit tests.

Build status

Build Status codecov

Installation

go get github.com/dnnrly/ipop

Or if you're using modules then just import it in your code.

API Reference

This aims to be API compatible with pop.

Tests

Run tests by using the command:

make test

How to use?

See the godoc examples.

Contribute

Please see the contributing guideline.

Credits

This is basically just wrapping up pop. All the praise goes to them - and then some.

License

Apache2 © Pascal Dennerly

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Connection

type Connection interface {
	String() string
	// URL returns the datasource connection string
	URL() string
	// MigrationURL returns the datasource connection string used for running the migrations
	MigrationURL() string
	// MigrationTableName returns the name of the table to track migrations
	MigrationTableName() string
	// Open creates a new datasource connection
	Open() error
	// Close destroys an active datasource connection
	Close() error
	// Transaction will start a new transaction on the connection. If the inner function
	// returns an error then the transaction will be rolled back, otherwise the transaction
	// will automatically commit at the end.
	Transaction(fn func(tx Connection) error) error
	// NewTransaction starts a new transaction on the connection
	NewTransaction() (Connection, error)
	// Rollback will open a new transaction and automatically rollback that transaction
	// when the inner function returns, regardless. This can be useful for tests, etc.
	Rollback(fn func(tx Connection)) error
	// Q creates a new "empty" query for the current connection.
	Q() *pop.Query
	// TruncateAll truncates all data from the datasource
	TruncateAll() error

	// BelongsTo adds a "where" clause based on the "ID" of the
	// "model" passed into it.
	BelongsTo(model interface{}) *pop.Query
	// BelongsToAs adds a "where" clause based on the "ID" of the
	// "model" passed into it using an alias.
	BelongsToAs(model interface{}, as string) *pop.Query
	// BelongsToThrough adds a "where" clause that connects the "bt" model
	// through the associated "thru" model.
	BelongsToThrough(bt, thru interface{}) *pop.Query

	// Reload fetch fresh data for a given model, using its ID.
	Reload(model interface{}) error
	// ValidateAndSave applies validation rules on the given entry, then save it
	// if the validation succeed, excluding the given columns.
	ValidateAndSave(model interface{}, excludeColumns ...string) (*validate.Errors, error)
	// Save wraps the Create and Update methods. It executes a Create if no ID is provided with the entry;
	// or issues an Update otherwise.
	Save(model interface{}, excludeColumns ...string) error
	// ValidateAndCreate applies validation rules on the given entry, then creates it
	// if the validation succeed, excluding the given columns.
	ValidateAndCreate(model interface{}, excludeColumns ...string) (*validate.Errors, error)
	// Create add a new given entry to the database, excluding the given columns.
	// It updates `created_at` and `updated_at` columns automatically.
	Create(model interface{}, excludeColumns ...string) error
	// ValidateAndUpdate applies validation rules on the given entry, then update it
	// if the validation succeed, excluding the given columns.
	ValidateAndUpdate(model interface{}, excludeColumns ...string) (*validate.Errors, error)
	// Update writes changes from an entry to the database, excluding the given columns.
	// It updates the `updated_at` column automatically.
	Update(model interface{}, excludeColumns ...string) error
	// Destroy deletes a given entry from the database
	Destroy(model interface{}) error

	// Find the first record of the model in the database with a particular id.
	//
	//	c.Find(&User{}, 1)
	Find(model interface{}, id interface{}) error
	// First record of the model in the database that matches the query.
	//
	//	c.First(&User{})
	First(model interface{}) error
	// Last record of the model in the database that matches the query.
	//
	//	c.Last(&User{})
	Last(model interface{}) error
	// All retrieves all of the records in the database that match the query.
	//
	//	c.All(&[]User{})
	All(models interface{}) error
	// Load loads all association or the fields specified in params for
	// an already loaded model.
	//
	// tx.First(&u)
	// tx.Load(&u)
	Load(model interface{}, fields ...string) error
	// Count the number of records in the database.
	//
	//	c.Count(&User{})
	Count(model interface{}) (int, error)
	// Select allows to query only fields passed as parameter.
	// c.conn.Select("field1", "field2").All(&model)
	// => SELECT field1, field2 FROM models
	Select(fields ...string) *pop.Query

	// MigrateUp is deprecated, and will be removed in a future version. Use FileMigrator#Up instead.
	MigrateUp(path string) error
	// MigrateDown is deprecated, and will be removed in a future version. Use FileMigrator#Down instead.
	MigrateDown(path string, step int) error
	// MigrateStatus is deprecated, and will be removed in a future version. Use FileMigrator#Status instead.
	MigrateStatus(path string) error
	// MigrateReset is deprecated, and will be removed in a future version. Use FileMigrator#Reset instead.
	MigrateReset(path string) error

	// Paginate records returned from the database.
	//
	//	return c.conn.Paginate(2, 15)
	//	q.All(&[]User{})
	//	q.Paginator
	Paginate(page int, perPage int) *pop.Query
	// PaginateFromParams paginates records returned from the database.
	//
	//	return c.conn.PaginateFromParams(req.URL.Query())
	//	q.All(&[]User{})
	//	q.Paginator
	PaginateFromParams(params pop.PaginationParams) *pop.Query

	// RawQuery will override the query building feature of Pop and will use
	// whatever query you want to execute against the `Connection`. You can continue
	// to use the `?` argument syntax.
	//
	//	c.RawQuery("select * from foo where id = ?", 1)
	RawQuery(stmt string, args ...interface{}) *pop.Query
	// Eager will enable load associations of the model.
	// by defaults loads all the associations on the model,
	// but can take a variadic list of associations to load.
	//
	// 	c.Eager().Find(model, 1) // will load all associations for model.
	// 	c.Eager("Books").Find(model, 1) // will load only Book association for model.
	Eager(fields ...string) Connection
	// Where will append a where clause to the query. You may use `?` in place of
	// arguments.
	//
	// 	c.Where("id = ?", 1)
	// 	q.Where("id in (?)", 1, 2, 3)
	Where(stmt string, args ...interface{}) *pop.Query
	// Order will append an order clause to the query.
	//
	// 	c.Order("name desc")
	Order(stmt string) *pop.Query
	// Limit will add a limit clause to the query.
	Limit(limit int) *pop.Query

	// Scope the query by using a `ScopeFunc`
	//
	//	func ByName(name string) ScopeFunc {
	//		return func(q Query) Query {
	//			return q.Where("name = ?", name)
	//		}
	//	}
	//
	//	func WithDeleted(q *pop.Query) *pop.Query {
	//		return q.Where("deleted_at is null")
	//	}
	//
	//	c.Scope(ByName("mark)).Scope(WithDeleted).First(&User{})
	Scope(sf pop.ScopeFunc) *pop.Query
}

Connection represents a Buffalo pop connection to a database

type ConnectionAdapter

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

func NewConnectionAdapter

func NewConnectionAdapter(c *pop.Connection) *ConnectionAdapter
Example
popConnection, _ := pop.Connect("test")
adapted := NewConnectionAdapter(popConnection)
adapted.String() // Use it as you would *pop.Connection
Output:

func (*ConnectionAdapter) All

func (c *ConnectionAdapter) All(models interface{}) error

All retrieves all of the records in the database that match the query.

c.All(&[]User{})

func (*ConnectionAdapter) BelongsTo

func (c *ConnectionAdapter) BelongsTo(model interface{}) *pop.Query

BelongsTo adds a "where" clause based on the "ID" of the "model" passed into it.

func (*ConnectionAdapter) BelongsToAs

func (c *ConnectionAdapter) BelongsToAs(model interface{}, as string) *pop.Query

BelongsToAs adds a "where" clause based on the "ID" of the "model" passed into it using an alias.

func (*ConnectionAdapter) BelongsToThrough

func (c *ConnectionAdapter) BelongsToThrough(bt interface{}, thru interface{}) *pop.Query

BelongsToThrough adds a "where" clause that connects the "bt" model through the associated "thru" model.

func (*ConnectionAdapter) Close

func (c *ConnectionAdapter) Close() error

Close destroys an active datasource connection

func (*ConnectionAdapter) Count

func (c *ConnectionAdapter) Count(model interface{}) (int, error)

Count the number of records in the database.

c.Count(&User{})

func (*ConnectionAdapter) Create

func (c *ConnectionAdapter) Create(model interface{}, excludeColumns ...string) error

Create add a new given entry to the database, excluding the given columns. It updates `created_at` and `updated_at` columns automatically.

func (*ConnectionAdapter) Destroy

func (c *ConnectionAdapter) Destroy(model interface{}) error

Destroy deletes a given entry from the database

func (*ConnectionAdapter) Eager

func (c *ConnectionAdapter) Eager(fields ...string) Connection

Eager will enable load associations of the model. by defaults loads all the associations on the model, but can take a variadic list of associations to load.

c.Eager().Find(model, 1) // will load all associations for model.
c.Eager("Books").Find(model, 1) // will load only Book association for model.

func (*ConnectionAdapter) Find

func (c *ConnectionAdapter) Find(model interface{}, id interface{}) error

Find the first record of the model in the database with a particular id.

c.Find(&User{}, 1)

func (*ConnectionAdapter) First

func (c *ConnectionAdapter) First(model interface{}) error

First record of the model in the database that matches the query.

c.First(&User{})

func (*ConnectionAdapter) Last

func (c *ConnectionAdapter) Last(model interface{}) error

Last record of the model in the database that matches the query.

c.Last(&User{})

func (*ConnectionAdapter) Limit

func (c *ConnectionAdapter) Limit(limit int) *pop.Query

Limit will add a limit clause to the query.

func (*ConnectionAdapter) Load

func (c *ConnectionAdapter) Load(model interface{}, fields ...string) error

Load loads all association or the fields specified in params for an already loaded model.

tx.First(&u) tx.Load(&u)

func (*ConnectionAdapter) MigrateDown

func (c *ConnectionAdapter) MigrateDown(path string, step int) error

MigrateDown is deprecated, and will be removed in a future version. Use FileMigrator#Down instead.

func (*ConnectionAdapter) MigrateReset

func (c *ConnectionAdapter) MigrateReset(path string) error

MigrateReset is deprecated, and will be removed in a future version. Use FileMigrator#Reset instead.

func (*ConnectionAdapter) MigrateStatus

func (c *ConnectionAdapter) MigrateStatus(path string) error

MigrateStatus is deprecated, and will be removed in a future version. Use FileMigrator#Status instead.

func (*ConnectionAdapter) MigrateUp

func (c *ConnectionAdapter) MigrateUp(path string) error

MigrateUp is deprecated, and will be removed in a future version. Use FileMigrator#Up instead.

func (*ConnectionAdapter) MigrationTableName

func (c *ConnectionAdapter) MigrationTableName() string

MigrationTableName returns the name of the table to track migrations

func (*ConnectionAdapter) MigrationURL

func (c *ConnectionAdapter) MigrationURL() string

MigrationURL returns the datasource connection string used for running the migrations

func (*ConnectionAdapter) NewTransaction

func (c *ConnectionAdapter) NewTransaction() (Connection, error)

NewTransaction starts a new transaction on the connection

func (*ConnectionAdapter) Open

func (c *ConnectionAdapter) Open() error

Open creates a new datasource connection

func (*ConnectionAdapter) Order

func (c *ConnectionAdapter) Order(stmt string) *pop.Query

Order will append an order clause to the query.

c.Order("name desc")

func (*ConnectionAdapter) Paginate

func (c *ConnectionAdapter) Paginate(page int, perPage int) *pop.Query

Paginate records returned from the database.

return c.conn.Paginate(2, 15)
q.All(&[]User{})
q.Paginator

func (*ConnectionAdapter) PaginateFromParams

func (c *ConnectionAdapter) PaginateFromParams(params pop.PaginationParams) *pop.Query

PaginateFromParams paginates records returned from the database.

return c.conn.PaginateFromParams(req.URL.Query())
q.All(&[]User{})
q.Paginator

func (*ConnectionAdapter) Q

func (c *ConnectionAdapter) Q() *pop.Query

Q creates a new "empty" query for the current connection.

func (*ConnectionAdapter) RawQuery

func (c *ConnectionAdapter) RawQuery(stmt string, args ...interface{}) *pop.Query

RawQuery will override the query building feature of Pop and will use whatever query you want to execute against the `Connection`. You can continue to use the `?` argument syntax.

c.RawQuery("select * from foo where id = ?", 1)]

func (*ConnectionAdapter) Reload

func (c *ConnectionAdapter) Reload(model interface{}) error

Reload fetch fresh data for a given model, using its ID.

func (*ConnectionAdapter) Rollback

func (c *ConnectionAdapter) Rollback(fn func(tx Connection)) error

Rollback will open a new transaction and automatically rollback that transaction when the inner function returns, regardless. This can be useful for tests, etc.

func (*ConnectionAdapter) Save

func (c *ConnectionAdapter) Save(model interface{}, excludeColumns ...string) error

Save wraps the Create and Update methods. It executes a Create if no ID is provided with the entry; or issues an Update otherwise.

func (*ConnectionAdapter) Scope

func (c *ConnectionAdapter) Scope(sf pop.ScopeFunc) *pop.Query

Scope the query by using a `ScopeFunc`

func ByName(name string) ScopeFunc {
	return func(q Query) Query {
		return q.Where("name = ?", name)
	}
}

func WithDeleted(q *pop.Query) *pop.Query {
	return q.Where("deleted_at is null")
}

c.Scope(ByName("mark)).Scope(WithDeleted).First(&User{})

func (*ConnectionAdapter) Select

func (c *ConnectionAdapter) Select(fields ...string) *pop.Query

Select allows to query only fields passed as parameter. c.conn.Select("field1", "field2").All(&model) => SELECT field1, field2 FROM models

func (*ConnectionAdapter) String

func (c *ConnectionAdapter) String() string

func (*ConnectionAdapter) Transaction

func (c *ConnectionAdapter) Transaction(fn func(tx Connection) error) error

Transaction will start a new transaction on the connection. If the inner function returns an error then the transaction will be rolled back, otherwise the transaction will automatically commit at the end.

func (*ConnectionAdapter) TruncateAll

func (c *ConnectionAdapter) TruncateAll() error

TruncateAll truncates all data from the datasource

func (*ConnectionAdapter) URL

func (c *ConnectionAdapter) URL() string

URL returns the datasource connection string

func (*ConnectionAdapter) Update

func (c *ConnectionAdapter) Update(model interface{}, excludeColumns ...string) error

Update writes changes from an entry to the database, excluding the given columns. It updates the `updated_at` column automatically.

func (*ConnectionAdapter) ValidateAndCreate

func (c *ConnectionAdapter) ValidateAndCreate(model interface{}, excludeColumns ...string) (*validate.Errors, error)

ValidateAndCreate applies validation rules on the given entry, then creates it if the validation succeed, excluding the given columns.

func (*ConnectionAdapter) ValidateAndSave

func (c *ConnectionAdapter) ValidateAndSave(model interface{}, excludeColumns ...string) (*validate.Errors, error)

ValidateAndSave applies validation rules on the given entry, then save it if the validation succeed, excluding the given columns.

func (*ConnectionAdapter) ValidateAndUpdate

func (c *ConnectionAdapter) ValidateAndUpdate(model interface{}, excludeColumns ...string) (*validate.Errors, error)

ValidateAndUpdate applies validation rules on the given entry, then update it if the validation succeed, excluding the given columns.

func (*ConnectionAdapter) Where

func (c *ConnectionAdapter) Where(stmt string, args ...interface{}) *pop.Query

Where will append a where clause to the query. You may use `?` in place of arguments.

c.Where("id = ?", 1)
q.Where("id in (?)", 1, 2, 3)

type Query

type Query interface {
	// BelongsTo adds a "where" clause based on the "ID" of the
	// "model" passed into it.
	BelongsTo(model interface{}) Query
	// BelongsToAs adds a "where" clause based on the "ID" of the
	// "model" passed into it, using an alias.
	BelongsToAs(model interface{}, as string) Query
	// BelongsToThrough adds a "where" clause that connects the "bt" model
	// through the associated "thru" model.
	BelongsToThrough(bt, thru interface{}) Query

	// Exec runs the given query.
	Exec() error
	// ExecWithCount runs the given query, and returns the amount of
	// affected rows.
	ExecWithCount() (int, error)

	// Find the first record of the model in the database with a particular id.
	//
	//	q.Find(&User{}, 1)
	Find(model interface{}, id interface{}) error
	// First record of the model in the database that matches the query.
	//
	//	q.Where("name = ?", "mark").First(&User{})
	First(model interface{}) error
	// Last record of the model in the database that matches the query.
	//
	//	q.Where("name = ?", "mark").Last(&User{})
	Last(model interface{}) error
	// All retrieves all of the records in the database that match the query.
	//
	//	q.Where("name = ?", "mark").All(&[]User{})
	All(models interface{}) error
	// Exists returns true/false if a record exists in the database that matches
	// the query.
	//
	// 	q.Where("name = ?", "mark").Exists(&User{})
	Exists(model interface{}) (bool, error)
	// Count the number of records in the database.
	//
	//	q.Where("name = ?", "mark").Count(&User{})
	Count(model interface{}) (int, error)
	// CountByField counts the number of records in the database, for a given field.
	//
	//	q.Where("sex = ?", "f").Count(&User{}, "name")
	CountByField(model interface{}, field string) (int, error)
	// Select allows to query only fields passed as parameter.
	// c.Select("field1", "field2").All(&model)
	// => SELECT field1, field2 FROM models
	Select(fields ...string) Query

	// Paginate records returned from the database.
	//
	//	q = q.Paginate(2, 15)
	//	q.All(&[]User{})
	//	q.Paginator
	Paginate(page int, perPage int) Query
	// PaginateFromParams paginates records returned from the database.
	//
	//	q = q.PaginateFromParams(req.URL.Query())
	//	q.All(&[]User{})
	//	q.Paginator
	PaginateFromParams(params pop.PaginationParams) Query

	// Clone will fill targetQ query with the connection used in q, if
	// targetQ is not empty, Clone will override all the fields.
	Clone(targetQ Query)
	// RawQuery will override the query building feature of Pop and will use
	// whatever query you want to execute against the `Connection`. You can continue
	// to use the `?` argument syntax.
	//
	//	q.RawQuery("select * from foo where id = ?", 1)
	RawQuery(stmt string, args ...interface{}) Query
	// Eager will enable load associations of the model.
	// by defaults loads all the associations on the model,
	// but can take a variadic list of associations to load.
	//
	// 	q.Eager().Find(model, 1) // will load all associations for model.
	// 	q.Eager("Books").Find(model, 1) // will load only Book association for model.
	Eager(fields ...string) Query
	// Where will append a where clause to the query. You may use `?` in place of
	// arguments.
	//
	// 	q.Where("id = ?", 1)
	// 	q.Where("id in (?)", 1, 2, 3)
	Where(stmt string, args ...interface{}) Query
	// Order will append an order clause to the query.
	//
	// 	q.Order("name desc")
	Order(stmt string) Query
	// Limit will add a limit clause to the query.
	Limit(limit int) Query
	// ToSQL will generate SQL and the appropriate arguments for that SQL
	// from the `Model` passed in.
	ToSQL(model *pop.Model, addColumns ...string) (string, []interface{})

	// GroupBy will append a GROUP BY clause to the query
	GroupBy(field string, fields ...string) Query

	// Having will append a HAVING clause to the query
	Having(condition string, args ...interface{}) Query

	// Join will append a JOIN clause to the query
	Join(table string, on string, args ...interface{}) Query
	// LeftJoin will append a LEFT JOIN clause to the query
	LeftJoin(table string, on string, args ...interface{}) Query
	// RightJoin will append a RIGHT JOIN clause to the query
	RightJoin(table string, on string, args ...interface{}) Query
	// LeftOuterJoin will append a LEFT OUTER JOIN clause to the query
	LeftOuterJoin(table string, on string, args ...interface{}) Query
	// RightOuterJoin will append a RIGHT OUTER JOIN clause to the query
	RightOuterJoin(table string, on string, args ...interface{}) Query
	// LeftInnerJoin will append a LEFT INNER JOIN clause to the query
	LeftInnerJoin(table string, on string, args ...interface{}) Query
	// RightInnerJoin will append a RIGHT INNER JOIN clause to the query
	RightInnerJoin(table string, on string, args ...interface{}) Query

	// Scope the query by using a `ScopeFunc`
	//
	//	func ByName(name string) ScopeFunc {
	//		return func(q Query) Query {
	//			return q.Where("name = ?", name)
	//		}
	//	}
	//
	//	func WithDeleted(q *pop.Query) *pop.Query {
	//		return q.Where("deleted_at is null")
	//	}
	//
	//	c.Scope(ByName("mark)).Scope(WithDeleted).First(&User{})
	Scope(sf pop.ScopeFunc) Query
}

Query ...

Jump to

Keyboard shortcuts

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