database

package
v0.0.0-...-3a4e6df Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	UsersTable = "users"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type DbMaker

type DbMaker func() *gorm.DB

func MakePostgresDb

func MakePostgresDb() DbMaker

Creates a pointer to a gorm db.

This uses environmental variables for the dsn.

A connection is then opened, checked for errors and returned.

Keys for environmental variables:

  • DB_ADDR : stores the address (IP)
  • DB_PORT : stores the port
  • DB_USER : stores the username
  • DB_PASS : stores the password
  • DB_NAME : stores the database name

func MakeSQLiteDb

func MakeSQLiteDb() DbMaker

Creates a pointer to a gorm db.

This uses an environmental variable for the path to the sqlite db.

Keys for environmental variables:

  • SQLITE_DB : file name of sqlite db.

type Store

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

Storers a *gorm.DB and its mutex.

func New

func New(m DbMaker) *Store

Make a new Storer.

*gorm.DB is made from .env - follow .env.example pattern.

func (*Store) AddUser

func (s *Store) AddUser(m models.UserModel, t string) error

Adds the given UserModel to the given table.

func (*Store) CheckUser

func (s *Store) CheckUser(i string, t string) bool

Checks if a given user id exists in the given table.

func (*Store) Close

func (s *Store) Close() error

Close the database

func (*Store) CreateTable

func (s *Store) CreateTable(n string, m interface{}) error

func (*Store) DeleteUser

func (s *Store) DeleteUser(i string, t string) error

Deletes the given user id in the given table.

func (*Store) GetLeaderboard

func (s *Store) GetLeaderboard(t string) ([]models.UserModel, error)

Gets the leaderboard, the, at max, top 10 users in the table by elo.

func (*Store) GetUser

func (s *Store) GetUser(i string, t string) (models.UserModel, error)

Returns a user with the given id and table.

func (*Store) ListUsers

func (s *Store) ListUsers(t string) ([]models.UserModel, error)

Lists all users stored in the given table.

func (*Store) Ping

func (s *Store) Ping() error

Ping the storer.

If not connected an error will be returned.

func (*Store) UpdateUser

func (s *Store) UpdateUser(m models.UserModel, t string) error

Updates the given UserModel in the given table.

Jump to

Keyboard shortcuts

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