db

package
v0.0.0-...-7735b63 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	UniqueViolation     = "23505"
	ForeignKeyViolation = "23503"
)

Variables

View Source
var ErrRecordNotFound = pgx.ErrNoRows // doing this because sql.ErrNoWRows returns a different string message than pgx does, so we made this for our unit tests
View Source
var ErrUniqueViolation = &pgconn.PgError{
	Code: UniqueViolation,
}

Functions

func ErrorCode

func ErrorCode(err error) string

Types

type CreatVerifyEmailParams

type CreatVerifyEmailParams struct {
	Username   string `json:"username"`
	Email      string `json:"email"`
	SecretCode string `json:"secret_code"`
}

type CreateSessionParams

type CreateSessionParams struct {
	ID           uuid.UUID `json:"id"`
	Username     string    `json:"username"`
	RefreshToken string    `json:"refresh_token"`
	UserAgent    string    `json:"user_agent"`
	ClientIp     string    `json:"client_ip"`
	IsBlocked    bool      `json:"is_blocked"`
	ExpiresAt    time.Time `json:"expires_at"`
}

type CreateUserParams

type CreateUserParams struct {
	Username       string `json:"username"`
	HashedPassword string `json:"hashed_password"`
	FullName       string `json:"full_name"`
	Email          string `json:"email"`
}

type CreateUserTxParams

type CreateUserTxParams struct {
	CreateUserParams
	//This is a callback function that will only be called after the user is created
	AfterCreate func(user User) error // output will determine weather to complete or rollback db tx
}

type CreateUserTxResult

type CreateUserTxResult struct {
	User User
}

CreateUserTxResult is the result of the transfer transaction

type DBTX

type DBTX interface {
	Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
	Query(context.Context, string, ...interface{}) (pgx.Rows, error)
	QueryRow(context.Context, string, ...interface{}) pgx.Row
}

type Querier

type Querier interface {
	CreatVerifyEmail(ctx context.Context, arg CreatVerifyEmailParams) (VerifyEmail, error)
	CreateSession(ctx context.Context, arg CreateSessionParams) (Session, error)
	CreateUser(ctx context.Context, arg CreateUserParams) (User, error)
	GetSession(ctx context.Context, id uuid.UUID) (Session, error)
	GetUser(ctx context.Context, username string) (User, error)
	UpdateUser(ctx context.Context, arg UpdateUserParams) (User, error)
	UpdateVerifyEmail(ctx context.Context, arg UpdateVerifyEmailParams) (VerifyEmail, error)
}

type Queries

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

func New

func New(db DBTX) *Queries

func (*Queries) CreatVerifyEmail

func (q *Queries) CreatVerifyEmail(ctx context.Context, arg CreatVerifyEmailParams) (VerifyEmail, error)

func (*Queries) CreateSession

func (q *Queries) CreateSession(ctx context.Context, arg CreateSessionParams) (Session, error)

func (*Queries) CreateUser

func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, error)

func (*Queries) GetSession

func (q *Queries) GetSession(ctx context.Context, id uuid.UUID) (Session, error)

func (*Queries) GetUser

func (q *Queries) GetUser(ctx context.Context, username string) (User, error)

func (*Queries) UpdateUser

func (q *Queries) UpdateUser(ctx context.Context, arg UpdateUserParams) (User, error)

func (*Queries) UpdateVerifyEmail

func (q *Queries) UpdateVerifyEmail(ctx context.Context, arg UpdateVerifyEmailParams) (VerifyEmail, error)

func (*Queries) WithTx

func (q *Queries) WithTx(tx pgx.Tx) *Queries

type SQLStore

type SQLStore struct {
	*Queries // all individual Query functions provided by `Queries` is available to Store struct
	// contains filtered or unexported fields
}

SQLStore provides all functions to execute db queries and transactions For individual queries, we already have the Queries struct in db.go (generated by sqlc) each query does 1 operation per table so it doesnt support transactions (COMMIT/ROLLBACK) This file will extend its functionality to support Transactions by embedding it in the store struct (This is called composition) It is the preferred way to extend struct functionality in goLang instead of inheritance

func (*SQLStore) CreateUserTx

func (store *SQLStore) CreateUserTx(ctx context.Context, arg CreateUserTxParams) (CreateUserTxResult, error)

func (*SQLStore) VerifyEmailTx

func (store *SQLStore) VerifyEmailTx(ctx context.Context, arg VerifyEmailTxParams) (VerifyEmailTxResult, error)

type Session

type Session struct {
	ID           uuid.UUID `json:"id"`
	Username     string    `json:"username"`
	RefreshToken string    `json:"refresh_token"`
	UserAgent    string    `json:"user_agent"`
	ClientIp     string    `json:"client_ip"`
	IsBlocked    bool      `json:"is_blocked"`
	ExpiresAt    time.Time `json:"expires_at"`
}

type Store

type Store interface {
	Querier
	CreateUserTx(ctx context.Context, arg CreateUserTxParams) (CreateUserTxResult, error)
	VerifyEmailTx(ctx context.Context, arg VerifyEmailTxParams) (VerifyEmailTxResult, error)
}

Store was created to be able to mock the DB for our tests. note : we updated sqlc.yaml with `emit_interface: true`, then we ran `make sqlc` so we won't have to copy everything that Queries implements into store interface (Check querier.go)

func NewStore

func NewStore(connPool *pgxpool.Pool) Store

type UpdateUserParams

type UpdateUserParams struct {
	HashedPassword   pgtype.Text        `json:"hashed_password"`
	PasswordChangeAt pgtype.Timestamptz `json:"password_change_at"`
	FullName         pgtype.Text        `json:"full_name"`
	Email            pgtype.Text        `json:"email"`
	IsEmailVerified  pgtype.Bool        `json:"is_email_verified"`
	Username         string             `json:"username"`
}

type UpdateVerifyEmailParams

type UpdateVerifyEmailParams struct {
	ID         int64  `json:"id"`
	SecretCode string `json:"secret_code"`
}

type User

type User struct {
	Username         string    `json:"username"`
	HashedPassword   string    `json:"hashed_password"`
	FullName         string    `json:"full_name"`
	Email            string    `json:"email"`
	IsEmailVerified  bool      `json:"is_email_verified"`
	PasswordChangeAt time.Time `json:"password_change_at"`
	CreatedAt        time.Time `json:"created_at"`
	Role             string    `json:"role"`
}

type VerifyEmail

type VerifyEmail struct {
	ID         int64     `json:"id"`
	Username   string    `json:"username"`
	Email      string    `json:"email"`
	SecretCode string    `json:"secret_code"`
	IsUsed     bool      `json:"is_used"`
	CreatedAt  time.Time `json:"created_at"`
	ExpiredAt  time.Time `json:"expired_at"`
}

type VerifyEmailTxParams

type VerifyEmailTxParams struct {
	EmailId    int64
	SecretCode string
}

type VerifyEmailTxResult

type VerifyEmailTxResult struct {
	User        User
	VerifyEmail VerifyEmail
}

Jump to

Keyboard shortcuts

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