@@ -4,6 +4,7 @@ const AlbumDefinition = require('../models/Album');
44const AnnotationDefinition = require ( '../models/Annotation' ) ;
55const ArtistDefinition = require ( '../models/Artist' ) ;
66const UserDefinition = require ( '../models/User' ) ;
7+ const MediaFileArtistDefinition = require ( '../models/MediaFileArtist' ) ;
78
89exports . init = async dbFilePath => {
910 const sequelize = new Sequelize ( {
@@ -28,6 +29,9 @@ const createRelationships = async sequelize => {
2829 // handling legacy Annotation schema (ann_id removed from navidrome DB schema in this commit: https://github.com/navidrome/navidrome/commit/47378c68828861751b9d1a05d3fc9b29ce8dd9f0)
2930 const annotationTableSchema = await sequelize . getQueryInterface ( ) . describeTable ( 'annotation' ) ;
3031
32+ // Following Big Fucking Refactor (Navidrome >= 0.55.0), many-to-many relationship exists between Track and Artist via MediaFileArtist table.
33+ const hasMediaFileArtists = await sequelize . getQueryInterface ( ) . tableExists ( 'media_file_artists' ) ;
34+
3135 const Track = sequelize . define ( 'Track' , TrackDefinition . attributes , TrackDefinition . options ) ;
3236 const Album = sequelize . define ( 'Album' , AlbumDefinition . attributes , AlbumDefinition . options ) ;
3337 const Annotation = sequelize . define (
@@ -37,12 +41,40 @@ const createRelationships = async sequelize => {
3741 ) ;
3842 const Artist = sequelize . define ( 'Artist' , ArtistDefinition . attributes , ArtistDefinition . options ) ;
3943 const User = sequelize . define ( 'User' , UserDefinition . attributes , UserDefinition . options ) ;
44+ const MediaFileArtist = sequelize . define (
45+ 'MediaFileArtist' ,
46+ MediaFileArtistDefinition . attributes ,
47+ MediaFileArtistDefinition . options
48+ ) ;
4049
4150 Track . belongsTo ( Album , { foreignKey : 'album_id' } ) ;
4251 Track . hasOne ( Annotation , { as : 'trackAnnotation' , foreignKey : 'item_id' } ) ;
4352 Track . belongsTo ( Artist , { as : 'artist' , foreignKey : 'artist_id' } ) ;
4453
45- Artist . hasMany ( Track , { as : 'tracks' , foreignKey : 'artist_id' } ) ;
54+ if ( ! hasMediaFileArtists ) {
55+ Artist . hasMany ( Track , { as : 'tracks' , foreignKey : 'artist_id' } ) ;
56+ } else {
57+ Artist . belongsToMany ( Track , {
58+ through : MediaFileArtist ,
59+ foreignKey : 'artist_id' ,
60+ otherKey : 'media_file_id' ,
61+ scope : {
62+ role : 'artist'
63+ } ,
64+ as : 'tracks'
65+ } ) ;
66+
67+ Track . belongsToMany ( Artist , {
68+ through : MediaFileArtist ,
69+ foreignKey : 'media_file_id' ,
70+ otherKey : 'artist_id' ,
71+ scope : {
72+ role : 'artist'
73+ } ,
74+ as : 'tracks'
75+ } ) ;
76+ }
77+
4678 Artist . hasOne ( Annotation , { as : 'artistAnnotation' , foreignKey : 'item_id' } ) ;
4779
4880 Album . hasMany ( Track , { as : 'tracks' , foreignKey : 'album_id' } ) ;
0 commit comments