db

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

package db defines the database interface

Index

Constants

View Source
const (
	StepDay     StepInterval = "day"
	StepWeek    StepInterval = "week"
	StepMonth   StepInterval = "month"
	StepYear    StepInterval = "year"
	StepDefault StepInterval = "day"

	DefaultRange int = 12
)

Variables

This section is empty.

Functions

func ListenActivityOptsToTimes

func ListenActivityOptsToTimes(opts ListenActivityOpts) (start, end time.Time)

start is the time of 00:00 at the beginning of opts.Range opts.Steps ago, end is the end time of the current opts.Step. E.g. if step is StepWeek and range is 4, start will be the time 00:00 on Sunday on the 4th week ago, and end will be 23:59:59 on Saturday at the end of the current week. If opts.Year (or opts.Year + opts.Month) is provided, start and end will simply by the start and end times of that year/month.

func StartTimeFromPeriod

func StartTimeFromPeriod(p Period) time.Time

Types

type AddArtistsToAlbumOpts

type AddArtistsToAlbumOpts struct {
	AlbumID   int32
	ArtistIDs []int32
}

type DB

type DB interface {
	// Get
	GetArtist(ctx context.Context, opts GetArtistOpts) (*models.Artist, error)
	GetAlbum(ctx context.Context, opts GetAlbumOpts) (*models.Album, error)
	GetTrack(ctx context.Context, opts GetTrackOpts) (*models.Track, error)
	GetTopTracksPaginated(ctx context.Context, opts GetItemsOpts) (*PaginatedResponse[*models.Track], error)
	GetTopArtistsPaginated(ctx context.Context, opts GetItemsOpts) (*PaginatedResponse[*models.Artist], error)
	GetTopAlbumsPaginated(ctx context.Context, opts GetItemsOpts) (*PaginatedResponse[*models.Album], error)
	GetListensPaginated(ctx context.Context, opts GetItemsOpts) (*PaginatedResponse[*models.Listen], error)
	GetListenActivity(ctx context.Context, opts ListenActivityOpts) ([]ListenActivityItem, error)
	GetAllArtistAliases(ctx context.Context, id int32) ([]models.Alias, error)
	GetAllAlbumAliases(ctx context.Context, id int32) ([]models.Alias, error)
	GetAllTrackAliases(ctx context.Context, id int32) ([]models.Alias, error)
	GetApiKeysByUserID(ctx context.Context, id int32) ([]models.ApiKey, error)
	GetUserBySession(ctx context.Context, sessionId uuid.UUID) (*models.User, error)
	GetUserByUsername(ctx context.Context, username string) (*models.User, error)
	GetUserByApiKey(ctx context.Context, key string) (*models.User, error)
	// Save
	SaveArtist(ctx context.Context, opts SaveArtistOpts) (*models.Artist, error)
	SaveArtistAliases(ctx context.Context, id int32, aliases []string, source string) error
	SaveAlbum(ctx context.Context, opts SaveAlbumOpts) (*models.Album, error)
	SaveAlbumAliases(ctx context.Context, id int32, aliases []string, source string) error
	SaveTrack(ctx context.Context, opts SaveTrackOpts) (*models.Track, error)
	SaveTrackAliases(ctx context.Context, id int32, aliases []string, source string) error
	SaveListen(ctx context.Context, opts SaveListenOpts) error
	SaveUser(ctx context.Context, opts SaveUserOpts) (*models.User, error)
	SaveApiKey(ctx context.Context, opts SaveApiKeyOpts) (*models.ApiKey, error)
	SaveSession(ctx context.Context, userId int32, expiresAt time.Time, persistent bool) (*models.Session, error)
	// Update
	UpdateArtist(ctx context.Context, opts UpdateArtistOpts) error
	UpdateTrack(ctx context.Context, opts UpdateTrackOpts) error
	UpdateAlbum(ctx context.Context, opts UpdateAlbumOpts) error
	AddArtistsToAlbum(ctx context.Context, opts AddArtistsToAlbumOpts) error
	UpdateUser(ctx context.Context, opts UpdateUserOpts) error
	UpdateApiKeyLabel(ctx context.Context, opts UpdateApiKeyLabelOpts) error
	RefreshSession(ctx context.Context, sessionId uuid.UUID, expiresAt time.Time) error
	SetPrimaryArtistAlias(ctx context.Context, id int32, alias string) error
	SetPrimaryAlbumAlias(ctx context.Context, id int32, alias string) error
	SetPrimaryTrackAlias(ctx context.Context, id int32, alias string) error
	// Delete
	DeleteArtist(ctx context.Context, id int32) error
	DeleteAlbum(ctx context.Context, id int32) error
	DeleteTrack(ctx context.Context, id int32) error
	DeleteListen(ctx context.Context, trackId int32, listenedAt time.Time) error
	DeleteArtistAlias(ctx context.Context, id int32, alias string) error
	DeleteAlbumAlias(ctx context.Context, id int32, alias string) error
	DeleteTrackAlias(ctx context.Context, id int32, alias string) error
	DeleteSession(ctx context.Context, sessionId uuid.UUID) error
	DeleteApiKey(ctx context.Context, id int32) error
	// Count
	CountListens(ctx context.Context, period Period) (int64, error)
	CountTracks(ctx context.Context, period Period) (int64, error)
	CountAlbums(ctx context.Context, period Period) (int64, error)
	CountArtists(ctx context.Context, period Period) (int64, error)
	CountTimeListened(ctx context.Context, period Period) (int64, error)
	CountUsers(ctx context.Context) (int64, error)
	// Search
	SearchArtists(ctx context.Context, q string) ([]*models.Artist, error)
	SearchAlbums(ctx context.Context, q string) ([]*models.Album, error)
	SearchTracks(ctx context.Context, q string) ([]*models.Track, error)
	// Merge
	MergeTracks(ctx context.Context, fromId, toId int32) error
	MergeAlbums(ctx context.Context, fromId, toId int32) error
	MergeArtists(ctx context.Context, fromId, toId int32) error
	// Etc
	ImageHasAssociation(ctx context.Context, image uuid.UUID) (bool, error)
	GetImageSource(ctx context.Context, image uuid.UUID) (string, error)
	AlbumsWithoutImages(ctx context.Context, from int32) ([]*models.Album, error)
	Ping(ctx context.Context) error
	Close(ctx context.Context)
}

type GetAlbumOpts

type GetAlbumOpts struct {
	ID            int32
	MusicBrainzID uuid.UUID
	ArtistID      int32
	Title         string
	Titles        []string
	Image         uuid.UUID
}

type GetArtistOpts

type GetArtistOpts struct {
	ID            int32
	MusicBrainzID uuid.UUID
	Name          string
	Image         uuid.UUID
}

type GetItemsOpts

type GetItemsOpts struct {
	Limit  int
	Period Period
	Page   int
	Week   int // 1-52
	Month  int // 1-12
	Year   int

	// Used only for getting top tracks
	ArtistID int
	AlbumID  int

	// Used for getting listens
	TrackID int
}

type GetTrackOpts

type GetTrackOpts struct {
	ID            int32
	MusicBrainzID uuid.UUID
	Title         string
	ArtistIDs     []int32
}

type InformationSource

type InformationSource string
const (
	InformationSourceInferred     InformationSource = "Inferred"
	InformationSourceMusicBrainz  InformationSource = "MusicBrainz"
	InformationSourceUserProvided InformationSource = "User"
)

type ListenActivityItem

type ListenActivityItem struct {
	Start   time.Time `json:"start_time"`
	Listens int64     `json:"listens"`
}

type ListenActivityOpts

type ListenActivityOpts struct {
	Step     StepInterval
	Range    int
	Month    int
	Year     int
	AlbumID  int32
	ArtistID int32
	TrackID  int32
}

type PaginatedResponse

type PaginatedResponse[T any] struct {
	Items        []T   `json:"items"`
	TotalCount   int64 `json:"total_record_count"`
	ItemsPerPage int32 `json:"items_per_page"`
	HasNextPage  bool  `json:"has_next_page"`
	CurrentPage  int32 `json:"current_page"`
}

type Period

type Period string
const (
	PeriodDay     Period = "day"
	PeriodWeek    Period = "week"
	PeriodMonth   Period = "month"
	PeriodYear    Period = "year"
	PeriodAllTime Period = "all_time"
	PeriodDefault Period = "day"
)

type SaveAlbumOpts

type SaveAlbumOpts struct {
	Title          string
	MusicBrainzID  uuid.UUID
	Type           string
	ArtistIDs      []int32
	VariousArtists bool
	Image          uuid.UUID
	ImageSrc       string
	Aliases        []string
}

type SaveApiKeyOpts

type SaveApiKeyOpts struct {
	Key    string
	UserID int32
	Label  string
}

type SaveArtistOpts

type SaveArtistOpts struct {
	Name          string
	MusicBrainzID uuid.UUID
	Aliases       []string
	Image         uuid.UUID
	ImageSrc      string
}

type SaveListenOpts

type SaveListenOpts struct {
	TrackID int32
	Time    time.Time
	UserID  int32
	Client  string
}

type SaveTrackOpts

type SaveTrackOpts struct {
	Title          string
	AlbumID        int32
	ArtistIDs      []int32
	RecordingMbzID uuid.UUID
	Duration       int32
}

type SaveUserOpts

type SaveUserOpts struct {
	Username string
	Password string
	Role     models.UserRole
}

type StepInterval

type StepInterval string

type UpdateAlbumOpts

type UpdateAlbumOpts struct {
	ID            int32
	MusicBrainzID uuid.UUID
	Image         uuid.UUID
	ImageSrc      string
}

type UpdateApiKeyLabelOpts

type UpdateApiKeyLabelOpts struct {
	UserID int32
	ID     int32
	Label  string
}

type UpdateArtistOpts

type UpdateArtistOpts struct {
	ID            int32
	MusicBrainzID uuid.UUID
	Image         uuid.UUID
	ImageSrc      string
}

type UpdateTrackOpts

type UpdateTrackOpts struct {
	ID            int32
	MusicBrainzID uuid.UUID
	Duration      int32
}

type UpdateUserOpts

type UpdateUserOpts struct {
	ID       int32
	Username string
	Password string
}

Directories

Path Synopsis
package psql implements the db.DB interface using psx and a sql generated repository
package psql implements the db.DB interface using psx and a sql generated repository

Jump to

Keyboard shortcuts

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