Replace beego/orm with dbx (#2693)

* Start migration to dbx package

* Fix annotations and bookmarks bindings

* Fix tests

* Fix more tests

* Remove remaining references to beego/orm

* Add PostScanner/PostMapper interfaces

* Fix importing SmartPlaylists

* Renaming

* More renaming

* Fix artist DB mapping

* Fix playlist updates

* Remove bookmarks at the end of the test

* Remove remaining `orm` struct tags

* Fix user timestamps DB access

* Fix smart playlist evaluated_at DB access

* Fix search3
This commit is contained in:
Deluan Quintão 2023-12-09 13:52:17 -05:00 committed by GitHub
parent 7074455e0e
commit 0ca0d5da22
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
60 changed files with 461 additions and 376 deletions

View file

@ -8,13 +8,13 @@ import (
"strings"
. "github.com/Masterminds/squirrel"
"github.com/beego/beego/v2/client/orm"
"github.com/deluan/rest"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/utils"
"github.com/pocketbase/dbx"
)
type artistRepository struct {
@ -24,14 +24,40 @@ type artistRepository struct {
}
type dbArtist struct {
model.Artist `structs:",flatten"`
SimilarArtists string `structs:"similar_artists" json:"similarArtists"`
*model.Artist `structs:",flatten"`
SimilarArtists string `structs:"-" json:"similarArtists"`
}
func NewArtistRepository(ctx context.Context, o orm.QueryExecutor) model.ArtistRepository {
func (a *dbArtist) PostScan() error {
if a.SimilarArtists == "" {
return nil
}
for _, s := range strings.Split(a.SimilarArtists, ";") {
fields := strings.Split(s, ":")
if len(fields) != 2 {
continue
}
name, _ := url.QueryUnescape(fields[1])
a.Artist.SimilarArtists = append(a.Artist.SimilarArtists, model.Artist{
ID: fields[0],
Name: name,
})
}
return nil
}
func (a *dbArtist) PostMapArgs(m map[string]any) error {
var sa []string
for _, s := range a.Artist.SimilarArtists {
sa = append(sa, fmt.Sprintf("%s:%s", s.ID, url.QueryEscape(s.Name)))
}
m["similar_artists"] = strings.Join(sa, ";")
return nil
}
func NewArtistRepository(ctx context.Context, db dbx.Builder) model.ArtistRepository {
r := &artistRepository{}
r.ctx = ctx
r.ormer = o
r.db = db
r.indexGroups = utils.ParseIndexGroups(conf.Server.IndexGroups)
r.tableName = "artist"
r.sortMappings = map[string]string{
@ -62,7 +88,7 @@ func (r *artistRepository) Exists(id string) (bool, error) {
func (r *artistRepository) Put(a *model.Artist, colsToUpdate ...string) error {
a.FullText = getFullText(a.Name, a.SortArtistName)
dba := r.fromModel(a)
dba := &dbArtist{Artist: a}
_, err := r.put(dba.ID, dba, colsToUpdate...)
if err != nil {
return err
@ -102,41 +128,11 @@ func (r *artistRepository) GetAll(options ...model.QueryOptions) (model.Artists,
func (r *artistRepository) toModels(dba []dbArtist) model.Artists {
res := model.Artists{}
for i := range dba {
a := dba[i]
res = append(res, *r.toModel(&a))
res = append(res, *dba[i].Artist)
}
return res
}
func (r *artistRepository) toModel(dba *dbArtist) *model.Artist {
a := dba.Artist
a.SimilarArtists = nil
for _, s := range strings.Split(dba.SimilarArtists, ";") {
fields := strings.Split(s, ":")
if len(fields) != 2 {
continue
}
name, _ := url.QueryUnescape(fields[1])
a.SimilarArtists = append(a.SimilarArtists, model.Artist{
ID: fields[0],
Name: name,
})
}
return &a
}
func (r *artistRepository) fromModel(a *model.Artist) *dbArtist {
dba := &dbArtist{Artist: *a}
var sa []string
for _, s := range a.SimilarArtists {
sa = append(sa, fmt.Sprintf("%s:%s", s.ID, url.QueryEscape(s.Name)))
}
dba.SimilarArtists = strings.Join(sa, ";")
return dba
}
func (r *artistRepository) getIndexKey(a *model.Artist) string {
name := strings.ToLower(utils.NoArticle(a.Name))
for k, v := range r.indexGroups {