@solidstarters/solid-code-builder 1.0.11 → 1.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/code-builder/lib/field/FieldManager.d.ts +2 -1
- package/dist/code-builder/lib/field/FieldManager.js +18 -5
- package/dist/code-builder/lib/field/FieldManager.js.map +1 -1
- package/dist/code-builder/lib/field/decorator-managers/entity/IndexDecoratorManager.js +1 -1
- package/dist/code-builder/lib/field/decorator-managers/entity/IndexDecoratorManager.js.map +1 -1
- package/dist/code-builder/lib/field/field-managers/base/BaseFieldManagerForEntity.d.ts +3 -0
- package/dist/code-builder/lib/field/field-managers/base/BaseFieldManagerForEntity.js +15 -3
- package/dist/code-builder/lib/field/field-managers/base/BaseFieldManagerForEntity.js.map +1 -1
- package/dist/code-builder/lib/field/field-managers/relation/ManyToManyInverseRelationFieldHandler.d.ts +13 -0
- package/dist/code-builder/lib/field/field-managers/relation/ManyToManyInverseRelationFieldHandler.js +33 -0
- package/dist/code-builder/lib/field/field-managers/relation/ManyToManyInverseRelationFieldHandler.js.map +1 -0
- package/dist/code-builder/lib/field/field-managers/relation/ManyToManyInverseRelationFieldManagerForDto.d.ts +4 -0
- package/dist/code-builder/lib/field/field-managers/relation/ManyToManyInverseRelationFieldManagerForDto.js +9 -0
- package/dist/code-builder/lib/field/field-managers/relation/ManyToManyInverseRelationFieldManagerForDto.js.map +1 -0
- package/dist/code-builder/lib/field/field-managers/relation/ManyToManyInverseRelationFieldManagerForEntity.d.ts +4 -0
- package/dist/code-builder/lib/field/field-managers/relation/ManyToManyInverseRelationFieldManagerForEntity.js +8 -0
- package/dist/code-builder/lib/field/field-managers/relation/ManyToManyInverseRelationFieldManagerForEntity.js.map +1 -0
- package/dist/code-builder/lib/field/field-managers/relation/ManyToManyRelationFieldManagerForDto.d.ts +1 -16
- package/dist/code-builder/lib/field/field-managers/relation/ManyToManyRelationFieldManagerForDto.js +15 -156
- package/dist/code-builder/lib/field/field-managers/relation/ManyToManyRelationFieldManagerForDto.js.map +1 -1
- package/dist/code-builder/lib/field/field-managers/relation/ManyToManyRelationFieldManagerForEntity.d.ts +1 -20
- package/dist/code-builder/lib/field/field-managers/relation/ManyToManyRelationFieldManagerForEntity.js +0 -109
- package/dist/code-builder/lib/field/field-managers/relation/ManyToManyRelationFieldManagerForEntity.js.map +1 -1
- package/dist/code-builder/lib/field/field-managers/relation/ManyToOneRelationFieldManagerForDto.d.ts +2 -17
- package/dist/code-builder/lib/field/field-managers/relation/ManyToOneRelationFieldManagerForDto.js +4 -175
- package/dist/code-builder/lib/field/field-managers/relation/ManyToOneRelationFieldManagerForDto.js.map +1 -1
- package/dist/code-builder/lib/field/field-managers/relation/ManyToOneRelationFieldManagerForEntity.d.ts +1 -22
- package/dist/code-builder/lib/field/field-managers/relation/ManyToOneRelationFieldManagerForEntity.js +1 -133
- package/dist/code-builder/lib/field/field-managers/relation/ManyToOneRelationFieldManagerForEntity.js.map +1 -1
- package/dist/code-builder/lib/field/field-managers/relation/OneToManyRelationFieldHandler.d.ts +13 -0
- package/dist/code-builder/lib/field/field-managers/relation/OneToManyRelationFieldHandler.js +33 -0
- package/dist/code-builder/lib/field/field-managers/relation/OneToManyRelationFieldHandler.js.map +1 -0
- package/dist/code-builder/lib/field/field-managers/relation/OneToManyRelationFieldManagerForDto.d.ts +34 -0
- package/dist/code-builder/lib/field/field-managers/relation/OneToManyRelationFieldManagerForDto.js +202 -0
- package/dist/code-builder/lib/field/field-managers/relation/OneToManyRelationFieldManagerForDto.js.map +1 -0
- package/dist/code-builder/lib/field/field-managers/relation/OneToManyRelationFieldManagerForEntity.d.ts +9 -0
- package/dist/code-builder/lib/field/field-managers/relation/OneToManyRelationFieldManagerForEntity.js +43 -0
- package/dist/code-builder/lib/field/field-managers/relation/OneToManyRelationFieldManagerForEntity.js.map +1 -0
- package/package.json +1 -1
- package/src/code-builder/lib/field/FieldManager.ts +19 -6
- package/src/code-builder/lib/field/decorator-managers/entity/IndexDecoratorManager.ts +1 -1
- package/src/code-builder/lib/field/field-managers/base/BaseFieldManagerForEntity.ts +19 -3
- package/src/code-builder/lib/field/field-managers/relation/ManyToManyInverseRelationFieldHandler.ts +52 -0
- package/src/code-builder/lib/field/field-managers/relation/ManyToManyInverseRelationFieldManagerForDto.ts +8 -0
- package/src/code-builder/lib/field/field-managers/relation/ManyToManyInverseRelationFieldManagerForEntity.ts +8 -0
- package/src/code-builder/lib/field/field-managers/relation/ManyToManyRelationFieldManagerForDto.ts +16 -189
- package/src/code-builder/lib/field/field-managers/relation/ManyToManyRelationFieldManagerForEntity.ts +2 -147
- package/src/code-builder/lib/field/field-managers/relation/ManyToOneRelationFieldManagerForDto.ts +6 -215
- package/src/code-builder/lib/field/field-managers/relation/ManyToOneRelationFieldManagerForEntity.ts +3 -174
- package/src/code-builder/lib/field/field-managers/relation/OneToManyRelationFieldHandler.ts +52 -0
- package/src/code-builder/lib/field/field-managers/relation/OneToManyRelationFieldManagerForDto.ts +247 -0
- package/src/code-builder/lib/field/field-managers/relation/OneToManyRelationFieldManagerForEntity.ts +51 -0
package/src/code-builder/lib/field/field-managers/relation/ManyToOneRelationFieldManagerForDto.ts
CHANGED
|
@@ -1,12 +1,8 @@
|
|
|
1
|
-
import { classify, dasherize } from '@angular-devkit/core/src/utils/strings';
|
|
2
1
|
import { Tree } from '@angular-devkit/schematics';
|
|
3
2
|
import ts, { PropertyDeclaration } from '@schematics/angular/third_party/github.com/Microsoft/TypeScript/lib/typescript';
|
|
4
|
-
import { Change } from '@schematics/angular/utility/change';
|
|
5
|
-
import { ArrayDecoratorManager } from '../../decorator-managers/dto/ArrayDecoratorManager';
|
|
6
3
|
import { OptionalDecoratorManager } from '../../decorator-managers/dto/OptionalDecoratorManager';
|
|
7
4
|
import { StringDecoratorManager } from '../../decorator-managers/dto/StringDecoratorManager';
|
|
8
|
-
import {
|
|
9
|
-
import { DecoratorManager, DecoratorType, DtoSourceType, FieldChange, FieldManager, FieldType, ManagerForDtoOptions, createSourceFile, safeInsertImport } from '../../FieldManager';
|
|
5
|
+
import { FieldChange, FieldManager, FieldType, ManagerForDtoOptions } from '../../FieldManager';
|
|
10
6
|
import { BaseFieldManagerForDto } from '../base/BaseFieldManagerForDto';
|
|
11
7
|
|
|
12
8
|
export class ManyToOneRelationFieldManagerForDto
|
|
@@ -18,31 +14,9 @@ export class ManyToOneRelationFieldManagerForDto
|
|
|
18
14
|
isBoolean(): boolean {
|
|
19
15
|
return false;
|
|
20
16
|
}
|
|
21
|
-
relationInverseSource: ts.SourceFile;
|
|
22
|
-
relationInverseDecoratorManagers: DecoratorManager[];
|
|
23
17
|
|
|
24
18
|
constructor(tree: Tree, moduleName: string, modelName: string, field: any, options: ManagerForDtoOptions) {
|
|
25
19
|
super(tree, moduleName, modelName, {...field, required: false}, options);
|
|
26
|
-
if (this.field.relationCreateInverse) {
|
|
27
|
-
const relatedEntityFileName = (this.options.sourceType === DtoSourceType.Create) ? `create-${dasherize(this.field.relationModelSingularName)}.dto.ts` : `update-${dasherize(this.field.relationModelSingularName)}.dto.ts`;
|
|
28
|
-
const relatedEntityPath = this.field.relationModelModuleName ? `src/${dasherize(this.field.relationModelModuleName)}/dtos/${relatedEntityFileName}` : `src/${dasherize(moduleName)}/dtos/${relatedEntityFileName}`;
|
|
29
|
-
this.relationInverseSource = createSourceFile(tree, relatedEntityPath);
|
|
30
|
-
this.relationInverseDecoratorManagers = this.getFieldDecoratorManagers(
|
|
31
|
-
this.field,
|
|
32
|
-
this.relationInverseSource,
|
|
33
|
-
DecoratorType.Array,
|
|
34
|
-
DecoratorType.ValidateNested,
|
|
35
|
-
// DecoratorType.Transform,
|
|
36
|
-
);
|
|
37
|
-
//HACK: This is a hack to add the transform decorator to the inverse field
|
|
38
|
-
this.relationInverseDecoratorManagers.push(
|
|
39
|
-
new TransformDecoratorManager({ isTransform: true, type: this.transformType(), source: this.relationInverseSource, field: field })
|
|
40
|
-
);
|
|
41
|
-
//HACK: This is a hack to replace the optional decorator to the inverse field
|
|
42
|
-
this.relationInverseDecoratorManagers.push(
|
|
43
|
-
new OptionalDecoratorManager({ isApplyOptional: true, optional: true, source: this.relationInverseSource, field: field })
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
20
|
}
|
|
47
21
|
|
|
48
22
|
isString(): boolean {
|
|
@@ -87,36 +61,12 @@ export class ManyToOneRelationFieldManagerForDto
|
|
|
87
61
|
};
|
|
88
62
|
}
|
|
89
63
|
|
|
90
|
-
// This field type is used in the case of one-to-many relation, when we generate the inverse field in the related entity
|
|
91
|
-
additionalFieldType(): FieldType {
|
|
92
|
-
const type = this.transformType() //TODO Discuss if this needs to be Create Or Update DTO
|
|
93
|
-
const text = `${type}[]`
|
|
94
|
-
return {
|
|
95
|
-
text: text,
|
|
96
|
-
node: (_field: any) =>
|
|
97
|
-
ts.factory.createArrayTypeNode(ts.factory.createTypeReferenceNode(
|
|
98
|
-
ts.factory.createIdentifier(type),
|
|
99
|
-
undefined
|
|
100
|
-
)),
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
private transformType(): string {
|
|
105
|
-
return `Update${classify(this.modelName)}Dto`;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
64
|
override addAdditionalField(): FieldChange[] {
|
|
109
65
|
const fieldChanges: FieldChange[] = [];
|
|
110
66
|
|
|
111
67
|
// Add the userKey field to the main entity
|
|
112
68
|
fieldChanges.push(this.addAdditionalUserKeyField());
|
|
113
69
|
|
|
114
|
-
if (this.field.relationCreateInverse) {
|
|
115
|
-
fieldChanges.push(this.addAdditionalInverseField());
|
|
116
|
-
fieldChanges.push(this.addAdditionalInverseIdsField());
|
|
117
|
-
fieldChanges.push(this.addAdditionalInverseCommandsField());
|
|
118
|
-
}
|
|
119
|
-
|
|
120
70
|
return fieldChanges;
|
|
121
71
|
}
|
|
122
72
|
|
|
@@ -136,71 +86,12 @@ export class ManyToOneRelationFieldManagerForDto
|
|
|
136
86
|
return fieldChange;
|
|
137
87
|
}
|
|
138
88
|
|
|
139
|
-
private addAdditionalInverseCommandsField(): FieldChange {
|
|
140
|
-
const fieldName = `${this.field.relationModelFieldName ?? this.modelName}Command`;
|
|
141
|
-
const fieldType = "string";
|
|
142
|
-
const source = this.relationInverseSource;
|
|
143
|
-
const field = this.field;
|
|
144
|
-
const modelName = this.field.relationModelSingularName;
|
|
145
|
-
const decoratorManagers = [
|
|
146
|
-
new StringDecoratorManager({ isString: true, source: source, field: field }),
|
|
147
|
-
new OptionalDecoratorManager({ isApplyOptional: true, optional: true, source: source, field: field }),
|
|
148
|
-
// TODO pending @IsEnum(RelationFieldsCommand)
|
|
149
|
-
];
|
|
150
|
-
|
|
151
|
-
//console.log(`\ncreate Dto addAdditionalInverseCommandsField ${fieldName} called ...`);
|
|
152
|
-
|
|
153
|
-
const fieldChange = this.addFieldInternal(fieldName, fieldType, decoratorManagers, field, modelName, source);
|
|
154
|
-
|
|
155
|
-
return fieldChange;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
private addAdditionalInverseIdsField(): FieldChange {
|
|
159
|
-
const fieldName = `${this.field.relationModelFieldName ?? this.modelName}Ids`;
|
|
160
|
-
const fieldType = "number[]";
|
|
161
|
-
const source = this.relationInverseSource;
|
|
162
|
-
const field = this.field;
|
|
163
|
-
const modelName = this.field.relationModelSingularName;
|
|
164
|
-
const decoratorManagers = [
|
|
165
|
-
new OptionalDecoratorManager({ isApplyOptional: true, optional: true, source: source, field: field }),
|
|
166
|
-
new ArrayDecoratorManager({ isArray: true, source: source, field: field })
|
|
167
|
-
]
|
|
168
|
-
|
|
169
|
-
//console.log(`\ncreate Dto addAdditionalInverseIdsField ${fieldName} called ...`);
|
|
170
|
-
|
|
171
|
-
const fieldChange = this.addFieldInternal(fieldName, fieldType, decoratorManagers, field, modelName, source);
|
|
172
|
-
|
|
173
|
-
return fieldChange;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
private addAdditionalInverseField() {
|
|
177
|
-
const fieldName = this.additionalFieldName();
|
|
178
|
-
const fieldType = this.additionalFieldType().text;
|
|
179
|
-
const source = this.relationInverseSource;
|
|
180
|
-
const field = this.field;
|
|
181
|
-
const modelName = this.field.relationModelSingularName;
|
|
182
|
-
const decoratorManagers = this.relationInverseDecoratorManagers;
|
|
183
|
-
//console.log(`\ncreate Dto addAdditionalInverseField ${fieldName} called ...`);
|
|
184
|
-
const fieldChange = this.addFieldInternal(fieldName, fieldType, decoratorManagers, field, modelName, source);
|
|
185
|
-
|
|
186
|
-
if (this.modelName !== this.field.relationModelSingularName) {
|
|
187
|
-
const currentModelName = this.modelName;
|
|
188
|
-
const currentModuleName = this.moduleName;
|
|
189
|
-
fieldChange.changes.push(this.inverseFieldImport(currentModelName, currentModuleName, source));
|
|
190
|
-
}
|
|
191
|
-
return fieldChange;
|
|
192
|
-
}
|
|
193
89
|
|
|
194
90
|
override addOrUpdateAdditionalField(): FieldChange[] {
|
|
195
91
|
const fieldChanges: FieldChange[] = [];
|
|
196
92
|
|
|
197
93
|
// Add the userKey field to the main entity
|
|
198
94
|
fieldChanges.push(this.updateAdditionalUserKeyField());
|
|
199
|
-
if (this.field.relationCreateInverse) {
|
|
200
|
-
fieldChanges.push(this.updateAdditionalInverseField());
|
|
201
|
-
fieldChanges.push(this.updateAdditionalInverseIdsField());
|
|
202
|
-
fieldChanges.push(this.updateAdditionalInverseCommandsField());
|
|
203
|
-
}
|
|
204
95
|
return fieldChanges;
|
|
205
96
|
}
|
|
206
97
|
|
|
@@ -230,126 +121,26 @@ export class ManyToOneRelationFieldManagerForDto
|
|
|
230
121
|
}
|
|
231
122
|
}
|
|
232
123
|
|
|
233
|
-
private updateAdditionalInverseCommandsField(): FieldChange {
|
|
234
|
-
|
|
235
|
-
const commandFieldName = `${this.field.relationModelFieldName ?? this.modelName}Command`
|
|
236
|
-
const commandFieldType = ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword);
|
|
237
|
-
const source = this.relationInverseSource
|
|
238
|
-
const field = this.field
|
|
239
|
-
const decoratorManagers = [
|
|
240
|
-
new StringDecoratorManager({ isString: true, source: source, field: field }),
|
|
241
|
-
new OptionalDecoratorManager({ isApplyOptional: true, optional: true, source: source, field: field }),
|
|
242
|
-
// TODO pending @IsEnum(RelationFieldsCommand)
|
|
243
|
-
]
|
|
244
|
-
|
|
245
|
-
const commandField = this.getFieldIdentifierNode(
|
|
246
|
-
commandFieldName,
|
|
247
|
-
source
|
|
248
|
-
)?.parent as PropertyDeclaration;
|
|
249
|
-
//console.log(`\ncreate Dto updateAdditionalInverseCommandsField ${commandFieldName} called ...`);
|
|
250
|
-
|
|
251
|
-
if (commandField == null) {
|
|
252
|
-
return this.addAdditionalInverseCommandsField();
|
|
253
|
-
}
|
|
254
|
-
else {
|
|
255
|
-
//Update the command field
|
|
256
|
-
return this.updateFieldInternal(commandFieldName, commandFieldType, decoratorManagers, field, source)
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
private updateAdditionalInverseIdsField(): FieldChange {
|
|
261
|
-
const idsFieldName = `${this.field.relationModelFieldName ?? this.modelName}Ids`
|
|
262
|
-
const idsFieldType = ts.factory.createArrayTypeNode(ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword));
|
|
263
|
-
const source = this.relationInverseSource
|
|
264
|
-
const field = this.field
|
|
265
|
-
const decoratorManagers = [
|
|
266
|
-
new OptionalDecoratorManager({ isApplyOptional: true, optional: true, source: source, field: field }),
|
|
267
|
-
new ArrayDecoratorManager({ isArray: true, source: source, field: field })
|
|
268
|
-
]
|
|
269
|
-
|
|
270
|
-
const idsField = this.getFieldIdentifierNode(
|
|
271
|
-
idsFieldName,
|
|
272
|
-
source
|
|
273
|
-
)?.parent as PropertyDeclaration;
|
|
274
|
-
//console.log(`\ncreate Dto updateAdditionalInverseIdsField ${idsFieldName} called ...`);
|
|
275
|
-
|
|
276
|
-
if (idsField == null) {
|
|
277
|
-
return this.addAdditionalInverseIdsField();
|
|
278
|
-
}
|
|
279
|
-
else {
|
|
280
|
-
//Update the ids field
|
|
281
|
-
return this.updateFieldInternal(idsFieldName, idsFieldType, decoratorManagers, field, source)
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
private updateAdditionalInverseField() : FieldChange{
|
|
287
|
-
const fieldName = this.additionalFieldName();
|
|
288
|
-
const fieldType = this.additionalFieldType().node(this.field);
|
|
289
|
-
const source = this.relationInverseSource;
|
|
290
|
-
const field = this.field;
|
|
291
|
-
const decoratorManagers = this.relationInverseDecoratorManagers;
|
|
292
|
-
|
|
293
|
-
const inverseField = this.getFieldIdentifierNode(fieldName, source)?.parent as PropertyDeclaration;
|
|
294
|
-
if (inverseField == null) {
|
|
295
|
-
return this.addAdditionalInverseField();
|
|
296
|
-
}
|
|
297
|
-
else {
|
|
298
|
-
const fieldChange = this.updateFieldInternal(fieldName, fieldType, decoratorManagers, field, source);
|
|
299
|
-
if (this.modelName !== this.field.relationModelSingularName) {
|
|
300
|
-
const currentModelName = this.modelName;
|
|
301
|
-
const currentModuleName = this.moduleName;
|
|
302
|
-
fieldChange.changes.push(this.inverseFieldImport(currentModelName, currentModuleName, source));
|
|
303
|
-
}
|
|
304
|
-
return fieldChange;
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
private inverseFieldImport(modelName: string, currentModuleName: string, source: ts.SourceFile): Change {
|
|
309
|
-
const inverseEntityImportName = `update-${dasherize(modelName)}.dto`;
|
|
310
|
-
const modulePath = `src/${currentModuleName}`;
|
|
311
|
-
|
|
312
|
-
const inverseEntityPath = `${modulePath}/dtos/${inverseEntityImportName}`;
|
|
313
|
-
|
|
314
|
-
return safeInsertImport(source, `Update${classify(modelName)}Dto`, inverseEntityPath, currentModuleName);
|
|
315
|
-
}
|
|
316
124
|
|
|
317
125
|
override removeAdditionalField(): FieldChange[] {
|
|
318
126
|
const fieldChanges: FieldChange[] = [];
|
|
127
|
+
|
|
128
|
+
fieldChanges.push(this.removeAdditionalUserKeyField());
|
|
319
129
|
|
|
320
|
-
if (this.field.relationCreateInverse) {
|
|
321
|
-
fieldChanges.push(this.removeAdditionalInverseField());
|
|
322
|
-
fieldChanges.push(this.removeAdditionalInverseIdsField());
|
|
323
|
-
fieldChanges.push(this.removeAdditionalInverseCommandsField());
|
|
324
|
-
}
|
|
325
130
|
return fieldChanges;
|
|
326
131
|
}
|
|
327
132
|
|
|
328
|
-
|
|
329
|
-
const fieldName = this.
|
|
330
|
-
const source = this.
|
|
331
|
-
return this.removeFieldFor(fieldName, source);
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
removeAdditionalInverseIdsField(): FieldChange {
|
|
335
|
-
const fieldName = `${this.field.relationModelFieldName ?? this.modelName}Ids`;
|
|
336
|
-
const source = this.relationInverseSource;
|
|
133
|
+
private removeAdditionalUserKeyField(): FieldChange {
|
|
134
|
+
const fieldName = `${this.field.name}UserKey`;
|
|
135
|
+
const source = this.source;
|
|
337
136
|
return this.removeFieldFor(fieldName, source);
|
|
338
137
|
}
|
|
339
138
|
|
|
340
|
-
removeAdditionalInverseCommandsField(): FieldChange {
|
|
341
|
-
const fieldName = `${this.field.relationModelFieldName ?? this.modelName}Command`;
|
|
342
|
-
const source = this.relationInverseSource;
|
|
343
|
-
return this.removeFieldFor(fieldName, source);
|
|
344
|
-
}
|
|
345
139
|
|
|
346
140
|
override fieldName(): string {
|
|
347
141
|
return `${this.field.name}Id`;
|
|
348
142
|
}
|
|
349
143
|
|
|
350
|
-
additionalFieldName(): string {
|
|
351
|
-
return this.field.relationModelFieldName ?? `${this.modelName}s`;
|
|
352
|
-
}
|
|
353
144
|
|
|
354
145
|
protected isAdditionalFieldRequired(): boolean {
|
|
355
146
|
return true;
|
package/src/code-builder/lib/field/field-managers/relation/ManyToOneRelationFieldManagerForEntity.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { classify, dasherize } from '@angular-devkit/core/src/utils/strings';
|
|
2
|
-
import
|
|
3
|
-
import ts, { PropertyDeclaration } from '@schematics/angular/third_party/github.com/Microsoft/TypeScript/lib/typescript';
|
|
2
|
+
import ts from '@schematics/angular/third_party/github.com/Microsoft/TypeScript/lib/typescript';
|
|
4
3
|
import { Change } from '@schematics/angular/utility/change';
|
|
5
|
-
import {
|
|
6
|
-
import { DecoratorManager, FieldChange, FieldManager, FieldType, RelationType, createSourceFile, safeInsertImport } from '../../FieldManager';
|
|
4
|
+
import { FieldChange, FieldManager, FieldType, safeInsertImport } from '../../FieldManager';
|
|
7
5
|
import {
|
|
8
6
|
BaseFieldManagerForEntity,
|
|
9
7
|
} from '../base/BaseFieldManagerForEntity';
|
|
@@ -11,59 +9,11 @@ import {
|
|
|
11
9
|
export class ManyToOneRelationFieldManagerForEntity
|
|
12
10
|
extends BaseFieldManagerForEntity
|
|
13
11
|
implements FieldManager {
|
|
14
|
-
source: ts.SourceFile;
|
|
15
|
-
relationInverseSource: ts.SourceFile;
|
|
16
|
-
|
|
17
|
-
oneToManyDecoratorManager: OneToManyDecoratorManager;
|
|
18
|
-
inverseManyToManyDecoratorManager: DecoratorManager;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
constructor(tree: Tree, moduleName: string, modelName: string, field: any) {
|
|
22
|
-
super(tree, moduleName, modelName, field);
|
|
23
|
-
if (this.isAdditionalFieldRequired()) {
|
|
24
|
-
const relatedEntityFileName = `${dasherize(this.field.relationModelSingularName)}.entity.ts`;
|
|
25
|
-
const relatedEntityPath = this.field.relationModelModuleName ? `src/${dasherize(this.field.relationModelModuleName)}/entities/${relatedEntityFileName}` : `src/${dasherize(moduleName)}/entities/${relatedEntityFileName}`;
|
|
26
|
-
this.relationInverseSource = createSourceFile( //TODO "src/iam/dtos/create-user.dto.ts" does not exist. If an entity is used in a many-to-one relation, the create-entity.dto.ts file should be exist.
|
|
27
|
-
tree,
|
|
28
|
-
relatedEntityPath,
|
|
29
|
-
);
|
|
30
|
-
|
|
31
|
-
this.oneToManyDecoratorManager = new OneToManyDecoratorManager(
|
|
32
|
-
{
|
|
33
|
-
isOneToMany: this.isOneToMany(),
|
|
34
|
-
relationCascade: this.field.relationCascade,
|
|
35
|
-
source: this.relationInverseSource,
|
|
36
|
-
field: this.field,
|
|
37
|
-
fieldName: this.fieldName(),
|
|
38
|
-
modelName: this.modelName,
|
|
39
|
-
},
|
|
40
|
-
);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
fieldName(): string {
|
|
45
|
-
switch(this.field.relationType) {
|
|
46
|
-
case RelationType.ManyToOne:
|
|
47
|
-
return super.fieldName();
|
|
48
|
-
case RelationType.ManyToMany:
|
|
49
|
-
return `${this.field.name}s`;
|
|
50
|
-
default:
|
|
51
|
-
throw new Error(`Unsupported relation type: ${this.field.relationType}`);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
12
|
|
|
55
13
|
fieldType(): FieldType {
|
|
56
|
-
|
|
57
|
-
case RelationType.ManyToOne:
|
|
58
|
-
return this.manyToOneFieldType();
|
|
59
|
-
case RelationType.ManyToMany:
|
|
60
|
-
return this.manyToManyFieldType();
|
|
61
|
-
default:
|
|
62
|
-
throw new Error(`Unsupported relation type: ${this.field.relationType}`);
|
|
63
|
-
}
|
|
14
|
+
return this.manyToOneFieldType();
|
|
64
15
|
}
|
|
65
16
|
|
|
66
|
-
|
|
67
17
|
private manyToOneFieldType(): FieldType {
|
|
68
18
|
return {
|
|
69
19
|
text: classify(this.field.relationModelSingularName),
|
|
@@ -76,20 +26,6 @@ export class ManyToOneRelationFieldManagerForEntity
|
|
|
76
26
|
};
|
|
77
27
|
}
|
|
78
28
|
|
|
79
|
-
private manyToManyFieldType(): FieldType {
|
|
80
|
-
const type = `${classify(this.field.relationModelSingularName)}`
|
|
81
|
-
const text = `${type}[]`
|
|
82
|
-
return {
|
|
83
|
-
text: text,
|
|
84
|
-
node: (_field: any) => ts.factory.createArrayTypeNode(
|
|
85
|
-
ts.factory.createTypeReferenceNode(
|
|
86
|
-
ts.factory.createIdentifier(type),
|
|
87
|
-
undefined
|
|
88
|
-
)
|
|
89
|
-
),
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
|
|
93
29
|
override addField(): FieldChange[] {
|
|
94
30
|
const fieldChanges = super.addField();
|
|
95
31
|
if (fieldChanges.length > 0 && this.modelName !== this.field.relationModelSingularName) {
|
|
@@ -114,112 +50,5 @@ export class ManyToOneRelationFieldManagerForEntity
|
|
|
114
50
|
return safeInsertImport(this.source, classify(this.field.relationModelSingularName), relatedEntityPath, this.moduleName);
|
|
115
51
|
}
|
|
116
52
|
|
|
117
|
-
private isOneToMany(): boolean {
|
|
118
|
-
return (this.field.type === 'relation' && this.field.relationType === RelationType.ManyToOne && this.field.relationCreateInverse);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
override removeAdditionalField(): FieldChange {
|
|
122
|
-
const changes: Change[] = [];
|
|
123
|
-
if (this.isFieldPresent(this.additionalFieldName(), this.relationInverseSource)) {
|
|
124
|
-
console.log(`\nEntity removeField ${this.additionalFieldName()} called ...`);
|
|
125
|
-
changes.push(...this.removeFieldFor(this.additionalFieldName(), this.relationInverseSource).changes);
|
|
126
|
-
}
|
|
127
|
-
return {
|
|
128
|
-
filePath: this.relationInverseSource.fileName,
|
|
129
|
-
field: this.field,
|
|
130
|
-
changes: changes,
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
override isAdditionalFieldRequired(): boolean {
|
|
135
|
-
return (this.field.type === 'relation' && this.field.relationCreateInverse);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
override addAdditionalField(): FieldChange[] {
|
|
139
|
-
const fieldChanges: FieldChange[] = [];
|
|
140
|
-
|
|
141
|
-
if (this.field.relationCreateInverse) {
|
|
142
|
-
fieldChanges.push(this.addAdditionalInverseField());
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
return fieldChanges;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
private addAdditionalInverseField(): FieldChange {
|
|
149
|
-
const fieldName = this.additionalFieldName();
|
|
150
|
-
const fieldType = this.additionalFieldType().text;
|
|
151
|
-
const source= this.relationInverseSource
|
|
152
|
-
const field = this.field
|
|
153
|
-
const modelName = this.field.relationModelSingularName
|
|
154
|
-
const decoratorManagers = this.additionalDecoratorManagers();
|
|
155
|
-
|
|
156
|
-
const fieldChange = this.addFieldInternal(fieldName, fieldType, decoratorManagers, field, modelName, source);
|
|
157
|
-
if (this.modelName !== this.field.relationModelSingularName) {
|
|
158
|
-
const currentModelName = this.modelName;
|
|
159
|
-
const currentModuleName = this.moduleName;
|
|
160
|
-
fieldChange.changes.push(this.inverseFieldImport(currentModelName, currentModuleName, source));
|
|
161
|
-
}
|
|
162
|
-
return fieldChange;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
private inverseFieldImport(modelName: string, currentModuleName: string, source: ts.SourceFile): Change {
|
|
166
|
-
const inverseEntityImportName = `${dasherize(modelName)}.entity`;
|
|
167
|
-
const modulePath = `src/${currentModuleName}`;
|
|
168
|
-
|
|
169
|
-
const inverseEntityPath = `${modulePath}/entities/${inverseEntityImportName}`;
|
|
170
|
-
return safeInsertImport(source, classify(modelName), inverseEntityPath, currentModuleName);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
override addOrUpdateAdditionalField(): FieldChange[] {
|
|
174
|
-
const fieldChanges: FieldChange[] = [];
|
|
175
|
-
|
|
176
|
-
if (this.field.relationCreateInverse) {
|
|
177
|
-
fieldChanges.push(this.updateAdditionalInverseField());
|
|
178
|
-
}
|
|
179
|
-
return fieldChanges;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
private updateAdditionalInverseField() : FieldChange{
|
|
183
|
-
const fieldName = this.additionalFieldName();
|
|
184
|
-
const fieldType = this.additionalFieldType().node(this.field);
|
|
185
|
-
const source = this.relationInverseSource;
|
|
186
|
-
const field = this.field;
|
|
187
|
-
const decoratorManagers = this.additionalDecoratorManagers();
|
|
188
|
-
|
|
189
|
-
const inverseField = this.getFieldIdentifierNode(fieldName, source)?.parent as PropertyDeclaration;
|
|
190
|
-
if (inverseField == null) {
|
|
191
|
-
return this.addAdditionalInverseField();
|
|
192
|
-
}
|
|
193
|
-
else {
|
|
194
|
-
const fieldChange = this.updateFieldInternal(fieldName, fieldType, decoratorManagers, field, source);
|
|
195
|
-
if (this.modelName !== this.field.relationModelSingularName) {
|
|
196
|
-
const currentModelName = this.modelName;
|
|
197
|
-
const currentModuleName = this.moduleName;
|
|
198
|
-
fieldChange.changes.push(this.inverseFieldImport(currentModelName, currentModuleName, source));
|
|
199
|
-
}
|
|
200
|
-
return fieldChange;
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
additionalFieldName(): string { // The inverse field type will remain the same for both one-to-many and many-to-many relations
|
|
205
|
-
return this.field.relationModelFieldName ?? `${this.modelName}s`;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
additionalFieldType(): FieldType { // The inverse field type will remain the same for both one-to-many and many-to-many relations
|
|
209
|
-
const type = `${classify(this.modelName)}`
|
|
210
|
-
const text = `${type}[]`
|
|
211
|
-
return {
|
|
212
|
-
text: text,
|
|
213
|
-
node: (_field: any) =>
|
|
214
|
-
ts.factory.createArrayTypeNode(ts.factory.createTypeReferenceNode(
|
|
215
|
-
ts.factory.createIdentifier(type),
|
|
216
|
-
undefined
|
|
217
|
-
)),
|
|
218
|
-
};
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
private additionalDecoratorManagers(): DecoratorManager[] {
|
|
222
|
-
return [this.oneToManyDecoratorManager];
|
|
223
|
-
}
|
|
224
53
|
|
|
225
54
|
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { DtoSourceType, FieldChange, FieldHandler, FieldManager, ManagerForDtoOptions } from '../../FieldManager';
|
|
2
|
+
import { OneToManyRelationFieldManagerForDto } from './OneToManyRelationFieldManagerForDto';
|
|
3
|
+
import { OneToManyRelationFieldManagerForEntity } from './OneToManyRelationFieldManagerForEntity';
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
export class OneToManyRelationFieldHandler implements FieldHandler {
|
|
7
|
+
entityFieldManager: FieldManager;
|
|
8
|
+
createDtoFieldManager: FieldManager;
|
|
9
|
+
updateDtoFieldManager: FieldManager;
|
|
10
|
+
constructor(tree: any, moduleName: string, modelName: string, field: any) {
|
|
11
|
+
this.entityFieldManager = new OneToManyRelationFieldManagerForEntity(
|
|
12
|
+
tree,
|
|
13
|
+
moduleName,
|
|
14
|
+
modelName,
|
|
15
|
+
field,
|
|
16
|
+
);
|
|
17
|
+
this.createDtoFieldManager = new OneToManyRelationFieldManagerForDto(
|
|
18
|
+
tree,
|
|
19
|
+
moduleName,
|
|
20
|
+
modelName,
|
|
21
|
+
field,
|
|
22
|
+
new ManagerForDtoOptions(DtoSourceType.Create),
|
|
23
|
+
);
|
|
24
|
+
this.updateDtoFieldManager = new OneToManyRelationFieldManagerForDto(
|
|
25
|
+
tree,
|
|
26
|
+
moduleName,
|
|
27
|
+
modelName,
|
|
28
|
+
field,
|
|
29
|
+
new ManagerForDtoOptions(DtoSourceType.Update),
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
updateEntityField(): FieldChange[] {
|
|
33
|
+
return this.entityFieldManager.updateField();
|
|
34
|
+
}
|
|
35
|
+
updateDtoField(): FieldChange[] {
|
|
36
|
+
return [...this.createDtoFieldManager.updateField(), ...this.updateDtoFieldManager.updateField()];
|
|
37
|
+
}
|
|
38
|
+
removeEntityField(): FieldChange[] {
|
|
39
|
+
return this.entityFieldManager.removeField();
|
|
40
|
+
}
|
|
41
|
+
removeDtoField(): FieldChange[] {
|
|
42
|
+
return [...this.createDtoFieldManager.removeField(), ...this.updateDtoFieldManager.removeField()];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
addEntityField(): FieldChange[] {
|
|
46
|
+
return this.entityFieldManager.addField();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
addDtoField(): FieldChange[] {
|
|
50
|
+
return [...this.createDtoFieldManager.addField(), ...this.updateDtoFieldManager.addField()];
|
|
51
|
+
}
|
|
52
|
+
}
|