Pop Goes The Database!

By Mark Bates

Elevator Pitch

A Tasty Treat For All Your Database Needs - Pop is a database agnostic package 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.

Description

The pop package for Go is a database agnostic (Postgres, MySQL, SQLite3, and CockroachDB) package and CLI tool that makes writing database backed applications simple, easy, and fun.

With Pop, and it’s CLI companion, Soda, your database code can be transformed for this:

// database/sql
db, err := sql.Open("postgres", "user=postgres password=postgres dbname=pop_test sslmode=disable")
if err != nil {
	log.Fatalln(err)
}

rows, err := db.Query("select id, first_name, last_name, created_at, updated_at from good_friends where id = $1", 1)
if err != nil {
	log.Fatal(err)
}
defer rows.Close()

friends := []GoodFriend{}
for rows.Next() {
	friend := GoodFriend{}
	if err := rows.Scan(&friend.ID, &friend.FirstName, &friend.LastName, &friend.CreatedAt, &friend.UpdatedAt); err != nil {
		log.Fatal(err)
	}
	friends = append(friends, friend)
}
if err := rows.Err(); err != nil {
	log.Fatal(err)
}
fmt.Println("### friends ->", friends)

To this:

// github.com/markbates/pop
dbp, err := pop.Connect("development")
if err != nil {
	log.Fatal(err)
}

friends = []GoodFriend{}
if err := dbp.Where("id = ?", 1).All(&friends); err != nil {
	log.Fatal(err)
}
fmt.Println("### friends ->", friends)

With support for several databases Pop makes it easy for you to write your code and run it on many different databases. This database agnostic approach is taken a step further with the concept of database agnostic migrations.

With simple and clean DSLs you’d be surprised how much you can do, and how fast you can do it.

Notes

Pop can be found at https://github.com/gobuffalo/pop.