Last active
August 29, 2015 14:18
-
-
Save tonyhb/581114ae710dcf3a9369 to your computer and use it in GitHub Desktop.
Exposing atomic and transactional database methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package models | |
import ( | |
"github.com/jmoiron/sqlx" | |
"github.com/tonyhb/govalidate" | |
) | |
type Page struct { | |
Id int64 | |
Name string | |
} | |
func (p *Page) Create() (err error) { | |
var tx *sqlx.Tx | |
// DB() returns a *sqlx.DB | |
if tx, err = DB().Beginx(); err != nil { | |
return err | |
} | |
if err = p.CreateTx(tx); err != nil { | |
tx.Rollback() | |
return | |
} | |
return tx.Commit() | |
} | |
func (p *Page) CreateTx(tx *sqlx.Tx) (err error) { | |
if err = validate.Run(p); err != nil { | |
return | |
} | |
// We could also use Modl to wrap the transaction and insert this record. | |
res, err := tx.NamedExec(`INSERT INTO pages (name) VALUES (:name)`, p) | |
if err != nil { | |
return | |
} | |
p.Id, err = res.LastInsertId() | |
return | |
} | |
// Ideally all models will implement the same CreateTx format allowing us to | |
// create an interface: | |
// | |
// type Createable interface { | |
// CreateTx(*sqlx.Tx) error | |
// } | |
// | |
// We can then abstract the Create() call from each of our types into its own | |
// function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment