Skip to content

Commit dec3ada

Browse files
committed
feat: new relationships following navidrome 0.55.0 BFR
1 parent 7c4a7e0 commit dec3ada

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

lib/handlers/dbManager.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const AlbumDefinition = require('../models/Album');
44
const AnnotationDefinition = require('../models/Annotation');
55
const ArtistDefinition = require('../models/Artist');
66
const UserDefinition = require('../models/User');
7+
const MediaFileArtistDefinition = require('../models/MediaFileArtist');
78

89
exports.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' });

lib/models/MediaFileArtist.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const { DataTypes } = require('sequelize');
2+
3+
exports.attributes = {
4+
media_file_id: { type: DataTypes.STRING, primaryKey: true, allowNull: false },
5+
artist_id: { type: DataTypes.STRING, allowNull: false, defaultValue: '', primaryKey: true },
6+
role: { type: DataTypes.STRING, allowNull: false, defaultValue: '', primaryKey: true }
7+
};
8+
9+
exports.options = {
10+
tableName: 'media_file_artist',
11+
timestamps: false,
12+
indexes: [
13+
{
14+
fields: ['media_file_id', 'artist_id', 'role'],
15+
unique: true
16+
}
17+
]
18+
};

0 commit comments

Comments
 (0)