Noi
1149f2800d
Moved modules into the assembly itself to simplify development of further features and reduce complexity in building this project. Additionally, many small adjustments were made, including: - Add documentation to most public methods that had it missing - Minor style updates - Updated readme to reflect near-completion of this rewrite - Remove any last remaining references to old project name Kerobot - Update dependencies
52 lines
2.1 KiB
C#
52 lines
2.1 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace RegexBot.Data;
|
|
/// <summary>
|
|
/// Represents a database connection using the settings defined in the bot's global configuration.
|
|
/// </summary>
|
|
public class BotDatabaseContext : DbContext {
|
|
private static string? _npgsqlConnectionString;
|
|
internal static string PostgresConnectionString {
|
|
#if DEBUG
|
|
get {
|
|
if (_npgsqlConnectionString != null) return _npgsqlConnectionString;
|
|
Console.WriteLine($"{nameof(RegexBot)} - {nameof(BotDatabaseContext)} note: Using hardcoded connection string!");
|
|
return _npgsqlConnectionString ?? "Host=localhost;Username=regexbot;Password=rb";
|
|
}
|
|
#else
|
|
get => _npgsqlConnectionString!;
|
|
#endif
|
|
set => _npgsqlConnectionString ??= value;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieves the <seealso cref="CachedUser">user cache</seealso>.
|
|
/// </summary>
|
|
public DbSet<CachedUser> UserCache { get; set; } = null!;
|
|
|
|
/// <summary>
|
|
/// Retrieves the <seealso cref="CachedGuildUser">guild user cache</seealso>.
|
|
/// </summary>
|
|
public DbSet<CachedGuildUser> GuildUserCache { get; set; } = null!;
|
|
|
|
/// <summary>
|
|
/// Retrieves the <seealso cref="CachedGuildMessage">guild message cache</seealso>.
|
|
/// </summary>
|
|
public DbSet<CachedGuildMessage> GuildMessageCache { get; set; } = null!;
|
|
|
|
/// <inheritdoc />
|
|
protected sealed override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
=> optionsBuilder
|
|
.UseNpgsql(PostgresConnectionString)
|
|
.UseSnakeCaseNamingConvention();
|
|
|
|
/// <inheritdoc />
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder) {
|
|
modelBuilder.Entity<CachedUser>(entity => entity.Property(e => e.Discriminator).HasMaxLength(4).IsFixedLength());
|
|
modelBuilder.Entity<CachedGuildUser>(entity => {
|
|
entity.HasKey(e => new { e.UserId, e.GuildId });
|
|
entity.Property(e => e.FirstSeenTime).HasDefaultValueSql("now()");
|
|
});
|
|
modelBuilder.Entity<CachedGuildMessage>(entity => entity.Property(e => e.CreatedAt).HasDefaultValueSql("now()"));
|
|
}
|
|
}
|