ezsqlx is a library which provides helpers for jmoiron/sqlx. It's goal is to improve your experience without incurring the penalties of an ORM.
- Insert
- Update
- Connection management
- Get database fields from a struct
Given a basic model struct, ezsqlx simplifies basic operations.
type FooBar struct {
Id int `db:"id"`
Message string `db:"message"`
Flip bool `db:"flip"`
Created *time.Time `db:"created"`
}
Insert(
db *sqlx.DB,
table string,
model interface{},
excludedFields []string
) (*sqlx.Rows, error)
newRow := &FooBar{Message: "confused unga bunga"}
rows, err := Insert(db, "foobar", newRow, []string{"id", "created"})
Update(
db *sqlx.DB,
table string,
model interface{},
where string,
excludedFields []string
) (sql.Result, error)
updatedRow := &FooBar{Id: 1, Message: "pc master race", Flip: true}
where := fmt.Sprintf("id=%v", updatedRow.Id)
_, err = Update(db, "foobar", updatedRow, where, []string{"id", "created"})
Fields(
values interface{}
) []string
fields := Fields(model)
ezsqlx.ConnectionSettings
abstracts away basic Postgres connection operations. Check connections.go
for a full list of helpers.
cs := &ConnectionSettings{
Host: "localhost",
Port: "1234",
User: "postgres",
Password: "postgres",
Database: "my_database"
}
db := cs.Open()
defer db.Close()