Documentation ¶
Overview ¶
So what does Pop do exactly? Well, it wraps the absolutely amazing https://github.com/jmoiron/sqlx library. It cleans up some of the common patterns and workflows usually associated with dealing with databases in Go.
Pop makes it easy to do CRUD operations, run migrations, and build/execute queries. Is Pop an ORM? I'll leave that up to you, the reader, to decide.
Pop, by default, follows conventions that were defined by the ActiveRecord Ruby gem, http://www.rubyonrails.org. What does this mean?
* Tables must have an "id" column and a corresponding "ID" field on the `struct` being used. * If there is a timestamp column named "created_at", "CreatedAt" on the `struct`, it will be set with the current time when the record is created. * If there is a timestamp column named "updated_at", "UpdatedAt" on the `struct`, it will be set with the current time when the record is updated. * Default databases are lowercase, underscored versions of the `struct` name. Examples: User{} is "users", FooBar{} is "foo_bars", etc...
Index ¶
- Variables
- func AddLookupPaths(paths ...string)
- func CreateDB(c *Connection) error
- func DropDB(c *Connection) error
- func LoadConfig()
- func LookupPaths() []string
- func MapTableName(name string, tableName string)
- func MigrationCreate(path, name, ext string, up, down []byte) error
- type Connection
- func (c *Connection) All(models interface{}) error
- func (c *Connection) BelongsTo(model interface{}) *Query
- func (c *Connection) BelongsToThrough(bt, thru interface{}) *Query
- func (c *Connection) Close() error
- func (c *Connection) Count(model interface{}) (int, error)
- func (c *Connection) Create(model interface{}, excludeColumns ...string) error
- func (c *Connection) Destroy(model interface{}) error
- func (c *Connection) Find(model interface{}, id int) error
- func (c *Connection) First(model interface{}) error
- func (c *Connection) Last(model interface{}) error
- func (c *Connection) Limit(limit int) *Query
- func (c *Connection) MigrateDown(path string) error
- func (c *Connection) MigrateReset(path string) error
- func (c *Connection) MigrateUp(path string) error
- func (c *Connection) MigrationURL() string
- func (c *Connection) NewTransaction() (*Connection, error)
- func (c *Connection) Open() error
- func (c *Connection) Order(stmt string) *Query
- func (c *Connection) Paginate(page int, per_page int) *Query
- func (c *Connection) PaginateFromParams(params PaginationParams) *Query
- func (c *Connection) Q() *Query
- func (c *Connection) RawQuery(stmt string, args ...interface{}) *Query
- func (c *Connection) Reload(model interface{}) error
- func (c *Connection) Rollback(fn func(tx *Connection)) error
- func (c *Connection) Save(model interface{}, excludeColumns ...string) error
- func (c *Connection) Scope(sf ScopeFunc) *Query
- func (c *Connection) String() string
- func (c *Connection) Transaction(fn func(tx *Connection) error) error
- func (c *Connection) URL() string
- func (c *Connection) Update(model interface{}, excludeColumns ...string) error
- func (c *Connection) Where(stmt string, args ...interface{}) *Query
- type ConnectionDetails
- type Dialect
- type Model
- type MySQL
- func (m *MySQL) Create(store Store, model *Model, cols Columns) error
- func (m *MySQL) CreateDB() error
- func (m *MySQL) Destroy(store Store, model *Model) error
- func (m *MySQL) Details() *ConnectionDetails
- func (m *MySQL) DropDB() error
- func (m *MySQL) FizzTranslator() fizz.Translator
- func (m *MySQL) Lock(fn func() error) error
- func (m *MySQL) MigrationURL() string
- func (m *MySQL) SelectMany(store Store, models *Model, query Query) error
- func (m *MySQL) SelectOne(store Store, model *Model, query Query) error
- func (m *MySQL) TranslateSQL(sql string) string
- func (m *MySQL) URL() string
- func (m *MySQL) Update(store Store, model *Model, cols Columns) error
- type PaginationParams
- type Paginator
- type PostgreSQL
- func (p *PostgreSQL) Create(store Store, model *Model, cols Columns) error
- func (p *PostgreSQL) CreateDB() error
- func (p *PostgreSQL) Destroy(store Store, model *Model) error
- func (p *PostgreSQL) Details() *ConnectionDetails
- func (p *PostgreSQL) DropDB() error
- func (p *PostgreSQL) FizzTranslator() fizz.Translator
- func (p *PostgreSQL) Lock(fn func() error) error
- func (m *PostgreSQL) MigrationURL() string
- func (p *PostgreSQL) SelectMany(store Store, models *Model, query Query) error
- func (p *PostgreSQL) SelectOne(store Store, model *Model, query Query) error
- func (p *PostgreSQL) TranslateSQL(sql string) string
- func (m *PostgreSQL) URL() string
- func (p *PostgreSQL) Update(store Store, model *Model, cols Columns) error
- type Query
- func (q *Query) All(models interface{}) error
- func (q *Query) BelongsTo(model interface{}) *Query
- func (q *Query) BelongsToThrough(bt, thru interface{}) *Query
- func (q Query) Count(model interface{}) (int, error)
- func (q *Query) Exec() error
- func (q *Query) Exists(model interface{}) (bool, error)
- func (q *Query) Find(model interface{}, id int) error
- func (q *Query) First(model interface{}) error
- func (q *Query) Last(model interface{}) error
- func (q *Query) Limit(limit int) *Query
- func (q *Query) Order(stmt string) *Query
- func (q *Query) Paginate(page int, per_page int) *Query
- func (q *Query) PaginateFromParams(params PaginationParams) *Query
- func (q *Query) RawQuery(stmt string, args ...interface{}) *Query
- func (q *Query) Scope(sf ScopeFunc) *Query
- func (q Query) ToSQL(model *Model, addColumns ...string) (string, []interface{})
- func (q Query) ToSQLBuilder(model *Model, addColumns ...string) *SQLBuilder
- func (q *Query) Where(stmt string, args ...interface{}) *Query
- type SQLBuilder
- type SQLite
- func (m *SQLite) Create(store Store, model *Model, cols Columns) error
- func (m *SQLite) CreateDB() error
- func (m *SQLite) Destroy(store Store, model *Model) error
- func (m *SQLite) Details() *ConnectionDetails
- func (m *SQLite) DropDB() error
- func (m *SQLite) FizzTranslator() fizz.Translator
- func (m *SQLite) Lock(fn func() error) error
- func (m *SQLite) MigrationURL() string
- func (m *SQLite) SelectMany(store Store, models *Model, query Query) error
- func (m *SQLite) SelectOne(store Store, model *Model, query Query) error
- func (m *SQLite) TranslateSQL(sql string) string
- func (m *SQLite) URL() string
- func (m *SQLite) Update(store Store, model *Model, cols Columns) error
- type ScopeFunc
- type Store
- type Value
Constants ¶
This section is empty.
Variables ¶
var ConfigName = "database.yml"
var Connections = map[string]*Connection{}
Connections contains all of the available connections
var Debug = false
var Log = func(s string, args ...interface{}) { if Debug { if len(args) > 0 { xargs := make([]string, len(args)) for i, a := range args { switch a.(type) { case string: xargs[i] = fmt.Sprintf("%q", a) default: xargs[i] = fmt.Sprintf("%v", a) } } s = fmt.Sprintf("%s | %s", s, xargs) } logger.Println(s) } }
var PaginatorPageKey = "page"
var PaginatorPerPageDefault = 20
var PaginatorPerPageKey = "per_page"
Functions ¶
func AddLookupPaths ¶
func AddLookupPaths(paths ...string)
func CreateDB ¶
func CreateDB(c *Connection) error
func DropDB ¶
func DropDB(c *Connection) error
func LoadConfig ¶
func LoadConfig()
func LookupPaths ¶
func LookupPaths() []string
func MapTableName ¶
MapTableName allows for the customize table mapping between a name and the database. For example the value `User{}` will automatically map to "users". MapTableName would allow this to change.
m := &pop.Model{Value: User{}} m.TableName() // "users" pop.MapTableName("user", "people") m = &pop.Model{Value: User{}} m.TableName() // "people"
func MigrationCreate ¶
Types ¶
type Connection ¶
Connection represents all of the necessary details for talking with a datastore
func Connect ¶
func Connect(e string) (*Connection, error)
Connect takes the name of a connection, default is "development", and will return that connection from the available `Connections`. If a connection with that name can not be found an error will be returned. If a connection is found, and it has yet to open a connection with its underlying datastore, a connection to that store will be opened.
func NewConnection ¶
func NewConnection(deets *ConnectionDetails) *Connection
NewConnection creates a new connection, and sets it's `Dialect` appropriately based on the `ConnectionDetails` passed into it.
func (*Connection) All ¶
func (c *Connection) All(models interface{}) error
All retrieves all of the records in the database that match the query.
c.All(&[]User{})
func (*Connection) BelongsTo ¶
func (c *Connection) BelongsTo(model interface{}) *Query
BelongsTo adds a "where" clause based on the "ID" of the "model" passed into it.
func (*Connection) BelongsToThrough ¶
func (c *Connection) BelongsToThrough(bt, thru interface{}) *Query
BelongsToThrough adds a "where" clause that connects the "bt" model through the associated "thru" model.
func (*Connection) Close ¶
func (c *Connection) Close() error
func (*Connection) Count ¶
func (c *Connection) Count(model interface{}) (int, error)
Count the number of records in the database.
c.Count(&User{})
func (*Connection) Create ¶
func (c *Connection) Create(model interface{}, excludeColumns ...string) error
func (*Connection) Destroy ¶
func (c *Connection) Destroy(model interface{}) error
func (*Connection) Find ¶
func (c *Connection) Find(model interface{}, id int) error
Find the first record of the model in the database with a particular id.
c.Find(&User{}, 1)
func (*Connection) First ¶
func (c *Connection) First(model interface{}) error
First record of the model in the database that matches the query.
c.First(&User{})
func (*Connection) Last ¶
func (c *Connection) Last(model interface{}) error
Last record of the model in the database that matches the query.
c.Last(&User{})
func (*Connection) Limit ¶
func (c *Connection) Limit(limit int) *Query
Limit will add a limit clause to the query.
func (*Connection) MigrateDown ¶
func (c *Connection) MigrateDown(path string) error
func (*Connection) MigrateReset ¶
func (c *Connection) MigrateReset(path string) error
func (*Connection) MigrateUp ¶
func (c *Connection) MigrateUp(path string) error
func (*Connection) MigrationURL ¶
func (c *Connection) MigrationURL() string
func (*Connection) NewTransaction ¶
func (c *Connection) NewTransaction() (*Connection, error)
func (*Connection) Open ¶
func (c *Connection) Open() error
func (*Connection) Order ¶
func (c *Connection) Order(stmt string) *Query
Order will append an order clause to the query.
c.Order("name desc")
func (*Connection) Paginate ¶
func (c *Connection) Paginate(page int, per_page int) *Query
Paginate records returned from the database.
q := c.Paginate(2, 15) q.All(&[]User{}) q.Paginator
func (*Connection) PaginateFromParams ¶
func (c *Connection) PaginateFromParams(params PaginationParams) *Query
Paginate records returned from the database.
q := c.PaginateFromParams(req.URL.Query()) q.All(&[]User{}) q.Paginator
func (*Connection) Q ¶
func (c *Connection) Q() *Query
Q creates a new "empty" query for the current connection.
func (*Connection) RawQuery ¶
func (c *Connection) RawQuery(stmt string, args ...interface{}) *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 (*Connection) Reload ¶
func (c *Connection) Reload(model interface{}) error
func (*Connection) Rollback ¶
func (c *Connection) 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 (*Connection) Save ¶
func (c *Connection) Save(model interface{}, excludeColumns ...string) error
func (*Connection) Scope ¶
func (c *Connection) Scope(sf ScopeFunc) *Query
Scope the query by using a `ScopeFunc`
func ByName(name string) ScopeFunc { return func(q *Query) *Query { return q.Where("name = ?", name) } } c.Scope(ByName("mark")).First(&User{})
func (*Connection) String ¶
func (c *Connection) String() string
func (*Connection) Transaction ¶
func (c *Connection) 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 (*Connection) URL ¶
func (c *Connection) URL() string
func (*Connection) Update ¶
func (c *Connection) Update(model interface{}, excludeColumns ...string) error
func (*Connection) Where ¶
func (c *Connection) Where(stmt string, args ...interface{}) *Query
Where will append a where clause to the query. You may use `?` in place of arguments.
c.Where("id = ?", 1)
type ConnectionDetails ¶
type ConnectionDetails struct { Dialect string Database string Host string Port string User string Password string URL string Options map[string]string }
func (*ConnectionDetails) Parse ¶
func (cd *ConnectionDetails) Parse(port string) error
Parse extracts the various components of a connection string.
func (*ConnectionDetails) RetryLimit ¶
func (cd *ConnectionDetails) RetryLimit() int
func (*ConnectionDetails) RetrySleep ¶
func (cd *ConnectionDetails) RetrySleep() time.Duration
type Dialect ¶
type Dialect interface { URL() string MigrationURL() string Details() *ConnectionDetails TranslateSQL(sql string) string Create(store Store, model *Model, cols Columns) error Update(store Store, model *Model, cols Columns) error Destroy(store Store, model *Model) error SelectOne(store Store, model *Model, query Query) error SelectMany(store Store, models *Model, query Query) error CreateDB() error DropDB() error FizzTranslator() fizz.Translator Lock(func() error) error }
func NewMySQL ¶
func NewMySQL(deets *ConnectionDetails) Dialect
func NewPostgreSQL ¶
func NewPostgreSQL(deets *ConnectionDetails) Dialect
func NewSQLite ¶
func NewSQLite(deets *ConnectionDetails) Dialect
type Model ¶
type Model struct { Value // contains filtered or unexported fields }
Model is used throughout Pop to wrap the end user interface that is passed in to many functions.
type MySQL ¶
type MySQL struct {
ConnectionDetails *ConnectionDetails
}
func (*MySQL) Details ¶
func (m *MySQL) Details() *ConnectionDetails
func (*MySQL) FizzTranslator ¶
func (m *MySQL) FizzTranslator() fizz.Translator
func (*MySQL) MigrationURL ¶
func (*MySQL) SelectMany ¶
func (*MySQL) TranslateSQL ¶
type PaginationParams ¶
type Paginator ¶
type Paginator struct { // Current page you're on Page int `json:"page"` // Number of results you want per page PerPage int `json:"per_page"` // Page * PerPage (ex: 2 * 20, Offset == 40) Offset int `json:"offset"` // Total potential records matching the query TotalEntriesSize int `json:"total_entries_size"` // Total records returns, will be <= PerPage CurrentEntriesSize int `json:"current_entries_size"` // Total pages TotalPages int `json:"total_pages"` }
Paginator is a type used to represent the pagination of records from the database.
func NewPaginator ¶
NewPaginator returns a new `Paginator` value with the appropriate defaults set.
func NewPaginatorFromParams ¶
func NewPaginatorFromParams(params PaginationParams) *Paginator
NewPaginatorFromParams takes an interface of type `PaginationParams`, the `url.Values` type works great with this interface, and returns a new `Paginator` based on the params or `PaginatorPageKey` and `PaginatorPerPageKey`. Defaults are `1` for the page and PaginatorPerPageDefault for the per page value.
type PostgreSQL ¶
type PostgreSQL struct { ConnectionDetails *ConnectionDetails // contains filtered or unexported fields }
func (*PostgreSQL) Create ¶
func (p *PostgreSQL) Create(store Store, model *Model, cols Columns) error
func (*PostgreSQL) CreateDB ¶
func (p *PostgreSQL) CreateDB() error
func (*PostgreSQL) Details ¶
func (p *PostgreSQL) Details() *ConnectionDetails
func (*PostgreSQL) DropDB ¶
func (p *PostgreSQL) DropDB() error
func (*PostgreSQL) FizzTranslator ¶
func (p *PostgreSQL) FizzTranslator() fizz.Translator
func (*PostgreSQL) Lock ¶
func (p *PostgreSQL) Lock(fn func() error) error
func (*PostgreSQL) MigrationURL ¶
func (m *PostgreSQL) MigrationURL() string
func (*PostgreSQL) SelectMany ¶
func (p *PostgreSQL) SelectMany(store Store, models *Model, query Query) error
func (*PostgreSQL) SelectOne ¶
func (p *PostgreSQL) SelectOne(store Store, model *Model, query Query) error
func (*PostgreSQL) TranslateSQL ¶
func (p *PostgreSQL) TranslateSQL(sql string) string
func (*PostgreSQL) URL ¶
func (m *PostgreSQL) URL() string
type Query ¶
type Query struct { RawSQL *clause Paginator *Paginator Connection *Connection // contains filtered or unexported fields }
Query is the main value that is used to build up a query to be executed against the `Connection`.
func Q ¶
func Q(c *Connection) *Query
Q will create a new "empty" query from the current connection.
func (*Query) All ¶
All retrieves all of the records in the database that match the query.
q.Where("name = ?", "mark").All(&[]User{})
func (*Query) BelongsTo ¶
BelongsTo adds a "where" clause based on the "ID" of the "model" passed into it.
func (*Query) BelongsToThrough ¶
BelongsToThrough adds a "where" clause that connects the "bt" model through the associated "thru" model.
func (Query) Count ¶
Count the number of records in the database.
q.Where("name = ?", "mark").Count(&User{})
func (*Query) Exists ¶
Exists returns true/false if a record exists in the database that matches the query.
q.Where("name = ?", "mark").Exists(&User{})
func (*Query) Find ¶
Find the first record of the model in the database with a particular id.
q.Find(&User{}, 1)
func (*Query) First ¶
First record of the model in the database that matches the query.
q.Where("name = ?", "mark").First(&User{})
func (*Query) Last ¶
Last record of the model in the database that matches the query.
q.Where("name = ?", "mark").Last(&User{})
func (*Query) Paginate ¶
Paginate records returned from the database.
q = q.Paginate(2, 15) q.All(&[]User{}) q.Paginator
func (*Query) PaginateFromParams ¶
func (q *Query) PaginateFromParams(params PaginationParams) *Query
Paginate records returned from the database.
q = q.PaginateFromParams(req.URL.Query()) q.All(&[]User{}) q.Paginator
func (*Query) RawQuery ¶
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)
func (*Query) Scope ¶
Scope the query by using a `ScopeFunc`
func ByName(name string) ScopeFunc { return func(q *Query) *Query { return q.Where("name = ?", name) } } q.Scope(ByName("mark").Where("id = ?", 1).First(&User{})
func (Query) ToSQL ¶
ToSQL will generate SQL and the appropriate arguments for that SQL from the `Model` passed in.
func (Query) ToSQLBuilder ¶
func (q Query) ToSQLBuilder(model *Model, addColumns ...string) *SQLBuilder
ToSQLBuilder returns a new `SQLBuilder` that can be used to generate SQL, get arguments, and more.
type SQLBuilder ¶
type SQLBuilder struct { Query Query Model *Model AddColumns []string // contains filtered or unexported fields }
func NewSQLBuilder ¶
func NewSQLBuilder(q Query, m *Model, addColumns ...string) *SQLBuilder
func (*SQLBuilder) Args ¶
func (sq *SQLBuilder) Args() []interface{}
func (*SQLBuilder) String ¶
func (sq *SQLBuilder) String() string
type SQLite ¶
type SQLite struct { ConnectionDetails *ConnectionDetails // contains filtered or unexported fields }
func (*SQLite) Details ¶
func (m *SQLite) Details() *ConnectionDetails
func (*SQLite) FizzTranslator ¶
func (m *SQLite) FizzTranslator() fizz.Translator
func (*SQLite) MigrationURL ¶
func (*SQLite) SelectMany ¶
func (*SQLite) TranslateSQL ¶
type Store ¶
type Store interface { Select(dest interface{}, query string, args ...interface{}) error Get(dest interface{}, query string, args ...interface{}) error NamedExec(query string, arg interface{}) (sql.Result, error) Exec(query string, args ...interface{}) (sql.Result, error) PrepareNamed(query string) (*sqlx.NamedStmt, error) Transaction() (*tX, error) Rollback() error Commit() error Close() error }
Store is an interface that must be implemented in order for Pop to be able to use the value as a way of talking to a datastore.