db

package
v0.1.32 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2025 License: MIT Imports: 6 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)
	GetArtistsForAlbum(ctx context.Context, id int32) ([]*models.Artist, error)
	GetArtistsForTrack(ctx context.Context, id int32) ([]*models.Artist, 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)
	GetUser(ctx context.Context, id int32) (*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
	UpdateArtistMetadata(ctx context.Context, opts UpdateArtistMetadataParams) error
	UpdateTrack(ctx context.Context, opts UpdateTrackOpts) error
	UpdateTrackMetadata(ctx context.Context, opts UpdateTrackMetadataParams) error
	UpdateAlbum(ctx context.Context, opts UpdateAlbumOpts) error
	UpdateReleaseMetadata(ctx context.Context, opts UpdateReleaseMetadataParams) 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
	SetPrimaryAlbumArtist(ctx context.Context, id int32, artistId int32, value bool) error
	SetPrimaryTrackArtist(ctx context.Context, id int32, artistId int32, value bool) 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, userId int32) 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, userID int32) error
	// Count
	CountListens(ctx context.Context, userID int32, period Period) (int64, error)
	CountTracks(ctx context.Context, userID int32, period Period) (int64, error)
	CountAlbums(ctx context.Context, userID int32, period Period) (int64, error)
	CountArtists(ctx context.Context, userID int32, period Period) (int64, error)
	CountTimeListened(ctx context.Context, userID int32, period Period) (int64, error)
	CountTimeListenedToItem(ctx context.Context, opts TimeListenedOpts) (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, replaceImage bool) error
	MergeArtists(ctx context.Context, fromId, toId int32, replaceImage bool) error
	// Etc
	ImageHasAssociation(ctx context.Context, image uuid.UUID) (bool, error)
	GetImageSource(ctx context.Context, image uuid.UUID) (string, error)
	GetAlbumByImage(ctx context.Context, image uuid.UUID) (*models.Album, error)
	GetArtistByImage(ctx context.Context, image uuid.UUID) (*models.Artist, error)
	AlbumsWithoutImages(ctx context.Context, from int32) ([]*models.Album, error)
	GetExportPage(ctx context.Context, opts GetExportPageOpts) ([]*ExportItem, error)
	// Theme
	SaveUserTheme(ctx context.Context, userId int32, themeData []byte) error
	GetUserTheme(ctx context.Context, userId int32) ([]byte, error)
	// Preferences
	SaveUserPreferences(ctx context.Context, userId int32, preferencesData []byte) error
	GetUserPreferences(ctx context.Context, userId int32) ([]byte, error)
	// Sources
	UpsertClientSource(ctx context.Context, userId int32, name, token string) error
	GetClientSources(ctx context.Context, userId int32) ([]models.ClientSource, error)
	// User
	GetAllUsers(ctx context.Context) ([]*models.User, error)
	DeleteUser(ctx context.Context, id int32) error
	ExistsListenFuzzy(ctx context.Context, arg repository.ExistsListenFuzzyParams) (bool, error)

	// Lifecycle
	Ping(ctx context.Context) error
	Close(ctx context.Context)
}

type ExportItem

type ExportItem struct {
	ListenedAt         time.Time
	UserID             int32
	Client             *string
	TrackID            int32
	TrackMbid          *uuid.UUID
	TrackDuration      int32
	TrackAliases       []models.Alias
	ReleaseID          int32
	ReleaseMbid        *uuid.UUID
	ReleaseImage       *uuid.UUID
	ReleaseImageSource string
	VariousArtists     bool
	ReleaseAliases     []models.Alias
	Artists            []models.ArtistWithFullAliases
}

type GetAlbumOpts

type GetAlbumOpts struct {
	ID            int32
	MusicBrainzID uuid.UUID
	ArtistID      int32
	Title         string
	Titles        []string
	Image         uuid.UUID
	UserID        int32 // Added for isolation
}

type GetArtistOpts

type GetArtistOpts struct {
	ID            int32
	MusicBrainzID uuid.UUID
	Name          string
	Image         uuid.UUID
	UserID        int32 // Added for isolation
}

type GetExportPageOpts

type GetExportPageOpts struct {
	UserID     int32
	ListenedAt time.Time
	TrackID    int32
	Limit      int32
}

type GetItemsOpts

type GetItemsOpts struct {
	UserID int // Added for isolation

	Limit  int
	Period Period
	Page   int
	Week   int // 1-52
	Month  int // 1-12
	Year   int
	From   int // unix timestamp
	To     int // unix timestamp

	// 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
	UserID        int32 // Added for isolation
}

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 {
	UserID   int32 // Added for isolation
	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
	// When true, checks for existing listens within +/- 60s window for the same track
	DeduplicateFuzzy bool
}

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 TimeListenedOpts

type TimeListenedOpts struct {
	UserID   int32 // Added for isolation
	Period   Period
	AlbumID  int32
	ArtistID int32
	TrackID  int32
}

type UpdateAlbumOpts

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

type UpdateApiKeyLabelOpts

type UpdateApiKeyLabelOpts struct {
	UserID int32
	ID     int32
	Label  string
}

type UpdateArtistMetadataParams added in v0.1.30

type UpdateArtistMetadataParams struct {
	ID         int32
	Genres     []string
	Bio        pgtype.Text
	Popularity pgtype.Int4
	SpotifyID  pgtype.Text
	Followers  pgtype.Int4
}

type UpdateArtistOpts

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

type UpdateReleaseMetadataParams added in v0.1.30

type UpdateReleaseMetadataParams struct {
	ID                   int32
	Genres               []string
	ReleaseDate          pgtype.Text
	Popularity           pgtype.Int4
	SpotifyID            pgtype.Text
	Label                pgtype.Text
	ReleaseDatePrecision pgtype.Text
}

type UpdateTrackMetadataParams added in v0.1.30

type UpdateTrackMetadataParams struct {
	ID               int32
	Popularity       pgtype.Int4
	SpotifyID        pgtype.Text
	Danceability     pgtype.Float8
	Energy           pgtype.Float8
	Key              pgtype.Int4
	Loudness         pgtype.Float8
	Mode             pgtype.Int4
	Speechiness      pgtype.Float8
	Acousticness     pgtype.Float8
	Instrumentalness pgtype.Float8
	Liveness         pgtype.Float8
	Valence          pgtype.Float8
	Tempo            pgtype.Float8
}

type UpdateTrackOpts

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

type UpdateUserOpts

type UpdateUserOpts struct {
	ID       int32
	Username string
	Password string
	Role     *models.UserRole
}

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