database

package
v0.0.0-...-a34e95e Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AddPermissionToRoleParams

type AddPermissionToRoleParams struct {
	RoleID       int32 `json:"role_id"`
	PermissionID int32 `json:"permission_id"`
}

type CreateNewInstallationParams

type CreateNewInstallationParams struct {
	InstallationID     uuid.UUID   `json:"installation_id"`
	NotificationToken  pgtype.Text `json:"notification_token"`
	Locale             string      `json:"locale"`
	DeviceManufacturer string      `json:"device_manufacturer"`
	DeviceOs           string      `json:"device_os"`
	DeviceOsVersion    string      `json:"device_os_version"`
	AppVersion         string      `json:"app_version"`
}

type CreateNewLoginOptionParams

type CreateNewLoginOptionParams struct {
	LoginMethod string      `json:"login_method"`
	AccessKey   string      `json:"access_key"`
	HashedPass  pgtype.Text `json:"hashed_pass"`
	PassSalt    pgtype.Text `json:"pass_salt"`
	UserID      int32       `json:"user_id"`
}

type CreateNewOtpParams

type CreateNewOtpParams struct {
	Code           string             `json:"code"`
	Intent         string             `json:"intent"`
	OtpFor         int32              `json:"otp_for"`
	UsingSessionID int32              `json:"using_session_id"`
	ExpiresAt      pgtype.Timestamptz `json:"expires_at"`
}

type CreateNewSessionParams

type CreateNewSessionParams struct {
	Token          string             `json:"token"`
	OriginatedFrom int32              `json:"originated_from"`
	InstallationID pgtype.Int4        `json:"installation_id"`
	ExpiresAt      pgtype.Timestamptz `json:"expires_at"`
}

type CreateNewUserParams

type CreateNewUserParams struct {
	Username     string      `json:"username"`
	ProfileImage pgtype.Text `json:"profile_image"`
	FirstName    string      `json:"first_name"`
	LastName     pgtype.Text `json:"last_name"`
	RoleID       pgtype.Int4 `json:"role_id"`
}

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 GetActiveLoginOptionParams

type GetActiveLoginOptionParams struct {
	LoginMethod string `json:"login_method"`
	AccessKey   string `json:"access_key"`
}

type GetActiveLoginOptionWithUserParams

type GetActiveLoginOptionWithUserParams struct {
	LoginMethod string `json:"login_method"`
	AccessKey   string `json:"access_key"`
}

type GetActiveLoginOptionWithUserRow

type GetActiveLoginOptionWithUserRow struct {
	LoginOptionID          int32              `json:"login_option_id"`
	LoginOptionLoginMethod string             `json:"login_option_login_method"`
	LoginOptionAccessKey   string             `json:"login_option_access_key"`
	LoginOptionHashedPass  pgtype.Text        `json:"login_option_hashed_pass"`
	LoginOptionPassSalt    pgtype.Text        `json:"login_option_pass_salt"`
	LoginOptionVerifiedAt  pgtype.Timestamptz `json:"login_option_verified_at"`
	LoginOptionCreatedAt   pgtype.Timestamptz `json:"login_option_created_at"`
	LoginOptionUpdatedAt   pgtype.Timestamptz `json:"login_option_updated_at"`
	LoginOptionDeletedAt   pgtype.Timestamptz `json:"login_option_deleted_at"`
	UserID                 int32              `json:"user_id"`
	UserUsername           string             `json:"user_username"`
	UserProfileImage       pgtype.Text        `json:"user_profile_image"`
	UserFirstName          string             `json:"user_first_name"`
	UserMiddleName         pgtype.Text        `json:"user_middle_name"`
	UserLastName           pgtype.Text        `json:"user_last_name"`
	UserCreatedAt          pgtype.Timestamptz `json:"user_created_at"`
	UserUpdatedAt          pgtype.Timestamptz `json:"user_updated_at"`
	UserBlockedAt          pgtype.Timestamptz `json:"user_blocked_at"`
	UserDeletedAt          pgtype.Timestamptz `json:"user_deleted_at"`
	UserRoleID             pgtype.Int4        `json:"user_role_id"`
}

type GetActiveOtpByIdPairedWithSessionIdParams

type GetActiveOtpByIdPairedWithSessionIdParams struct {
	ID             int32 `json:"id"`
	UsingSessionID int32 `json:"using_session_id"`
}

type GetAllPermissionsRow

type GetAllPermissionsRow struct {
	ID        int32              `json:"id"`
	Name      string             `json:"name"`
	CreatedAt pgtype.Timestamptz `json:"created_at"`
	UpdatedAt pgtype.Timestamptz `json:"updated_at"`
}

type GetAllRoleRow

type GetAllRoleRow struct {
	ID        int32              `json:"id"`
	Name      string             `json:"name"`
	CreatedAt pgtype.Timestamptz `json:"created_at"`
	UpdatedAt pgtype.Timestamptz `json:"updated_at"`
}

type GetRoleWithItsPermissionsRow

type GetRoleWithItsPermissionsRow struct {
	RoleID         int32  `json:"role_id"`
	RoleName       string `json:"role_name"`
	PermissionID   int32  `json:"permission_id"`
	PermissionName string `json:"permission_name"`
}

type Installation

type Installation struct {
	ID                      int32              `json:"id"`
	InstallationID          uuid.UUID          `json:"installation_id"`
	NotificationToken       pgtype.Text        `json:"notification_token"`
	Locale                  string             `json:"locale"`
	TimezoneOffsetInMinutes int32              `json:"timezone_offset_in_minutes"`
	DeviceManufacturer      string             `json:"device_manufacturer"`
	DeviceOs                string             `json:"device_os"`
	DeviceOsVersion         string             `json:"device_os_version"`
	AppVersion              string             `json:"app_version"`
	CreatedAt               pgtype.Timestamptz `json:"created_at"`
	UpdatedAt               pgtype.Timestamptz `json:"updated_at"`
	DeletedAt               pgtype.Timestamptz `json:"deleted_at"`
}

type LoginOption

type LoginOption struct {
	ID          int32              `json:"id"`
	LoginMethod string             `json:"login_method"`
	AccessKey   string             `json:"access_key"`
	HashedPass  pgtype.Text        `json:"hashed_pass"`
	PassSalt    pgtype.Text        `json:"pass_salt"`
	VerifiedAt  pgtype.Timestamptz `json:"verified_at"`
	CreatedAt   pgtype.Timestamptz `json:"created_at"`
	UpdatedAt   pgtype.Timestamptz `json:"updated_at"`
	DeletedAt   pgtype.Timestamptz `json:"deleted_at"`
	UserID      int32              `json:"user_id"`
}

type Otp

type Otp struct {
	ID             int32              `json:"id"`
	Code           string             `json:"code"`
	HitCount       int16              `json:"hit_count"`
	CreatedAt      pgtype.Timestamptz `json:"created_at"`
	UpdatedAt      pgtype.Timestamptz `json:"updated_at"`
	ExpiresAt      pgtype.Timestamptz `json:"expires_at"`
	DeletedAt      pgtype.Timestamptz `json:"deleted_at"`
	Intent         string             `json:"intent"`
	OtpFor         int32              `json:"otp_for"`
	UsingSessionID int32              `json:"using_session_id"`
}

type Permission

type Permission struct {
	ID        int32              `json:"id"`
	Name      string             `json:"name"`
	CreatedAt pgtype.Timestamptz `json:"created_at"`
	UpdatedAt pgtype.Timestamptz `json:"updated_at"`
	DeletedAt pgtype.Timestamptz `json:"deleted_at"`
}

type Queries

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

func New

func New(db DBTX) *Queries

func (*Queries) AddPermissionToRole

func (q *Queries) AddPermissionToRole(ctx context.Context, arg AddPermissionToRoleParams) error

AddPermissionToRole

INSERT INTO role_permission(role_id, permission_id)
VALUES($1, $2)

func (*Queries) CreateNewInstallation

func (q *Queries) CreateNewInstallation(ctx context.Context, arg CreateNewInstallationParams) (Installation, error)

CreateNewInstallation

INSERT INTO installation (
        installation_id,
        notification_token,
        locale,
        device_manufacturer,
        device_os,
        device_os_version,
        app_version
    )
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING id, installation_id, notification_token, locale, timezone_offset_in_minutes, device_manufacturer, device_os, device_os_version, app_version, created_at, updated_at, deleted_at

func (*Queries) CreateNewLoginOption

func (q *Queries) CreateNewLoginOption(ctx context.Context, arg CreateNewLoginOptionParams) (LoginOption, error)

CreateNewLoginOption

INSERT INTO login_option(
        login_method,
        access_key,
        hashed_pass,
        pass_salt,
        user_id
    )
VALUES ($1, $2, $3, $4, $5)
RETURNING id, login_method, access_key, hashed_pass, pass_salt, verified_at, created_at, updated_at, deleted_at, user_id

func (*Queries) CreateNewOtp

func (q *Queries) CreateNewOtp(ctx context.Context, arg CreateNewOtpParams) (Otp, error)

CreateNewOtp

INSERT INTO otp (
        code,
        intent,
        otp_for,
        using_session_id,
        expires_at
    )
VALUES ($1, $2, $3, $4, $5)
RETURNING id, code, hit_count, created_at, updated_at, expires_at, deleted_at, intent, otp_for, using_session_id

func (*Queries) CreateNewPermission

func (q *Queries) CreateNewPermission(ctx context.Context, name string) (Permission, error)

CreateNewPermission

INSERT INTO permission(name)
VALUES($1)
RETURNING id, name, created_at, updated_at, deleted_at

func (*Queries) CreateNewRole

func (q *Queries) CreateNewRole(ctx context.Context, name string) (Role, error)

CreateNewRole

INSERT INTO role(name)
VALUES($1)
RETURNING id, name, created_at, updated_at, deleted_at

func (*Queries) CreateNewSession

func (q *Queries) CreateNewSession(ctx context.Context, arg CreateNewSessionParams) (Session, error)

CreateNewSession

INSERT INTO session (
        token,
        originated_from,
        installation_id,
        expires_at
    )
VALUES ($1, $2, $3, $4)
RETURNING id, token, created_at, updated_at, expires_at, deleted_at, originated_from, installation_id

func (*Queries) CreateNewUser

func (q *Queries) CreateNewUser(ctx context.Context, arg CreateNewUserParams) (User, error)

CreateNewUser

INSERT INTO users (
        username,
        profile_image,
        first_name,
        last_name,
        role_id
    )
VALUES ($1, $2, $3, $4, $5)
RETURNING id, username, profile_image, first_name, middle_name, last_name, created_at, updated_at, blocked_at, deleted_at, role_id

func (*Queries) GetActiveLoginOption

func (q *Queries) GetActiveLoginOption(ctx context.Context, arg GetActiveLoginOptionParams) (LoginOption, error)

GetActiveLoginOption

SELECT id, login_method, access_key, hashed_pass, pass_salt, verified_at, created_at, updated_at, deleted_at, user_id
FROM login_option
WHERE login_method = $1
    AND access_key = $2
    AND verified_at IS NOT NULL
    AND deleted_at IS NULL
LIMIT 1

func (*Queries) GetActiveLoginOptionWithUser

GetActiveLoginOptionWithUser

SELECT lo.id as login_option_id,
    lo.login_method as login_option_login_method,
    lo.access_key as login_option_access_key,
    lo.hashed_pass as login_option_hashed_pass,
    lo.pass_salt as login_option_pass_salt,
    lo.verified_at as login_option_verified_at,
    lo.created_at as login_option_created_at,
    lo.updated_at as login_option_updated_at,
    lo.deleted_at as login_option_deleted_at,
    u.id as user_id,
    u.username as user_username,
    u.profile_image as user_profile_image,
    u.first_name as user_first_name,
    u.middle_name as user_middle_name,
    u.last_name as user_last_name,
    u.created_at as user_created_at,
    u.updated_at as user_updated_at,
    u.blocked_at as user_blocked_at,
    u.deleted_at as user_deleted_at,
    u.role_id as user_role_id
FROM login_option AS lo
    JOIN users AS u ON lo.user_id = u.id
WHERE lo.login_method = $1
    AND lo.access_key = $2
    AND lo.verified_at IS NOT NULL
    AND lo.deleted_at IS NULL
    AND u.deleted_at IS NULL
LIMIT 1

func (*Queries) GetActiveOtpByIdPairedWithSessionId

func (q *Queries) GetActiveOtpByIdPairedWithSessionId(ctx context.Context, arg GetActiveOtpByIdPairedWithSessionIdParams) (Otp, error)

GetActiveOtpByIdPairedWithSessionId

SELECT id, code, hit_count, created_at, updated_at, expires_at, deleted_at, intent, otp_for, using_session_id
FROM otp
WHERE id = $1
    AND using_session_id = $2
    AND hit_count < 3
    AND expires_at > NOW()
    AND deleted_at IS NULL
LIMIT 1

func (*Queries) GetActiveSessionById

func (q *Queries) GetActiveSessionById(ctx context.Context, id int32) (Session, error)

GetActiveSessionById

SELECT id, token, created_at, updated_at, expires_at, deleted_at, originated_from, installation_id
FROM session
WHERE id = $1
    AND expires_at > NOW()
    AND deleted_at IS NULL
LIMIT 1

func (*Queries) GetActiveSessionByToken

func (q *Queries) GetActiveSessionByToken(ctx context.Context, token string) (Session, error)

GetActiveSessionByToken

SELECT id, token, created_at, updated_at, expires_at, deleted_at, originated_from, installation_id
FROM session
WHERE token = $1
    AND expires_at > NOW()
    AND deleted_at IS NULL
LIMIT 1

func (*Queries) GetAllPermissions

func (q *Queries) GetAllPermissions(ctx context.Context) ([]GetAllPermissionsRow, error)

GetAllPermissions

SELECT id,
    name,
    created_at,
    updated_at
FROM permission

func (*Queries) GetAllRole

func (q *Queries) GetAllRole(ctx context.Context) ([]GetAllRoleRow, error)

GetAllRole

SELECT id,
    name,
    created_at,
    updated_at
FROM role

func (*Queries) GetRoleWithItsPermissions

func (q *Queries) GetRoleWithItsPermissions(ctx context.Context, id int32) ([]GetRoleWithItsPermissionsRow, error)

GetRoleWithItsPermissions

SELECT r.id as role_id,
    r.name as role_name,
    p.id as permission_id,
    p.name as permission_name
FROM role AS r
    JOIN role_permission AS rp ON r.id = rp.role_id
    JOIN permission AS p ON p.id = rp.permission_id
WHERE r.id = $1

func (*Queries) GetUserById

func (q *Queries) GetUserById(ctx context.Context, id int32) (User, error)

GetUserById

SELECT id, username, profile_image, first_name, middle_name, last_name, created_at, updated_at, blocked_at, deleted_at, role_id
FROM users
WHERE id = $1
    AND deleted_at IS NULL
LIMIT 1

func (*Queries) GetUserBySessionToken

func (q *Queries) GetUserBySessionToken(ctx context.Context, token string) (User, error)

GetUserBySessionToken

SELECT u.id, u.username, u.profile_image, u.first_name, u.middle_name, u.last_name, u.created_at, u.updated_at, u.blocked_at, u.deleted_at, u.role_id
FROM session AS s
    JOIN login_option AS lo ON s.originated_from = lo.id
    JOIN users AS u ON u.id = lo.user_id
WHERE s.token = $1
    AND s.deleted_at IS NULL
    AND u.deleted_at IS NULL
    AND lo.deleted_at IS NULL
    AND s.expires_at > NOW()
LIMIT 1

func (*Queries) IsUsernameUsed

func (q *Queries) IsUsernameUsed(ctx context.Context, username string) (int64, error)

IsUsernameUsed

SELECT COUNT(*)
FROM users
WHERE username = $1

func (*Queries) MarkUserLoginOptionAsVerified

func (q *Queries) MarkUserLoginOptionAsVerified(ctx context.Context, id int32) error

MarkUserLoginOptionAsVerified

UPDATE login_option
SET verified_at = NOW()
WHERE id = $1

func (*Queries) RecordHitForOtp

func (q *Queries) RecordHitForOtp(ctx context.Context, id int32) (Otp, error)

RecordHitForOtp

UPDATE otp
SET hit_count = hit_count + 1
WHERE id = $1
RETURNING id, code, hit_count, created_at, updated_at, expires_at, deleted_at, intent, otp_for, using_session_id

func (*Queries) RemovePermissionFromRole

func (q *Queries) RemovePermissionFromRole(ctx context.Context, arg RemovePermissionFromRoleParams) error

RemovePermissionFromRole

DELETE FROM role_permission
WHERE role_id = $1
    AND permission_id = $2

func (*Queries) SetPasswordForUserLoginOption

func (q *Queries) SetPasswordForUserLoginOption(ctx context.Context, arg SetPasswordForUserLoginOptionParams) error

SetPasswordForUserLoginOption

UPDATE login_option
SET hashed_pass = $2,
    pass_salt = $3
WHERE id = $1

func (*Queries) SoftDeleteInstallation

func (q *Queries) SoftDeleteInstallation(ctx context.Context, id int32) error

SoftDeleteInstallation

UPDATE installation
SET deleted_at = NOW()
WHERE id = $1

func (*Queries) SoftDeleteOtp

func (q *Queries) SoftDeleteOtp(ctx context.Context, id int32) error

SoftDeleteOtp

UPDATE otp
SET deleted_at = NOW()
WHERE id = $1

func (*Queries) SoftDeletePermission

func (q *Queries) SoftDeletePermission(ctx context.Context, id int32) error

SoftDeletePermission

UPDATE permission
SET deleted_at = NOW()
WHERE id = $1

func (*Queries) SoftDeleteRole

func (q *Queries) SoftDeleteRole(ctx context.Context, id int32) error

SoftDeleteRole

UPDATE role
SET deleted_at = NOW()
WHERE id = $1

func (*Queries) SoftDeleteSession

func (q *Queries) SoftDeleteSession(ctx context.Context, id int32) error

SoftDeleteSession

UPDATE session
SET deleted_at = NOW()
WHERE id = $1

func (*Queries) SoftDeleteTodo

func (q *Queries) SoftDeleteTodo(ctx context.Context, id int32) error

SoftDeleteTodo

UPDATE todo
SET deleted_at = NOW()
WHERE id = $1

func (*Queries) SoftDeleteUser

func (q *Queries) SoftDeleteUser(ctx context.Context, id int32) error

SoftDeleteUser

UPDATE users
SET deleted_at = NOW()
WHERE id = $1

func (*Queries) SoftDeleteUserLoginOption

func (q *Queries) SoftDeleteUserLoginOption(ctx context.Context, id int32) error

SoftDeleteUserLoginOption

UPDATE login_option
SET deleted_at = NOW()
WHERE id = $1

func (*Queries) UpdateInstallation

func (q *Queries) UpdateInstallation(ctx context.Context, arg UpdateInstallationParams) error

UpdateInstallation

UPDATE installation
SET notification_token = $3,
    locale = $4,
    timezone_Offset_in_minutes = $5
WHERE id = $1
    AND installation_id = $2
    AND deleted_at IS NULL

func (*Queries) WithTx

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

type RemovePermissionFromRoleParams

type RemovePermissionFromRoleParams struct {
	RoleID       int32 `json:"role_id"`
	PermissionID int32 `json:"permission_id"`
}

type Role

type Role struct {
	ID        int32              `json:"id"`
	Name      string             `json:"name"`
	CreatedAt pgtype.Timestamptz `json:"created_at"`
	UpdatedAt pgtype.Timestamptz `json:"updated_at"`
	DeletedAt pgtype.Timestamptz `json:"deleted_at"`
}

type RolePermission

type RolePermission struct {
	RoleID       int32              `json:"role_id"`
	PermissionID int32              `json:"permission_id"`
	CreatedAt    pgtype.Timestamptz `json:"created_at"`
	UpdatedAt    pgtype.Timestamptz `json:"updated_at"`
}

type Service

type Service struct {
	ConnPool *pgxpool.Pool
	Queries  *Queries
}

func NewConnection

func NewConnection(ctx context.Context, log zerolog.Logger) *Service

func (*Service) Close

func (s *Service) Close()

Close closes the database connection. It logs a message indicating the disconnection from the specific database.

func (*Service) Health

func (s *Service) Health(ctx context.Context) map[string]string

Health checks the health of the database connection by pinging the database. It returns a map with keys indicating various health statistics.

type Session

type Session struct {
	ID             int32              `json:"id"`
	Token          string             `json:"token"`
	CreatedAt      pgtype.Timestamptz `json:"created_at"`
	UpdatedAt      pgtype.Timestamptz `json:"updated_at"`
	ExpiresAt      pgtype.Timestamptz `json:"expires_at"`
	DeletedAt      pgtype.Timestamptz `json:"deleted_at"`
	OriginatedFrom int32              `json:"originated_from"`
	InstallationID pgtype.Int4        `json:"installation_id"`
}

type SetPasswordForUserLoginOptionParams

type SetPasswordForUserLoginOptionParams struct {
	ID         int32       `json:"id"`
	HashedPass pgtype.Text `json:"hashed_pass"`
	PassSalt   pgtype.Text `json:"pass_salt"`
}

type Todo

type Todo struct {
	ID        int32              `json:"id"`
	Title     string             `json:"title"`
	Body      pgtype.Text        `json:"body"`
	Status    string             `json:"status"`
	CreatedAt pgtype.Timestamptz `json:"created_at"`
	UpdatedAt pgtype.Timestamptz `json:"updated_at"`
	DeletedAt pgtype.Timestamptz `json:"deleted_at"`
	UserID    int32              `json:"user_id"`
}

type UpdateInstallationParams

type UpdateInstallationParams struct {
	ID                      int32       `json:"id"`
	InstallationID          uuid.UUID   `json:"installation_id"`
	NotificationToken       pgtype.Text `json:"notification_token"`
	Locale                  string      `json:"locale"`
	TimezoneOffsetInMinutes int32       `json:"timezone_offset_in_minutes"`
}

type User

type User struct {
	ID           int32              `json:"id"`
	Username     string             `json:"username"`
	ProfileImage pgtype.Text        `json:"profile_image"`
	FirstName    string             `json:"first_name"`
	MiddleName   pgtype.Text        `json:"middle_name"`
	LastName     pgtype.Text        `json:"last_name"`
	CreatedAt    pgtype.Timestamptz `json:"created_at"`
	UpdatedAt    pgtype.Timestamptz `json:"updated_at"`
	BlockedAt    pgtype.Timestamptz `json:"blocked_at"`
	DeletedAt    pgtype.Timestamptz `json:"deleted_at"`
	RoleID       pgtype.Int4        `json:"role_id"`
}

Jump to

Keyboard shortcuts

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