database

package
v0.0.0-...-2e3f1f0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package database is the middleware between the app database and the code. All data (de)serialization (save/load) from a persistent database are handled here. Database specific logic should never escape this package.

To use this package you need to apply migrations to the database if needed/wanted, connect to it (using the database data source name from config), and then initialize an instance of AppDatabase from the DB connection.

For example, this code adds a parameter in `webapi` executable for the database data source name (add it to the main.WebAPIConfiguration structure):

DB struct {
	Filename string `conf:""`
}

This is an example on how to migrate the DB and connect to it:

// Start Database
logger.Println("initializing database support")
db, err := sql.Open("sqlite3", "./foo.db")
if err != nil {
	logger.WithError(err).Error("error opening SQLite DB")
	return fmt.Errorf("opening SQLite: %w", err)
}
defer func() {
	logger.Debug("database stopping")
	_ = db.Close()
}()

Then you can initialize the AppDatabase and pass it to the api package.

Index

Constants

View Source
const (
	SUCCESS = iota
	NO_ROWS
	UNIQUE_FAILED
	ERROR
)
View Source
const LIMIT_STREAM = 10

Variables

This section is empty.

Functions

This section is empty.

Types

type AppDatabase

type AppDatabase interface {
	FindUserByUsername(string, ...int) (utils.User, int, error)
	FindUserByID(int) (utils.User, int, error)
	CreateUser(string) (int, int, error)
	UpdateUser(utils.User) (utils.User, int, error)
	UpdateUsername(int, string) (utils.User, int, error)
	GetUserStream(int, int) ([]utils.Photo, int, error)
	GetUserPhotos(int, int) ([]utils.Photo, int, error)
	CheckIfBanned(int, int) (bool, error)
	GetBanned(int) ([]utils.User, int, error)
	BanUser(int, int) (int, error)
	UnbanUser(int, int) (int, error)
	FollowUser(int, int) (int, error)
	UnfollowUser(int, int) (int, error)
	GetFollowings(int, int) ([]utils.User, int, error)
	GetFollowers(int, int) ([]utils.User, int, error)
	CreatePhoto(utils.Photo) (int, int, error)
	DeletePhoto(int) (int, error)
	GetPhotoById(int) (utils.Photo, int, error)
	LikePhoto(int, int) (utils.Like, int, error)
	UnlikePhoto(int, int) (int, error)
	GetLikesByPhoto(int, int) ([]utils.Like, int, error)
	CommentPhoto(utils.Comment) (int, int, error)
	UncommentPhoto(int, int) (int, error)
	GetCommentById(int) (utils.Comment, int, error)
	GetCommentsByPhoto(int, int) ([]utils.Comment, int, error)

	Ping() error
}

AppDatabase is the high level interface for the DB

func New

func New(db *sql.DB) (AppDatabase, error)

New returns a new instance of AppDatabase based on the SQLite connection `db`. `db` is required - an error will be returned if `db` is `nil`.

Jump to

Keyboard shortcuts

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