-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrismaModelGenerator.cs
More file actions
104 lines (90 loc) · 4.26 KB
/
PrismaModelGenerator.cs
File metadata and controls
104 lines (90 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
namespace TopModel.Generator.Prisma;
using Microsoft.Extensions.Logging;
using TopModel.Core;
using TopModel.Generator.Core;
using TopModel.Utils;
/// <summary>
/// Générateur de définitions Typescript.
/// </summary>
public class PrismaModelGenerator : ClassGroupGeneratorBase<PrismaConfig>
{
private readonly ILogger<PrismaModelGenerator> _logger;
public PrismaModelGenerator(ILogger<PrismaModelGenerator> logger)
: base(logger)
{
this._logger = logger;
}
public override string Name => "PrismaModelGen";
protected override IEnumerable<(string FileType, string FileName)> GetFileNames(Class classe, string tag)
{
List<(string FileType, string FileName)> fileNames = new()
{
(string.Empty,
Path.Combine(
this.Config.OutputDirectory,
this.Config.ResolveVariables(this.Config.EntityFilePath!, tag),
$"{classe.Namespace.Module.ToLower()}.prisma"
.Replace("\\", "/"))),
};
return fileNames;
}
protected override void HandleFile(string fileType, string fileName, string tag, IEnumerable<Class> classes)
{
using var fw = new FileWriter(fileName, this._logger, false);
foreach (var classe in classes.Where(c => c.IsPersistent))
{
this.HandleClass(classe, tag, fw);
}
}
private void HandleClass(Class classe, string tag, FileWriter fw)
{
fw.WriteLine($@"model {classe.Name} {{");
var propertyNameIndent = classe.GetProperties(this.Classes).Select(p => p.NameCamel.Length)
.Concat(classe.GetProperties(this.Classes).OfType<AssociationProperty>().Select(a => a.Association.PrimaryKey.First().NameCamel.Length))
.Max();
var propertyTypeIndent = classe.GetProperties(this.Classes).Select(p => this.Config.GetType(p, this.Classes, false).Length)
.Concat(classe.GetProperties(this.Classes).OfType<AssociationProperty>().Select(a => a.Association.NamePascal.Length + (a.Type.IsToMany() ? 2 : 0)))
.Max();
foreach (var property in classe.GetProperties(this.Classes))
{
var line = $@" {property.NameCamel.PadRight(propertyNameIndent, ' ')}";
line += $@" {this.Config.GetType(property, this.Classes, false).PadRight(propertyTypeIndent, ' ')}";
line += $@" @map(""{property.SqlName}"")";
if (property.PrimaryKey)
{
line += " @id";
if (property.Domain.AutoGeneratedValue)
{
line += " @default(autoincrement())";
}
}
if (property is AssociationProperty ap && ap.Type != AssociationType.ManyToMany && property is not ReverseAssociationProperty)
{
var assoLine = $@" {ap.Association.Name.Value.ToCamelCase().PadRight(propertyNameIndent, ' ')}";
assoLine += $" {(ap.Association.NamePascal + (ap.Type.IsToMany() ? "[]" : string.Empty)).PadRight(propertyNameIndent, ' ')}";
assoLine += $" @relation(fields: [{property.NameCamel}], references: [{ap.Association.PrimaryKey.FirstOrDefault()!.NameCamel}])";
fw.WriteLine(assoLine);
}
else if (property is ReverseAssociationProperty rp && rp.Type != AssociationType.ManyToMany)
{
var assoLine = $@" {rp.Association.Name.Value.PadRight(propertyNameIndent, ' ')} {rp.Association.NamePascal}{(rp.ReverseProperty.Type.IsToMany() ? string.Empty : "[]")}";
fw.WriteLine(assoLine);
}
fw.WriteLine(line);
}
fw.WriteLine("}");
if (this.Config.CanClassUseEnums(classe, Classes))
{
fw.WriteLine();
fw.WriteLine($@"enum {this.Config.GetEnumType(classe.PrimaryKey.FirstOrDefault())} {{");
var refs = this.GetAllValues(classe)
.OrderBy(x => x.Name, StringComparer.Ordinal)
.ToList();
foreach (var value in refs)
{
fw.WriteLine(1, $"{value.Value[classe.PrimaryKey.FirstOrDefault()]}");
}
fw.WriteLine("}");
}
}
}