| | 1 | | using Microsoft.EntityFrameworkCore; |
| | 2 | | using Microsoft.EntityFrameworkCore.Metadata.Builders; |
| | 3 | |
|
| | 4 | | namespace NLightning.Infrastructure.Persistence.EntityConfiguration.Node; |
| | 5 | |
|
| | 6 | | using Domain.Crypto.Constants; |
| | 7 | | using Entities.Node; |
| | 8 | | using Enums; |
| | 9 | | using ValueConverters; |
| | 10 | |
|
| | 11 | | public static class PeerEntityConfiguration |
| | 12 | | { |
| | 13 | | public static void ConfigurePeerEntity(this ModelBuilder modelBuilder, DatabaseType databaseType) |
| | 14 | | { |
| 0 | 15 | | modelBuilder.Entity<PeerEntity>(entity => |
| 0 | 16 | | { |
| 0 | 17 | | // Set PrimaryKey |
| 0 | 18 | | entity.HasKey(e => e.NodeId); |
| 0 | 19 | |
|
| 0 | 20 | | // Set required props |
| 0 | 21 | | entity.Property(e => e.Host).IsRequired(); |
| 0 | 22 | | entity.Property(e => e.Port).IsRequired(); |
| 0 | 23 | | entity.Property(e => e.LastSeenAt).IsRequired(); |
| 0 | 24 | |
|
| 0 | 25 | | // Required byte[] properties |
| 0 | 26 | | entity.Property(e => e.NodeId) |
| 0 | 27 | | .HasConversion<CompactPubKeyConverter>() |
| 0 | 28 | | .IsRequired(); |
| 0 | 29 | |
|
| 0 | 30 | | if (databaseType == DatabaseType.MicrosoftSql) |
| 0 | 31 | | OptimizeConfigurationForSqlServer(entity); |
| 0 | 32 | | }); |
| 0 | 33 | | } |
| | 34 | |
|
| | 35 | | private static void OptimizeConfigurationForSqlServer(EntityTypeBuilder<PeerEntity> entity) |
| | 36 | | { |
| 0 | 37 | | entity.Property(e => e.NodeId).HasColumnType($"varbinary({CryptoConstants.CompactPubkeyLen})"); |
| 0 | 38 | | } |
| | 39 | | } |