Move model configuration to its own classes

pull/6522/head
Fernando Fernández 3 years ago
parent c2652d21e1
commit f4af78817d
No known key found for this signature in database
GPG Key ID: 44495B839CCFF8CF

@ -153,100 +153,10 @@ namespace Jellyfin.Server.Implementations
{
modelBuilder.SetDefaultDateTimeKind(DateTimeKind.Utc);
base.OnModelCreating(modelBuilder);
modelBuilder.HasDefaultSchema("jellyfin");
// Collations
modelBuilder.Entity<User>()
.Property(user => user.Username)
.UseCollation("NOCASE");
// Delete behavior
modelBuilder.Entity<User>()
.HasOne(u => u.ProfileImage)
.WithOne()
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<User>()
.HasMany(u => u.Permissions)
.WithOne()
.HasForeignKey(p => p.UserId)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<User>()
.HasMany(u => u.Preferences)
.WithOne()
.HasForeignKey(p => p.UserId)
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<User>()
.HasMany(u => u.AccessSchedules)
.WithOne()
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<User>()
.HasMany(u => u.DisplayPreferences)
.WithOne()
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<User>()
.HasMany(u => u.ItemDisplayPreferences)
.WithOne()
.OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<DisplayPreferences>()
.HasMany(d => d.HomeSections)
.WithOne()
.OnDelete(DeleteBehavior.Cascade);
// Indexes
modelBuilder.Entity<ApiKey>()
.HasIndex(entity => entity.AccessToken)
.IsUnique();
modelBuilder.Entity<User>()
.HasIndex(entity => entity.Username)
.IsUnique();
modelBuilder.Entity<Device>()
.HasIndex(entity => new { entity.DeviceId, entity.DateLastActivity });
modelBuilder.Entity<Device>()
.HasIndex(entity => new { entity.AccessToken, entity.DateLastActivity });
modelBuilder.Entity<Device>()
.HasIndex(entity => new { entity.UserId, entity.DeviceId });
modelBuilder.Entity<Device>()
.HasIndex(entity => entity.DeviceId);
modelBuilder.Entity<DeviceOptions>()
.HasIndex(entity => entity.DeviceId)
.IsUnique();
modelBuilder.Entity<DisplayPreferences>()
.HasIndex(entity => new { entity.UserId, entity.ItemId, entity.Client })
.IsUnique();
modelBuilder.Entity<CustomItemDisplayPreferences>()
.HasIndex(entity => new { entity.UserId, entity.ItemId, entity.Client, entity.Key })
.IsUnique();
// Used to get a user's permissions or a specific permission for a user.
// Also prevents multiple values being created for a user.
// Filtered over non-null user ids for when other entities (groups, API keys) get permissions
modelBuilder.Entity<Permission>()
.HasIndex(p => new { p.UserId, p.Kind })
.HasFilter("[UserId] IS NOT NULL")
.IsUnique();
modelBuilder.Entity<Preference>()
.HasIndex(p => new { p.UserId, p.Kind })
.HasFilter("[UserId] IS NOT NULL")
.IsUnique();
// Configuration for each entity is in it's own class inside 'ModelConfiguratio'.
modelBuilder.ApplyConfigurationsFromAssembly(typeof(JellyfinDb).Assembly);
}
}
}

@ -0,0 +1,22 @@
using Jellyfin.Data.Entities.Security;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Jellyfin.Server.Implementations.ModelConfiguration
{
/// <summary>
/// FluentAPI configuration for the ApiKey entity.
/// </summary>
public class ApiKeyConfiguration : IEntityTypeConfiguration<ApiKey>
{
/// <inheritdoc/>
public void Configure(EntityTypeBuilder<ApiKey> builder)
{
// Indexes
builder
.HasIndex(entity => entity.AccessToken)
.IsUnique();
}
}
}

@ -0,0 +1,22 @@
using Jellyfin.Data.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Jellyfin.Server.Implementations.ModelConfiguration
{
/// <summary>
/// FluentAPI configuration for the CustomItemDisplayPreferences entity.
/// </summary>
public class CustomItemDisplayPreferencesConfiguration : IEntityTypeConfiguration<CustomItemDisplayPreferences>
{
/// <inheritdoc/>
public void Configure(EntityTypeBuilder<CustomItemDisplayPreferences> builder)
{
// Indexes
builder
.HasIndex(entity => new { entity.UserId, entity.ItemId, entity.Client, entity.Key })
.IsUnique();
}
}
}

@ -0,0 +1,30 @@
using Jellyfin.Data.Entities.Security;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Jellyfin.Server.Implementations.ModelConfiguration
{
/// <summary>
/// FluentAPI configuration for the Device entity.
/// </summary>
public class DeviceConfiguration : IEntityTypeConfiguration<Device>
{
/// <inheritdoc/>
public void Configure(EntityTypeBuilder<Device> builder)
{
// Indexes
builder
.HasIndex(entity => new { entity.DeviceId, entity.DateLastActivity });
builder
.HasIndex(entity => new { entity.AccessToken, entity.DateLastActivity });
builder
.HasIndex(entity => new { entity.UserId, entity.DeviceId });
builder
.HasIndex(entity => entity.DeviceId);
}
}
}

@ -0,0 +1,22 @@
using Jellyfin.Data.Entities.Security;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Jellyfin.Server.Implementations.ModelConfiguration
{
/// <summary>
/// FluentAPI configuration for the DeviceOptions entity.
/// </summary>
public class DeviceOptionsConfiguration : IEntityTypeConfiguration<DeviceOptions>
{
/// <inheritdoc/>
public void Configure(EntityTypeBuilder<DeviceOptions> builder)
{
// Indexes
builder
.HasIndex(entity => entity.DeviceId)
.IsUnique();
}
}
}

@ -0,0 +1,29 @@
using Jellyfin.Data.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Jellyfin.Server.Implementations.ModelConfiguration
{
/// <summary>
/// FluentAPI configuration for the DisplayPreferencesConfiguration entity.
/// </summary>
public class DisplayPreferencesConfiguration : IEntityTypeConfiguration<DisplayPreferences>
{
/// <inheritdoc/>
public void Configure(EntityTypeBuilder<DisplayPreferences> builder)
{
// Delete behaviour
builder
.HasMany(d => d.HomeSections)
.WithOne()
.OnDelete(DeleteBehavior.Cascade);
// Indexes
builder
.HasIndex(entity => new { entity.UserId, entity.ItemId, entity.Client })
.IsUnique();
}
}
}

@ -0,0 +1,26 @@
using Jellyfin.Data.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Jellyfin.Server.Implementations.ModelConfiguration
{
/// <summary>
/// FluentAPI configuration for the Permission entity.
/// </summary>
public class PermissionConfiguration : IEntityTypeConfiguration<Permission>
{
/// <inheritdoc/>
public void Configure(EntityTypeBuilder<Permission> builder)
{
// Indexes
// Used to get a user's permissions or a specific permission for a user.
// Also prevents multiple values being created for a user.
// Filtered over non-null user ids for when other entities (groups, API keys) get permissions
builder
.HasIndex(p => new { p.UserId, p.Kind })
.HasFilter("[UserId] IS NOT NULL")
.IsUnique();
}
}
}

@ -0,0 +1,23 @@
using Jellyfin.Data.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Jellyfin.Server.Implementations.ModelConfiguration
{
/// <summary>
/// FluentAPI configuration for the Permission entity.
/// </summary>
public class PreferenceConfiguration : IEntityTypeConfiguration<Preference>
{
/// <inheritdoc/>
public void Configure(EntityTypeBuilder<Preference> builder)
{
// Indexes
builder
.HasIndex(p => new { p.UserId, p.Kind })
.HasFilter("[UserId] IS NOT NULL")
.IsUnique();
}
}
}

@ -0,0 +1,62 @@
using Jellyfin.Data.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Jellyfin.Server.Implementations.ModelConfiguration
{
/// <summary>
/// FluentAPI configuration for the User entity.
/// </summary>
public class UserConfiguration : IEntityTypeConfiguration<User>
{
/// <inheritdoc/>
public void Configure(EntityTypeBuilder<User> builder)
{
// Collations
builder
.Property(user => user.Username)
.UseCollation("NOCASE");
// Delete behavior
builder
.HasOne(u => u.ProfileImage)
.WithOne()
.OnDelete(DeleteBehavior.Cascade);
builder
.HasMany(u => u.Permissions)
.WithOne()
.HasForeignKey(p => p.UserId)
.OnDelete(DeleteBehavior.Cascade);
builder
.HasMany(u => u.Preferences)
.WithOne()
.HasForeignKey(p => p.UserId)
.OnDelete(DeleteBehavior.Cascade);
builder
.HasMany(u => u.AccessSchedules)
.WithOne()
.OnDelete(DeleteBehavior.Cascade);
builder
.HasMany(u => u.DisplayPreferences)
.WithOne()
.OnDelete(DeleteBehavior.Cascade);
builder
.HasMany(u => u.ItemDisplayPreferences)
.WithOne()
.OnDelete(DeleteBehavior.Cascade);
// Indexes
builder
.HasIndex(entity => entity.Username)
.IsUnique();
}
}
}
Loading…
Cancel
Save