database

package
v0.0.0-...-c00e9d6 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// min bytes for each session token
	MinSessionBytesPerToken = 32
)

Variables

This section is empty.

Functions

func Migrate

func Migrate(db *sql.DB, dir string) error

this is similar to running from command line goose postgres "host=localhost port=5432 user=xyz dbname=abc password=ppp sslmode=disable" up

func MigrateFS

func MigrateFS(db *sql.DB, migrationsFS fs.FS, dir string) error

this is needed to check if .sql files exist in migrations folder

func Open

func Open(cfg PostgresConfig) (*sql.DB, error)

PS: When Open() is called, it is the responsibility of the caller to close the DB connection with db.Close()

Types

type DB

type DB struct {
	*sqlx.DB
}

func New

func New(dsn string) (*DB, error)

type NewSession

type NewSession struct {
	NEWSession Session
	// Token is only set when creating a new session. When looking up a session this will be left empty, as we only store the hash of a session token in our database and we cannot reverse it into a raw token.
	Token string
}

type PostgresConfig

type PostgresConfig struct {
	Host     string
	Port     string
	User     string
	Password string
	Database string
	SSLMode  string
}

func DefaultPostgresConfig

func DefaultPostgresConfig() PostgresConfig

default setup config from docker compose

func (*PostgresConfig) DSN

func (cfg *PostgresConfig) DSN() string

type Session

type Session struct {
	ID        int
	UserID    int
	TokenHash string
}

type SessionStore

type SessionStore struct {
	DB *sql.DB
	//SessionBytesPerToken sets the length of generated session tokens. If unset or too small, the minimum length (MinSessionBytesPerToken) is used.
	SessionBytesPerToken int
}

func (*SessionStore) Create

func (ss *SessionStore) Create(userID int) (*NewSession, error)

func (*SessionStore) Delete

func (ss *SessionStore) Delete(token string) error

Delete the session associated with the provided token

func (*SessionStore) UserLookup

func (ss *SessionStore) UserLookup(token string) (*User, error)

Once a session is created we will need a way to query our SessionStore to determine who the user is with that session.

type User

type User struct {
	ID           int
	Email        string
	PasswordHash string
}

type UserCreator

type UserCreator interface {
	Create(email, password string) (*User, error)
	Authenticate(email, password string) (*User, error)
}

As of now this interface is not so much useful. But maybe it will be useful in the future

type UserStore

type UserStore struct {
	DB *sql.DB
}

func (*UserStore) Authenticate

func (us *UserStore) Authenticate(email, password string) (*User, error)

func (*UserStore) Create

func (us *UserStore) Create(email, password string) (*User, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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