db

package
v0.0.0-...-524bf5b Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package db provides functions for interacting with a database. Only MySQL database is supported for now.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConnectDB

func ConnectDB(driver, dsn string) error

ConnectDB establishes the global connection to a database.

func MySQLDSNConfig

func MySQLDSNConfig(user, passwd, addr, dbname string) *mysql.Config

MySQLDSNConfig is a simple util function for creating a mysql.Config with custom connection options.

Types

type DBConnection

type DBConnection interface {

	// UserByUsername returns a UserDBEntity from database specified by the username
	// parameter. If the username doesn't exist, error is returned.
	UserByUsername(username string) (*UserDBEntity, error)

	// SaveUser saves the UserModel passed as parameter to a database.
	SaveUser(user *UserModel) error

	// Save2FASecret saves secret for 2FA
	Save2FASecret(username, secret string) error

	// Get2FASecret retrieves 2FA secret
	Get2FASecret(username string) (string, error)

	UpdateEnabled2FA(username string, enabled bool) error

	GetEnabled2FA(username string) (bool, error)
}

DBConnection represents a layer between the database and the application logic.

var DBConn DBConnection = nil

DBConnectino is a global connection to a database, through which application logic interacts with database.

type UserDBEntity

type UserDBEntity struct {
	// Uuid is a primary key of the UserDBEntity entry in database.
	Uuid uuid.UUID

	// Username of the user, must be unique.
	Username string

	// Email of the user, must be unique.
	Email string

	// Password hash of users account password.
	PasswordHash string

	// Secret2FA is used during 2FA
	Secret2FA sql.NullString

	// Enabled2FA indicates if user enabled 2FA
	Enabled2FA bool
}

UserDBEntity is a model for how users are represented in database.

func (UserDBEntity) String

func (u UserDBEntity) String() string

String returns string representation of a UserDBEntity.

type UserModel

type UserModel struct {
	// Username of the user, must be unique.
	Username string

	// Email of the user, must be unique.
	Email string

	// Password hash of users account password.
	PasswordHash string
}

UserModel represents a model of an user, which is used in application logic.

func (UserModel) String

func (u UserModel) String() string

String returns string representation of a UserModel.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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