Skip to content

Commit 0fbe04e

Browse files
committed
added a whole bunch of documentation
1 parent e149002 commit 0fbe04e

15 files changed

+274
-88
lines changed

LICENSE.txt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2015 Mark Bates
2+
3+
MIT License
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
"Software"), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ So what does Pop do exactly? Well, it wraps the absolutely amazing [https://gith
66

77
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.
88

9+
Pop, by default, follows conventions that were defined by the ActiveRecord Ruby gem, http://www.rubyonrails.org. What does this mean?
10+
11+
* Tables must have an "id" column and a corresponding "ID" field on the `struct` being used.
12+
* 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.
13+
* 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.
14+
* Default databases are lowercase, underscored versions of the `struct` name. Examples: User{} is "users", FooBar{} is "foo_bars", etc...
15+
16+
## Docs
17+
18+
The API docs can be found at [http://godoc.org/github.com/markbates/pop](http://godoc.org/github.com/markbates/pop)
19+
920
## Connecting to Databases
1021

1122
Pop is easily configured using a YAML file. The configuration file should be stored in `config/database.yml` or `database.yml`.

belongs_to.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func (c *Connection) BelongsTo(model interface{}) *Query {
1212
// "model" passed into it.
1313
func (q *Query) BelongsTo(model interface{}) *Query {
1414
m := &Model{Value: model}
15-
q.Where(fmt.Sprintf("%s = ?", m.AssociationName()), m.ID())
15+
q.Where(fmt.Sprintf("%s = ?", m.associationName()), m.ID())
1616
return q
1717
}
1818

connection.go

+20
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ import (
88
"github.com/markbates/going/defaults"
99
)
1010

11+
// Connections contains all of the available connections
1112
var Connections = map[string]*Connection{}
1213

14+
// Connection represents all of the necessary details for
15+
// talking with a datastore
1316
type Connection struct {
1417
Store Store
1518
Dialect Dialect
@@ -20,6 +23,8 @@ func (c *Connection) String() string {
2023
return c.Dialect.URL()
2124
}
2225

26+
// NewConnection creates a new connection, and sets it's `Dialect`
27+
// appropriately based on the `ConnectionDetails` passed into it.
2328
func NewConnection(deets *ConnectionDetails) *Connection {
2429
c := &Connection{
2530
Timings: []time.Duration{},
@@ -35,19 +40,30 @@ func NewConnection(deets *ConnectionDetails) *Connection {
3540
return c
3641
}
3742

43+
// Connect takes the name of a connection, default is "development", and will
44+
// return that connection from the available `Connections`. If a connection with
45+
// that name can not be found an error will be returned. If a connection is
46+
// found, and it has yet to open a connection with its underlying datastore,
47+
// a connection to that store will be opened.
3848
func Connect(e string) (*Connection, error) {
3949
e = defaults.String(e, "development")
4050
c := Connections[e]
4151
if c == nil {
4252
return c, fmt.Errorf("Could not find connection named %s!", e)
4353
}
54+
if c.Store != nil {
55+
return c, nil
56+
}
4457
db, err := sqlx.Open(c.Dialect.Details().Dialect, c.Dialect.URL())
4558
if err == nil {
4659
c.Store = &dB{db}
4760
}
4861
return c, nil
4962
}
5063

64+
// Transaction will start a new transaction on the connection. If the inner function
65+
// returns an error then the transaction will be rolled back, otherwise the transaction
66+
// will automatically commit at the end.
5167
func (c *Connection) Transaction(fn func(tx *Connection) error) error {
5268
tx, err := c.Store.Transaction()
5369
if err != nil {
@@ -67,6 +83,8 @@ func (c *Connection) Transaction(fn func(tx *Connection) error) error {
6783
return err
6884
}
6985

86+
// Rollback will open a new transaction and automatically rollback that transaction
87+
// when the inner function returns, regardless. This can be useful for tests, etc...
7088
func (c *Connection) Rollback(fn func(tx *Connection)) error {
7189
tx, err := c.Store.Transaction()
7290
if err != nil {
@@ -80,6 +98,8 @@ func (c *Connection) Rollback(fn func(tx *Connection)) error {
8098
fn(cn)
8199
return tx.Rollback()
82100
}
101+
102+
// Q creates a new "empty" query for the current connection.
83103
func (c *Connection) Q() *Query {
84104
return Q(c)
85105
}

dialect.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func genericCreate(store Store, model *Model, cols Columns) error {
3232
}
3333
id, err = res.LastInsertId()
3434
if err == nil {
35-
model.SetID(int(id))
35+
model.setID(int(id))
3636
}
3737
return err
3838
}

doc.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
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.
3+
4+
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.
5+
6+
Pop, by default, follows conventions that were defined by the ActiveRecord Ruby gem, http://www.rubyonrails.org. What does this mean?
7+
8+
* Tables must have an "id" column and a corresponding "ID" field on the `struct` being used.
9+
* 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.
10+
* 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.
11+
* Default databases are lowercase, underscored versions of the `struct` name. Examples: User{} is "users", FooBar{} is "foo_bars", etc...
12+
*/
13+
package pop

executors.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ func (c *Connection) Create(model interface{}, excludeColumns ...string) error {
3131
cols := ColumnsForStruct(model, sm.TableName())
3232
cols.Remove(excludeColumns...)
3333

34-
sm.TouchCreatedAt()
35-
sm.TouchUpdatedAt()
34+
sm.touchCreatedAt()
35+
sm.touchUpdatedAt()
3636

3737
return c.Dialect.Create(c.Store, sm, cols)
3838
})
@@ -46,7 +46,7 @@ func (c *Connection) Update(model interface{}, excludeColumns ...string) error {
4646
cols.Remove("id", "created_at")
4747
cols.Remove(excludeColumns...)
4848

49-
sm.TouchUpdatedAt()
49+
sm.touchUpdatedAt()
5050

5151
return c.Dialect.Update(c.Store, sm, cols)
5252
})

finders.go

+34-4
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,30 @@ package pop
22

33
import "reflect"
44

5+
// Find the first record of the model in the database with a particular id.
6+
//
7+
// c.Find(&User{}, 1)
58
func (c *Connection) Find(model interface{}, id int) error {
69
return Q(c).Find(model, id)
710
}
811

12+
// Find the first record of the model in the database with a particular id.
13+
//
14+
// q.Find(&User{}, 1)
915
func (q *Query) Find(model interface{}, id int) error {
1016
return q.Where("id = ?", id).First(model)
1117
}
1218

19+
// First record of the model in the database that matches the query.
20+
//
21+
// c.First(&User{})
1322
func (c *Connection) First(model interface{}) error {
1423
return Q(c).First(model)
1524
}
1625

26+
// First record of the model in the database that matches the query.
27+
//
28+
// q.Where("name = ?", "mark").First(&User{})
1729
func (q *Query) First(model interface{}) error {
1830
return q.Connection.timeFunc("First", func() error {
1931
q.Limit(1)
@@ -22,10 +34,16 @@ func (q *Query) First(model interface{}) error {
2234
})
2335
}
2436

37+
// Last record of the model in the database that matches the query.
38+
//
39+
// c.Last(&User{})
2540
func (c *Connection) Last(model interface{}) error {
2641
return Q(c).Last(model)
2742
}
2843

44+
// Last record of the model in the database that matches the query.
45+
//
46+
// q.Where("name = ?", "mark").Last(&User{})
2947
func (q *Query) Last(model interface{}) error {
3048
return q.Connection.timeFunc("Last", func() error {
3149
q.Limit(1)
@@ -35,10 +53,16 @@ func (q *Query) Last(model interface{}) error {
3553
})
3654
}
3755

56+
// All retrieves all of the records in the database that match the query.
57+
//
58+
// c.All(&[]User{})
3859
func (c *Connection) All(models interface{}) error {
3960
return Q(c).All(models)
4061
}
4162

63+
// All retrieves all of the records in the database that match the query.
64+
//
65+
// q.Where("name = ?", "mark").All(&[]User{})
4266
func (q *Query) All(models interface{}) error {
4367
return q.Connection.timeFunc("All", func() error {
4468
m := &Model{Value: models}
@@ -59,19 +83,25 @@ func (q *Query) All(models interface{}) error {
5983
})
6084
}
6185

62-
func (c *Connection) Exists(model interface{}) (bool, error) {
63-
return Q(c).Exists(model)
64-
}
65-
86+
// Exists returns true/false if a record exists in the database that matches
87+
// the query.
88+
//
89+
// q.Where("name = ?", "mark").Exists(&User{})
6690
func (q *Query) Exists(model interface{}) (bool, error) {
6791
i, err := q.Count(model)
6892
return i != 0, err
6993
}
7094

95+
// Count the number of records in the database.
96+
//
97+
// c.Count(&User{})
7198
func (c *Connection) Count(model interface{}) (int, error) {
7299
return Q(c).Count(model)
73100
}
74101

102+
// Count the number of records in the database.
103+
//
104+
// q.Where("name = ?", "mark").Count(&User{})
75105
func (q Query) Count(model interface{}) (int, error) {
76106
res := &rowCount{}
77107
err := q.Connection.timeFunc("Count", func() error {

0 commit comments

Comments
 (0)