database

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: MIT Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const NotificationLease = 60 * time.Second

NotificationLease exceeds the worker attempt deadline to allow crash recovery

Variables

View Source
var (
	// ErrNotFound is used when a record is not found
	ErrNotFound = errors.New("not found")
	// ErrUserExists is used when username/email already exists
	ErrUserExists = errors.New("user already exists")
	// ErrOAuthLinkExists is used when a user already has an OAuth link for the same provider
	ErrOAuthLinkExists = errors.New("oauth link already exists for this provider")
)
View Source
var ErrLeaseLost = errors.New("notification lease lost")

ErrLeaseLost means the claim has expired or another worker owns it

Functions

This section is empty.

Types

type EmailUnsubscribe

type EmailUnsubscribe struct {
	ID    string `db:"id"`
	Email string `db:"email"`
}

EmailUnsubscribe represents an email unsubscribe record

func NewEmailUnsubscribe

func NewEmailUnsubscribe(email string) EmailUnsubscribe

NewEmailUnsubscribe creates a new email unsubscribe record

type EmailVerification

type EmailVerification struct {
	ID               string         `db:"id"`
	UserID           string         `db:"user_id"`
	CodeHash         sql.NullString `db:"verification_code"`
	MessageID        sql.NullString `db:"message_id"`
	UnsubscribeToken sql.NullString `db:"unsubscribe_token"`
	DateCreated      time.Time      `db:"date_created"`
}

EmailVerification represents an email verification record

func NewEmailVerification

func NewEmailVerification(userID, codeHash, unsubscribeToken string, createdAt time.Time) EmailVerification

NewEmailVerification creates a new email verification record

func (*EmailVerification) IsExpired

func (ev *EmailVerification) IsExpired() bool

IsExpired checks if the verification code has expired

type Notification added in v1.2.0

type Notification struct {
	ID          string    `db:"id"`
	ReferenceID string    `db:"reference_id"`
	Kind        string    `db:"kind"`
	Payload     []byte    `db:"payload"`
	Attempts    int       `db:"attempts"`
	LockedBy    string    `db:"locked_by"`
	LockedAt    time.Time `db:"locked_at"`
	ExpiresAt   time.Time `db:"expires_at"`
	CreatedAt   time.Time `db:"created_at"`
	Reclaimed   bool      `db:"reclaimed"`
}

Notification contains the JSON payload and the exact lease returned by a claim

type NotificationStats added in v1.2.0

type NotificationStats struct {
	Pending   int64   `db:"pending"`
	OldestAge float64 `db:"oldest_age"`
}

NotificationStats describes the shared queue, not one worker process

type RefreshToken

type RefreshToken struct {
	ID          string    `db:"id"`
	UserID      string    `db:"user_id"`
	TokenHash   string    `db:"token_hash"`
	ExpiresAt   time.Time `db:"expires_at"`
	DateCreated time.Time `db:"date_created"`
}

RefreshToken represents a refresh token

func NewRefreshToken

func NewRefreshToken(userID, tokenHash string, expiresAt time.Time) RefreshToken

NewRefreshToken creates a new refresh token

func (*RefreshToken) IsExpired

func (rt *RefreshToken) IsExpired() bool

IsExpired checks if the refresh token has expired

type User

type User struct {
	ID            string         `db:"id"`
	Username      string         `db:"username"`
	DisplayName   string         `db:"name"`
	Email         sql.NullString `db:"email"`
	EmailVerified bool           `db:"email_verified"`
	PasswordHash  []byte         `db:"password_hash"`
	Role          model.Role     `db:"role"`
	DateCreated   time.Time      `db:"date_created"`
	DateUpdated   sql.NullTime   `db:"date_updated"`
}

User represents a user

func NewUser

func NewUser(username, name string, passwordHash []byte, role model.Role) User

NewUser creates a new user

func (*User) IsEmpty

func (u *User) IsEmpty() bool

IsEmpty returns whether User has ID

func (*User) SetEmail

func (u *User) SetEmail(email string, verified bool)

SetEmail sets user email and verification status

type UserOAuthLink struct {
	UserID        string `db:"user_id"`
	OAuthProvider string `db:"oauth_provider"`
	OAuthID       string `db:"oauth_id"`
}

UserOAuthLink represents a link between a user and an OAuth provider

func NewUserOAuthLink(userID, provider, oauthID string) UserOAuthLink

NewUserOAuthLink creates a new user OAuth link

type UserRepo

type UserRepo struct {
	// contains filtered or unexported fields
}

UserRepo manages API for user access

func NewUserRepo

func NewUserRepo(db *sqlx.DB, log *zap.Logger) *UserRepo

NewUserRepo constructs a user Repo

func (*UserRepo) CheckUserExists

func (r *UserRepo) CheckUserExists(ctx context.Context, name string, role model.Role) (bool, error)

CheckUserExists checks whether user with provided name and role exists

func (*UserRepo) ClaimNotification added in v1.2.0

func (r *UserRepo) ClaimNotification(ctx context.Context, owner string) (Notification, error)

ClaimNotification commits its short row lock before the provider is contacted

func (*UserRepo) CompleteNotification added in v1.2.0

func (r *UserRepo) CompleteNotification(ctx context.Context, n Notification, messageID string) error

CompleteNotification acknowledges delivery and records the provider message ID in one atomic statement

func (*UserRepo) CreateEmailUnsubscribe

func (r *UserRepo) CreateEmailUnsubscribe(ctx context.Context, unsubscribe EmailUnsubscribe) error

CreateEmailUnsubscribe creates a new email unsubscribe record

func (*UserRepo) CreateEmailVerification

func (r *UserRepo) CreateEmailVerification(ctx context.Context, vrf EmailVerification) error

CreateEmailVerification creates a new email verification record

func (*UserRepo) CreateNotification added in v1.2.0

func (r *UserRepo) CreateNotification(ctx context.Context, n Notification) error

CreateNotification participates in the caller's transaction without querying the referenced entity

func (*UserRepo) CreateRefreshToken

func (r *UserRepo) CreateRefreshToken(ctx context.Context, refreshToken RefreshToken) error

CreateRefreshToken inserts a new refresh token into the database

func (*UserRepo) CreateUser

func (r *UserRepo) CreateUser(ctx context.Context, user User) error

CreateUser inserts a new user into the database

func (r *UserRepo) CreateUserOAuthLink(ctx context.Context, link UserOAuthLink) error

CreateUserOAuthLink inserts a new user OAuth link, ignoring duplicates for the same provider+oauth_id pair. Returns ErrOAuthLinkExists if the user already has a link for the given provider

func (*UserRepo) DeleteExpiredRefreshTokens

func (r *UserRepo) DeleteExpiredRefreshTokens(ctx context.Context) error

DeleteExpiredRefreshTokens deletes all expired refresh tokens

func (*UserRepo) DeleteRefreshToken

func (r *UserRepo) DeleteRefreshToken(ctx context.Context, tokenHash string) error

DeleteRefreshToken deletes a refresh token by token hash string

func (*UserRepo) DeleteRefreshTokensByUserID

func (r *UserRepo) DeleteRefreshTokensByUserID(ctx context.Context, userID string) error

DeleteRefreshTokensByUserID deletes all refresh tokens for a user

func (*UserRepo) DeleteUser

func (r *UserRepo) DeleteUser(ctx context.Context, userID string) error

DeleteUser deletes a user by user id

func (*UserRepo) ExpireNotifications added in v1.2.0

func (r *UserRepo) ExpireNotifications(ctx context.Context) error

ExpireNotifications ignores retry eligibility, including long provider Retry-After delays

func (*UserRepo) FinishNotification added in v1.2.0

func (r *UserRepo) FinishNotification(ctx context.Context, n Notification, status, reason string) error

FinishNotification clears the payload after a terminal delivery decision

func (*UserRepo) GetEmailVerificationByUserID

func (r *UserRepo) GetEmailVerificationByUserID(ctx context.Context, userID string) (EmailVerification, error)

GetEmailVerificationByUserID gets email verification by user ID (most recent unused)

func (*UserRepo) GetRefreshTokenByHash

func (r *UserRepo) GetRefreshTokenByHash(ctx context.Context, tokenHash string) (RefreshToken, error)

GetRefreshTokenByHash retrieves a refresh token by token hash string with row-level lock

func (*UserRepo) GetUserByEmail

func (r *UserRepo) GetUserByEmail(ctx context.Context, email string) (User, error)

GetUserByEmail gets user by email address

func (*UserRepo) GetUserByID

func (r *UserRepo) GetUserByID(ctx context.Context, userID string) (user User, err error)

GetUserByID returns user by id

func (r *UserRepo) GetUserByOAuthLink(ctx context.Context, provider string, oauthID string) (User, error)

GetUserByOAuthLink returns a user by oauth provider and oauth_id via user_oauth_links table

func (*UserRepo) GetUserByUsername

func (r *UserRepo) GetUserByUsername(ctx context.Context, username string) (user User, err error)

GetUserByUsername returns user by username

func (*UserRepo) GetVerificationState added in v1.2.0

func (r *UserRepo) GetVerificationState(ctx context.Context, userID, email string) (VerificationState, error)

GetVerificationState returns the database time, whether email is unsubscribed and the user's latest unused code, locking that code. Call it after locking the user row so the latest code is read from a fresh snapshot

func (r *UserRepo) HasOAuthLink(ctx context.Context, userID string) (bool, error)

HasOAuthLink checks if a user has any OAuth link

func (*UserRepo) IsEmailUnsubscribed

func (r *UserRepo) IsEmailUnsubscribed(ctx context.Context, email string) (bool, error)

IsEmailUnsubscribed checks if an email address is unsubscribed

func (*UserRepo) NotificationEligible added in v1.2.0

func (r *UserRepo) NotificationEligible(ctx context.Context, n Notification, email string) (bool, error)

NotificationEligible reports whether a still-owned claim targets a pending code for an unverified, subscribed publisher at email. It runs just before sending, without holding locks over HTTP

func (*UserRepo) NotificationStats added in v1.2.0

func (r *UserRepo) NotificationStats(ctx context.Context) (NotificationStats, error)

NotificationStats returns queue depth and age for monitoring

func (*UserRepo) PurgeNotifications added in v1.2.0

func (r *UserRepo) PurgeNotifications(ctx context.Context) error

PurgeNotifications removes a bounded batch of old terminal metadata

func (*UserRepo) RetryNotification added in v1.2.0

func (r *UserRepo) RetryNotification(ctx context.Context, n Notification, delay time.Duration, reason string) error

RetryNotification releases an owned lease and persists the next attempt time

func (*UserRepo) RunWithTx

func (r *UserRepo) RunWithTx(ctx context.Context, f func(context.Context) error) error

RunWithTx runs a function in a transaction

func (*UserRepo) SetEmailVerificationUsed

func (r *UserRepo) SetEmailVerificationUsed(ctx context.Context, id string, verified bool) error

SetEmailVerificationUsed sets email verification as used by clearing code hash and optionally setting verified_at

func (*UserRepo) SetUnsubscribeToken

func (r *UserRepo) SetUnsubscribeToken(ctx context.Context, id string, token string) error

SetUnsubscribeToken sets the unsubscribe token for an email verification record

func (*UserRepo) SetUserEmailVerified

func (r *UserRepo) SetUserEmailVerified(ctx context.Context, userID string) error

SetUserEmailVerified sets user email as verified

func (*UserRepo) UpdateUser

func (r *UserRepo) UpdateUser(ctx context.Context, user User) error

UpdateUser updates user

type VerificationState added in v1.2.0

type VerificationState struct {
	Now           time.Time      `db:"now"`
	Unsubscribed  bool           `db:"unsubscribed"`
	LatestID      sql.NullString `db:"latest_id"`
	LatestCreated sql.NullTime   `db:"latest_created"`
}

VerificationState holds what queueing a verification code depends on

Jump to

Keyboard shortcuts

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