upgrade from go-pg to bun

This commit is contained in:
dhax 2024-08-31 18:57:54 +02:00
parent f59f129354
commit 1886be62bc
23 changed files with 415 additions and 385 deletions

View file

@ -3,24 +3,24 @@ package jwt
import (
"time"
"github.com/go-pg/pg/orm"
"github.com/uptrace/bun"
)
// Token holds refresh jwt information.
type Token struct {
ID int `json:"id,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
AccountID int `json:"-"`
ID int `bun:"id,pk,autoincrement" json:"id,omitempty"`
CreatedAt time.Time `bun:"created_at,nullzero,notnull,default:current_timestamp" json:"created_at,omitempty"`
UpdatedAt time.Time `bun:"updated_at,nullzero,notnull,default:current_timestamp" json:"updated_at,omitempty"`
AccountID int `bun:"account_id,notnull" json:"-"`
Token string `json:"-"`
Expiry time.Time `json:"-"`
Mobile bool `sql:",notnull" json:"mobile"`
Identifier string `json:"identifier,omitempty"`
Token string `bun:"token,notnull" json:"-"`
Expiry time.Time `bun:"expiry,notnull" json:"-"`
Mobile bool `bun:"mobile,notnull" json:"mobile"`
Identifier string `bun:"identifier" json:"identifier,omitempty"`
}
// BeforeInsert hook executed before database insert operation.
func (t *Token) BeforeInsert(db orm.DB) error {
func (t *Token) BeforeInsert(db *bun.DB) error {
now := time.Now()
if t.CreatedAt.IsZero() {
t.CreatedAt = now
@ -30,7 +30,7 @@ func (t *Token) BeforeInsert(db orm.DB) error {
}
// BeforeUpdate hook executed before database update operation.
func (t *Token) BeforeUpdate(db orm.DB) error {
func (t *Token) BeforeUpdate(db *bun.DB) error {
t.UpdatedAt = time.Now()
return nil
}

View file

@ -6,28 +6,28 @@ import (
validation "github.com/go-ozzo/ozzo-validation"
"github.com/go-ozzo/ozzo-validation/is"
"github.com/go-pg/pg/orm"
"github.com/uptrace/bun"
"github.com/dhax/go-base/auth/jwt"
)
// Account represents an authenticated application user
type Account struct {
ID int `json:"id"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
LastLogin time.Time `json:"last_login,omitempty"`
ID int `bun:"id,pk,autoincrement" json:"id"`
CreatedAt time.Time `bun:"created_at,nullzero,notnull,default:current_timestamp" json:"created_at,omitempty"`
UpdatedAt time.Time `bun:"updated_at,nullzero,notnull,default:current_timestamp" json:"updated_at,omitempty"`
LastLogin time.Time `bun:"last_login" json:"last_login,omitempty"`
Email string `json:"email"`
Name string `json:"name"`
Active bool `sql:",notnull" json:"active"`
Roles []string `pg:",array" json:"roles,omitempty"`
Email string `bun:"email,notnull" json:"email"`
Name string `bun:"name,notnull" json:"name"`
Active bool `bun:"active,notnull" json:"active"`
Roles []string `bun:"roles,array" json:"roles,omitempty"`
Token []jwt.Token `json:"token,omitempty"`
Token []jwt.Token `bun:"rel:has-many" json:"token,omitempty"`
}
// BeforeInsert hook executed before database insert operation.
func (a *Account) BeforeInsert(db orm.DB) error {
func (a *Account) BeforeInsert(db *bun.DB) error {
now := time.Now()
if a.CreatedAt.IsZero() {
a.CreatedAt = now
@ -37,13 +37,13 @@ func (a *Account) BeforeInsert(db orm.DB) error {
}
// BeforeUpdate hook executed before database update operation.
func (a *Account) BeforeUpdate(db orm.DB) error {
func (a *Account) BeforeUpdate(db *bun.DB) error {
a.UpdatedAt = time.Now()
return a.Validate()
}
// BeforeDelete hook executed before database delete operation.
func (a *Account) BeforeDelete(db orm.DB) error {
func (a *Account) BeforeDelete(db *bun.DB) error {
return nil
}