using Azaion.Common.Entities; using Azaion.Common.Extensions; using LinqToDB; using LinqToDB.Mapping; using Newtonsoft.Json; namespace Azaion.Common.Database; public static class AzaionDbSchemaHolder { public static readonly MappingSchema MappingSchema; static AzaionDbSchemaHolder() { MappingSchema = new MappingSchema(); MappingSchema.EntityDescriptorCreatedCallback = (_, entityDescriptor) => { foreach (var entityDescriptorColumn in entityDescriptor.Columns) entityDescriptorColumn.ColumnName = entityDescriptorColumn.ColumnName.ToSnakeCase(); }; var builder = new FluentMappingBuilder(MappingSchema); builder.Entity() .HasTableName("users") .Property(x => x.Id) .IsPrimaryKey() .HasDataType(DataType.Guid) .Property(x => x.Role) .HasDataType(DataType.Text) .HasConversion(v => v.ToString(), v => (RoleEnum)Enum.Parse(typeof(RoleEnum), v)) .Property(x => x.UserConfig) .HasConversion( v => v == null ? null : JsonConvert.SerializeObject(v), p => string.IsNullOrEmpty(p) ? new UserConfig() : JsonConvert.DeserializeObject(p)) .IsNullable() // AZ-534 — mfa_recovery_codes is JSONB; tell the provider so Npgsql sends // the JSON type oid instead of text (otherwise inserts fail with // "column is of type jsonb but expression is of type text"). .Property(x => x.MfaRecoveryCodes) .HasDataType(DataType.BinaryJson); builder.Entity() .HasTableName("detection_classes") .Property(x => x.Id) .IsPrimaryKey() .IsIdentity(); builder.Entity() .HasTableName("audit_events") .Property(x => x.Id) .IsPrimaryKey() .IsIdentity(); builder.Entity() .HasTableName("sessions") .Property(x => x.Id) .IsPrimaryKey() .HasDataType(DataType.Guid); builder.Build(); } }