upgrade from go-pg to bun
This commit is contained in:
parent
f59f129354
commit
1886be62bc
23 changed files with 415 additions and 385 deletions
|
|
@ -1,15 +1,15 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"net/url"
|
||||
|
||||
"github.com/dhax/go-base/auth/jwt"
|
||||
"github.com/dhax/go-base/auth/pwdless"
|
||||
"github.com/dhax/go-base/models"
|
||||
"github.com/go-pg/pg"
|
||||
"github.com/go-pg/pg/orm"
|
||||
"github.com/go-pg/pg/urlvalues"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
@ -21,11 +21,11 @@ var (
|
|||
|
||||
// AdmAccountStore implements database operations for account management by admin.
|
||||
type AdmAccountStore struct {
|
||||
db *pg.DB
|
||||
db *bun.DB
|
||||
}
|
||||
|
||||
// NewAdmAccountStore returns an AccountStore.
|
||||
func NewAdmAccountStore(db *pg.DB) *AdmAccountStore {
|
||||
func NewAdmAccountStore(db *bun.DB) *AdmAccountStore {
|
||||
return &AdmAccountStore{
|
||||
db: db,
|
||||
}
|
||||
|
|
@ -33,8 +33,9 @@ func NewAdmAccountStore(db *pg.DB) *AdmAccountStore {
|
|||
|
||||
// AccountFilter provides pagination and filtering options on accounts.
|
||||
type AccountFilter struct {
|
||||
Pager *urlvalues.Pager
|
||||
Filter *urlvalues.Filter
|
||||
Limit int
|
||||
Offset int
|
||||
Filter map[string]interface{}
|
||||
Order []string
|
||||
}
|
||||
|
||||
|
|
@ -44,29 +45,47 @@ func NewAccountFilter(params interface{}) (*AccountFilter, error) {
|
|||
if !ok {
|
||||
return nil, ErrBadParams
|
||||
}
|
||||
p := urlvalues.Values(v)
|
||||
f := &AccountFilter{
|
||||
Pager: urlvalues.NewPager(p),
|
||||
Filter: urlvalues.NewFilter(p),
|
||||
Order: p["order"],
|
||||
Limit: 10, // Default limit
|
||||
Offset: 0, // Default offset
|
||||
Filter: make(map[string]interface{}),
|
||||
Order: v["order"],
|
||||
}
|
||||
// Parse limit and offset
|
||||
if limit := v.Get("limit"); limit != "" {
|
||||
f.Limit = int(limit[0] - '0')
|
||||
}
|
||||
if offset := v.Get("offset"); offset != "" {
|
||||
f.Offset = int(offset[0] - '0')
|
||||
}
|
||||
// Parse filters
|
||||
for key, values := range v {
|
||||
if key != "limit" && key != "offset" && key != "order" {
|
||||
f.Filter[key] = values[0]
|
||||
}
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
|
||||
// Apply applies an AccountFilter on an orm.Query.
|
||||
func (f *AccountFilter) Apply(q *orm.Query) (*orm.Query, error) {
|
||||
q = q.Apply(f.Pager.Pagination)
|
||||
q = q.Apply(f.Filter.Filters)
|
||||
q = q.Order(f.Order...)
|
||||
return q, nil
|
||||
// Apply applies an AccountFilter on a bun.SelectQuery.
|
||||
func (f *AccountFilter) Apply(q *bun.SelectQuery) *bun.SelectQuery {
|
||||
q = q.Limit(f.Limit).Offset(f.Offset)
|
||||
for key, value := range f.Filter {
|
||||
q = q.Where("? = ?", bun.Ident(key), value)
|
||||
}
|
||||
for _, order := range f.Order {
|
||||
q = q.Order(order)
|
||||
}
|
||||
return q
|
||||
}
|
||||
|
||||
// List applies a filter and returns paginated array of matching results and total count.
|
||||
func (s *AdmAccountStore) List(f *AccountFilter) ([]pwdless.Account, int, error) {
|
||||
a := []pwdless.Account{}
|
||||
count, err := s.db.Model(&a).
|
||||
var a []pwdless.Account
|
||||
count, err := s.db.NewSelect().
|
||||
Model(&a).
|
||||
Apply(f.Apply).
|
||||
SelectAndCount()
|
||||
ScanAndCount(context.Background())
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
|
@ -75,55 +94,90 @@ func (s *AdmAccountStore) List(f *AccountFilter) ([]pwdless.Account, int, error)
|
|||
|
||||
// Create creates a new account.
|
||||
func (s *AdmAccountStore) Create(a *pwdless.Account) error {
|
||||
count, _ := s.db.Model(a).
|
||||
Where("email = ?email").
|
||||
Count()
|
||||
|
||||
if count != 0 {
|
||||
return ErrUniqueEmailConstraint
|
||||
exists, err := s.db.NewSelect().
|
||||
Model((*pwdless.Account)(nil)).
|
||||
Where("email = ?", a.Email).
|
||||
Exists(context.Background())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err := s.db.RunInTransaction(func(tx *pg.Tx) error {
|
||||
err := tx.Insert(a)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
p := &models.Profile{
|
||||
AccountID: a.ID,
|
||||
}
|
||||
return tx.Insert(p)
|
||||
})
|
||||
if exists {
|
||||
return ErrUniqueEmailConstraint
|
||||
}
|
||||
ctx := context.Background()
|
||||
tx, err := s.db.BeginTx(ctx, &sql.TxOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.NewInsert().
|
||||
Model(a).
|
||||
Exec(ctx); err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
p := &models.Profile{
|
||||
AccountID: a.ID,
|
||||
}
|
||||
if _, err := tx.NewInsert().
|
||||
Model(p).
|
||||
Exec(ctx); err != nil {
|
||||
tx.Rollback()
|
||||
|
||||
return err
|
||||
return err
|
||||
}
|
||||
tx.Commit()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get account by ID.
|
||||
func (s *AdmAccountStore) Get(id int) (*pwdless.Account, error) {
|
||||
a := pwdless.Account{ID: id}
|
||||
err := s.db.Select(&a)
|
||||
return &a, err
|
||||
a := &pwdless.Account{ID: id}
|
||||
err := s.db.NewSelect().
|
||||
Model(a).
|
||||
WherePK().
|
||||
Scan(context.Background())
|
||||
return a, err
|
||||
}
|
||||
|
||||
// Update account.
|
||||
func (s *AdmAccountStore) Update(a *pwdless.Account) error {
|
||||
err := s.db.Update(a)
|
||||
_, err := s.db.NewUpdate().
|
||||
Model(a).
|
||||
WherePK().
|
||||
Exec(context.Background())
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete account.
|
||||
func (s *AdmAccountStore) Delete(a *pwdless.Account) error {
|
||||
err := s.db.RunInTransaction(func(tx *pg.Tx) error {
|
||||
if _, err := tx.Model(&jwt.Token{}).
|
||||
Where("account_id = ?", a.ID).
|
||||
Delete(); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.Model(&models.Profile{}).
|
||||
Where("account_id = ?", a.ID).
|
||||
Delete(); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Delete(a)
|
||||
})
|
||||
return err
|
||||
ctx := context.Background()
|
||||
tx, err := s.db.BeginTx(ctx, &sql.TxOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.NewDelete().
|
||||
Model((*jwt.Token)(nil)).
|
||||
Where("account_id = ?", a.ID).
|
||||
Exec(ctx); err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
if _, err := tx.NewDelete().
|
||||
Model((*models.Profile)(nil)).
|
||||
Where("account_id = ?", a.ID).
|
||||
Exec(ctx); err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
if _, err := tx.NewDelete().
|
||||
Model(a).
|
||||
WherePK().
|
||||
Exec(ctx); err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
tx.Commit()
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue