Foxchat.NET/Foxchat.Identity/Database/IdentityContext.cs

77 lines
2.7 KiB
C#
Raw Permalink Normal View History

2024-05-11 15:26:47 +02:00
using Foxchat.Core;
using Foxchat.Core.Database;
using Foxchat.Core.Extensions;
2024-05-11 15:26:47 +02:00
using Foxchat.Identity.Database.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
2024-05-19 17:20:45 +02:00
using Npgsql;
2024-05-11 15:26:47 +02:00
namespace Foxchat.Identity.Database;
public class IdentityContext : IDatabaseContext
{
2024-05-19 17:20:45 +02:00
private readonly NpgsqlDataSource _dataSource;
private readonly ILoggerFactory? _loggerFactory;
2024-05-11 15:26:47 +02:00
public override DbSet<Instance> Instance { get; set; }
2024-05-11 15:26:47 +02:00
public DbSet<Account> Accounts { get; set; }
public DbSet<ChatInstance> ChatInstances { get; set; }
2024-05-19 17:20:45 +02:00
public DbSet<Application> Applications { get; set; }
2024-05-11 15:26:47 +02:00
public DbSet<Token> Tokens { get; set; }
public DbSet<GuildAccount> GuildAccounts { get; set; }
public IdentityContext(InstanceConfig config, ILoggerFactory? loggerFactory)
2024-05-11 15:26:47 +02:00
{
2024-05-19 17:20:45 +02:00
var connString = new NpgsqlConnectionStringBuilder(config.Database.Url)
2024-05-11 15:26:47 +02:00
{
Timeout = config.Database.Timeout ?? 5,
MaxPoolSize = config.Database.MaxPoolSize ?? 50,
}.ConnectionString;
2024-05-19 17:20:45 +02:00
var dataSourceBuilder = new NpgsqlDataSourceBuilder(connString);
dataSourceBuilder.UseNodaTime();
_dataSource = dataSourceBuilder.Build();
_loggerFactory = loggerFactory;
2024-05-11 15:26:47 +02:00
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseNpgsql(_dataSource, o => o.UseNodaTime())
.UseSnakeCaseNamingConvention()
.UseLoggerFactory(_loggerFactory);
2024-05-11 15:26:47 +02:00
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
// ULIDs are stored as UUIDs in the database
configurationBuilder.Properties<Ulid>().HaveConversion<UlidConverter>();
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Account>().HasIndex(a => a.Username).IsUnique();
modelBuilder.Entity<Account>().HasIndex(a => a.Email).IsUnique();
modelBuilder.Entity<ChatInstance>().HasIndex(i => i.Domain).IsUnique();
modelBuilder.Entity<GuildAccount>().HasKey(g => new { g.ChatInstanceId, g.GuildId, g.AccountId });
2024-05-19 17:20:45 +02:00
modelBuilder.Entity<Application>().HasIndex(a => a.ClientId).IsUnique();
2024-05-11 15:26:47 +02:00
}
}
// ReSharper disable once UnusedType.Global
2024-05-11 15:26:47 +02:00
public class DesignTimeIdentityContextFactory : IDesignTimeDbContextFactory<IdentityContext>
{
public IdentityContext CreateDbContext(string[] args)
{
// Read the configuration file
var config = new ConfigurationBuilder()
.AddConfiguration("identity.ini")
.Build()
// Get the configuration as our config class
.Get<InstanceConfig>() ?? new();
return new IdentityContext(config, null);
2024-05-11 15:26:47 +02:00
}
}