data

package
v0.0.0-...-ab9459b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 2, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrDuplicateKey = errors.New("db.err.duplicate_key")
View Source
var ErrObjectNotFound = errors.New("db.err.object_not_found")
View Source
var ErrTooManyRows = errors.New("db.err.too_many_rows")

Functions

func Exec

func Exec(ctx context.Context, q Querier, sql string, args ...any) (pgconn.CommandTag, error)

func GenerateRandomSecret

func GenerateRandomSecret(length int) (string, error)

func HashToken

func HashToken(token string) string

func QueryMany

func QueryMany[T any](ctx context.Context, q Querier, sql string, args ...any) ([]T, error)

func QueryOne

func QueryOne[T any](ctx context.Context, q Querier, sql string, args ...any) (T, error)

func WithTransaction

func WithTransaction(ctx context.Context, pool *pgxpool.Pool, fn func(Querier) error) error

Types

type Entity

type Entity struct {
	ID        uuid.UUID  `json:"id" db:"id"`
	CreatedAt time.Time  `json:"created_at" db:"created_at"`
	UpdatedAt *time.Time `json:"updated_at" db:"updated_at"`
	DeletedAt *time.Time `json:"deleted_at" db:"deleted_at"`
}

type OAuthAuthorizationCode

type OAuthAuthorizationCode struct {
	ID                  uuid.UUID  `db:"id"`
	CreatedAt           time.Time  `db:"created_at"`
	Code                string     `db:"code"`
	UserID              uuid.UUID  `db:"user_id"`
	RedirectURI         string     `db:"redirect_uri"`
	CodeChallenge       string     `db:"code_challenge"`
	CodeChallengeMethod string     `db:"code_challenge_method"`
	ExpiresAt           time.Time  `db:"expires_at"`
	UsedAt              *time.Time `db:"used_at"`
}

func NewOAuthAuthorizationCode

func NewOAuthAuthorizationCode(plainCode string, userID uuid.UUID, redirectURI, challenge, method string) *OAuthAuthorizationCode

type PasswordResetToken

type PasswordResetToken struct {
	ID        uuid.UUID  `db:"id"`
	UserID    uuid.UUID  `db:"user_id"`
	Token     string     `db:"token"`
	CreatedAt time.Time  `db:"created_at"`
	ExpiresAt time.Time  `db:"expires_at"`
	UsedAt    *time.Time `db:"used_at"`
}

func NewPasswordResetToken

func NewPasswordResetToken(plainToken string, userID uuid.UUID) *PasswordResetToken

type Querier

type Querier interface {
	Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
	Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)
}

Querier is satisfied by both *pgxpool.Pool and pgx.Tx, allowing the generic helpers below to be used inside transactions as well as outside them.

type Session

type Session struct {
	ID         uuid.UUID `db:"id"`
	UserID     uuid.UUID `db:"user_id"`
	IpAddress  string    `db:"ip_address"`
	UserAgent  string    `db:"user_agent"`
	CreatedAt  time.Time `db:"created_at"`
	ExpiresAt  time.Time `db:"expires_at"`
	LastSeenAt time.Time `db:"last_seen_at"`
}

type Store

type Store interface {
	UserByEmail(ctx context.Context, email string) (*User, error)
	User(ctx context.Context, id uuid.UUID) (*User, error)
	CreateUser(ctx context.Context, user *User) (*User, error)
	UpdateUserLastLogin(ctx context.Context, userID uuid.UUID, loginAt time.Time) error
	CreateSession(ctx context.Context, session *Session) (*Session, error)
	UserAndSessionBySessionID(ctx context.Context, sessionID uuid.UUID) (*User, *Session, error)
	DeleteSession(ctx context.Context, sessionID uuid.UUID) error
	CreateOAuthCode(ctx context.Context, code *OAuthAuthorizationCode) (*OAuthAuthorizationCode, error)
	OAuthCodeByHash(ctx context.Context, codeHash string) (*OAuthAuthorizationCode, error)
	ConsumeOAuthCode(ctx context.Context, codeID uuid.UUID, usedAt time.Time) error
	CreatePasswordResetToken(ctx context.Context, token *PasswordResetToken) (*PasswordResetToken, error)
	PasswordResetTokenByHash(ctx context.Context, hash string) (*PasswordResetToken, error)
	ConsumePasswordResetToken(ctx context.Context, id uuid.UUID, usedAt time.Time) error
	UpdateUserPassword(ctx context.Context, userID uuid.UUID, passwordHash string) error
}

type User

type User struct {
	Entity

	Email         string     `json:"email" db:"email"`
	IsEnabled     bool       `json:"is_enabled" db:"is_enabled"`
	SecurityStamp uuid.UUID  `json:"security_stamp" db:"security_stamp"`
	LastLoginAt   *time.Time `json:"last_login_at" db:"last_login_at"`
	PasswordHash  *string    `json:"-" db:"password_hash"`
}

func NewUser

func NewUser(email string) *User

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL