Skip to content

Latest commit

 

History

History
66 lines (48 loc) · 1.23 KB

README.md

File metadata and controls

66 lines (48 loc) · 1.23 KB

Hyperbun

Hyperbun is a utility library built on top of Bun to reduce boilerplate code.

Queries like this in bun:

func Foo() {
  // ...
	
  var user User
  if err := db.NewSelect().Model(&user).Where("id = ?", id).Scan(c.Request.Context()); err != nil {
    if errors.Is(err, sql.ErrNoRows) {
      // ...
    }
	// ...
  }
		
  db.NewDelete().Model(&user).Where("id = ?", id).Exec(c.Request.Context())
}

can be written like this with Hyperbun:

func Foo() {
  hdb := hyperbun.NewContext(c.Request.Context(), db)

  user, err := hyperbun.ByID(hdb, "users", id)
  if err != nil {
	// ...	
  }	
  if user == nil {
    // ...	
  }
	
  hyperbun.DeleteByID(hdb, "users", id)
}

It also makes working with transactions easier and tx and hdb can be used interchangeably. This promotes code reuse.

func Foo(db hyperbun.DB) {
  hyperbun.DeleteByID(db, "users", id)
}

func main() {
  hdb := hyperbun.NewContext(c.Request.Context(), db)

  hyperbun.RunInTx(hdb, func(tx hyperbun.TxContext) error {
    Foo(tx)
    return nil
  })

  Foo(hdb)
}

License

  • Distributed under MIT License, please see license file within the code for more details.

Credits