@solidstarters/solid-code-builder 1.0.11 → 1.0.13

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.
Files changed (58) hide show
  1. package/dist/code-builder/lib/field/FieldManager.d.ts +2 -1
  2. package/dist/code-builder/lib/field/FieldManager.js +18 -5
  3. package/dist/code-builder/lib/field/FieldManager.js.map +1 -1
  4. package/dist/code-builder/lib/field/decorator-managers/entity/IndexDecoratorManager.js +1 -1
  5. package/dist/code-builder/lib/field/decorator-managers/entity/IndexDecoratorManager.js.map +1 -1
  6. package/dist/code-builder/lib/field/field-managers/base/BaseFieldManagerForDto.d.ts +6 -3
  7. package/dist/code-builder/lib/field/field-managers/base/BaseFieldManagerForDto.js +50 -15
  8. package/dist/code-builder/lib/field/field-managers/base/BaseFieldManagerForDto.js.map +1 -1
  9. package/dist/code-builder/lib/field/field-managers/base/BaseFieldManagerForEntity.d.ts +3 -0
  10. package/dist/code-builder/lib/field/field-managers/base/BaseFieldManagerForEntity.js +15 -3
  11. package/dist/code-builder/lib/field/field-managers/base/BaseFieldManagerForEntity.js.map +1 -1
  12. package/dist/code-builder/lib/field/field-managers/relation/ManyToManyInverseRelationFieldHandler.d.ts +13 -0
  13. package/dist/code-builder/lib/field/field-managers/relation/ManyToManyInverseRelationFieldHandler.js +33 -0
  14. package/dist/code-builder/lib/field/field-managers/relation/ManyToManyInverseRelationFieldHandler.js.map +1 -0
  15. package/dist/code-builder/lib/field/field-managers/relation/ManyToManyInverseRelationFieldManagerForDto.d.ts +4 -0
  16. package/dist/code-builder/lib/field/field-managers/relation/ManyToManyInverseRelationFieldManagerForDto.js +9 -0
  17. package/dist/code-builder/lib/field/field-managers/relation/ManyToManyInverseRelationFieldManagerForDto.js.map +1 -0
  18. package/dist/code-builder/lib/field/field-managers/relation/ManyToManyInverseRelationFieldManagerForEntity.d.ts +4 -0
  19. package/dist/code-builder/lib/field/field-managers/relation/ManyToManyInverseRelationFieldManagerForEntity.js +8 -0
  20. package/dist/code-builder/lib/field/field-managers/relation/ManyToManyInverseRelationFieldManagerForEntity.js.map +1 -0
  21. package/dist/code-builder/lib/field/field-managers/relation/ManyToManyRelationFieldManagerForDto.d.ts +1 -16
  22. package/dist/code-builder/lib/field/field-managers/relation/ManyToManyRelationFieldManagerForDto.js +15 -156
  23. package/dist/code-builder/lib/field/field-managers/relation/ManyToManyRelationFieldManagerForDto.js.map +1 -1
  24. package/dist/code-builder/lib/field/field-managers/relation/ManyToManyRelationFieldManagerForEntity.d.ts +1 -20
  25. package/dist/code-builder/lib/field/field-managers/relation/ManyToManyRelationFieldManagerForEntity.js +0 -109
  26. package/dist/code-builder/lib/field/field-managers/relation/ManyToManyRelationFieldManagerForEntity.js.map +1 -1
  27. package/dist/code-builder/lib/field/field-managers/relation/ManyToOneRelationFieldManagerForDto.d.ts +2 -17
  28. package/dist/code-builder/lib/field/field-managers/relation/ManyToOneRelationFieldManagerForDto.js +4 -175
  29. package/dist/code-builder/lib/field/field-managers/relation/ManyToOneRelationFieldManagerForDto.js.map +1 -1
  30. package/dist/code-builder/lib/field/field-managers/relation/ManyToOneRelationFieldManagerForEntity.d.ts +1 -22
  31. package/dist/code-builder/lib/field/field-managers/relation/ManyToOneRelationFieldManagerForEntity.js +1 -133
  32. package/dist/code-builder/lib/field/field-managers/relation/ManyToOneRelationFieldManagerForEntity.js.map +1 -1
  33. package/dist/code-builder/lib/field/field-managers/relation/OneToManyRelationFieldHandler.d.ts +13 -0
  34. package/dist/code-builder/lib/field/field-managers/relation/OneToManyRelationFieldHandler.js +33 -0
  35. package/dist/code-builder/lib/field/field-managers/relation/OneToManyRelationFieldHandler.js.map +1 -0
  36. package/dist/code-builder/lib/field/field-managers/relation/OneToManyRelationFieldManagerForDto.d.ts +34 -0
  37. package/dist/code-builder/lib/field/field-managers/relation/OneToManyRelationFieldManagerForDto.js +202 -0
  38. package/dist/code-builder/lib/field/field-managers/relation/OneToManyRelationFieldManagerForDto.js.map +1 -0
  39. package/dist/code-builder/lib/field/field-managers/relation/OneToManyRelationFieldManagerForEntity.d.ts +9 -0
  40. package/dist/code-builder/lib/field/field-managers/relation/OneToManyRelationFieldManagerForEntity.js +43 -0
  41. package/dist/code-builder/lib/field/field-managers/relation/OneToManyRelationFieldManagerForEntity.js.map +1 -0
  42. package/dist/files/generate-model/controllers/__model@dasherize__.controller.ts +6 -2
  43. package/files/generate-model/controllers/__model@dasherize__.controller.ts +6 -2
  44. package/package.json +1 -1
  45. package/src/code-builder/lib/field/FieldManager.ts +19 -6
  46. package/src/code-builder/lib/field/decorator-managers/entity/IndexDecoratorManager.ts +1 -1
  47. package/src/code-builder/lib/field/field-managers/base/BaseFieldManagerForDto.ts +74 -26
  48. package/src/code-builder/lib/field/field-managers/base/BaseFieldManagerForEntity.ts +19 -3
  49. package/src/code-builder/lib/field/field-managers/relation/ManyToManyInverseRelationFieldHandler.ts +52 -0
  50. package/src/code-builder/lib/field/field-managers/relation/ManyToManyInverseRelationFieldManagerForDto.ts +8 -0
  51. package/src/code-builder/lib/field/field-managers/relation/ManyToManyInverseRelationFieldManagerForEntity.ts +8 -0
  52. package/src/code-builder/lib/field/field-managers/relation/ManyToManyRelationFieldManagerForDto.ts +16 -189
  53. package/src/code-builder/lib/field/field-managers/relation/ManyToManyRelationFieldManagerForEntity.ts +2 -147
  54. package/src/code-builder/lib/field/field-managers/relation/ManyToOneRelationFieldManagerForDto.ts +6 -215
  55. package/src/code-builder/lib/field/field-managers/relation/ManyToOneRelationFieldManagerForEntity.ts +3 -174
  56. package/src/code-builder/lib/field/field-managers/relation/OneToManyRelationFieldHandler.ts +52 -0
  57. package/src/code-builder/lib/field/field-managers/relation/OneToManyRelationFieldManagerForDto.ts +247 -0
  58. package/src/code-builder/lib/field/field-managers/relation/OneToManyRelationFieldManagerForEntity.ts +51 -0
@@ -0,0 +1,52 @@
1
+ import { DtoSourceType, FieldChange, FieldHandler, FieldManager, ManagerForDtoOptions } from '../../FieldManager';
2
+ import { ManyToManyInverseRelationFieldManagerForDto } from './ManyToManyInverseRelationFieldManagerForDto';
3
+ import { ManyToManyInverseRelationFieldManagerForEntity } from './ManyToManyInverseRelationFieldManagerForEntity';
4
+
5
+
6
+ export class ManyToManyInverseRelationFieldHandler 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 ManyToManyInverseRelationFieldManagerForEntity(
12
+ tree,
13
+ moduleName,
14
+ modelName,
15
+ field,
16
+ );
17
+ this.createDtoFieldManager = new ManyToManyInverseRelationFieldManagerForDto(
18
+ tree,
19
+ moduleName,
20
+ modelName,
21
+ field,
22
+ new ManagerForDtoOptions(DtoSourceType.Create),
23
+ );
24
+ this.updateDtoFieldManager = new ManyToManyInverseRelationFieldManagerForDto(
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
+ }
@@ -0,0 +1,8 @@
1
+ import { FieldManager } from '../../FieldManager';
2
+ import { ManyToManyRelationFieldManagerForDto } from './ManyToManyRelationFieldManagerForDto';
3
+
4
+ // For DTO's, the inverse relation behaivour is the same as the normal relation
5
+ export class ManyToManyInverseRelationFieldManagerForDto
6
+ extends ManyToManyRelationFieldManagerForDto
7
+ implements FieldManager {
8
+ }
@@ -0,0 +1,8 @@
1
+ import { FieldManager } from '../../FieldManager';
2
+ import { ManyToManyRelationFieldManagerForEntity } from './ManyToManyRelationFieldManagerForEntity';
3
+
4
+ export class ManyToManyInverseRelationFieldManagerForEntity
5
+ extends ManyToManyRelationFieldManagerForEntity
6
+ implements FieldManager {
7
+
8
+ }
@@ -6,7 +6,7 @@ import { ArrayDecoratorManager } from '../../decorator-managers/dto/ArrayDecorat
6
6
  import { OptionalDecoratorManager } from '../../decorator-managers/dto/OptionalDecoratorManager';
7
7
  import { StringDecoratorManager } from '../../decorator-managers/dto/StringDecoratorManager';
8
8
  import { TransformDecoratorManager } from '../../decorator-managers/dto/TransformDecoratorManager';
9
- import { DecoratorManager, DecoratorType, DtoSourceType, FieldChange, FieldManager, FieldType, ManagerForDtoOptions, createSourceFile, safeInsertImport } from '../../FieldManager';
9
+ import { DecoratorType, FieldChange, FieldManager, FieldType, ManagerForDtoOptions, safeInsertImport } from '../../FieldManager';
10
10
  import { BaseFieldManagerForDto } from '../base/BaseFieldManagerForDto';
11
11
 
12
12
  export class ManyToManyRelationFieldManagerForDto
@@ -18,8 +18,6 @@ export class ManyToManyRelationFieldManagerForDto
18
18
  isBoolean(): boolean {
19
19
  return false;
20
20
  }
21
- relationInverseSource: ts.SourceFile;
22
- relationInverseDecoratorManagers: DecoratorManager[];
23
21
 
24
22
  constructor(tree: Tree, moduleName: string, modelName: string, field: any, options: ManagerForDtoOptions) {
25
23
  super(tree, moduleName, modelName, {...field, required: false}, options);
@@ -28,32 +26,11 @@ export class ManyToManyRelationFieldManagerForDto
28
26
  this.source,
29
27
  DecoratorType.Array,
30
28
  DecoratorType.ValidateNested,
31
- // DecoratorType.Transform,
32
29
  )];
33
30
  this.decoratorManagers.push(
34
31
  new TransformDecoratorManager({ isTransform: true, type: this.transformType(this.field.relationModelSingularName), source: this.source, field: field })
35
32
  );
36
33
 
37
- // TODO : inverse source file logic is required, but needs to be handled in a better way
38
- if (this.field.relationCreateInverse) {
39
- const relatedEntityFileName = (this.options.sourceType === DtoSourceType.Create) ? `create-${dasherize(this.field.relationModelSingularName)}.dto.ts` : `update-${dasherize(this.field.relationModelSingularName)}.dto.ts`;
40
- const relatedEntityPath = this.field.relationModelModuleName ? `src/${dasherize(this.field.relationModelModuleName)}/dtos/${relatedEntityFileName}` : `src/${dasherize(moduleName)}/dtos/${relatedEntityFileName}`;
41
- this.relationInverseSource = createSourceFile(tree, relatedEntityPath);
42
- this.relationInverseDecoratorManagers = this.getFieldDecoratorManagers(
43
- this.field,
44
- this.relationInverseSource,
45
- DecoratorType.Array,
46
- DecoratorType.ValidateNested,
47
- // DecoratorType.Transform,
48
- );
49
- this.relationInverseDecoratorManagers.push(
50
- new TransformDecoratorManager({ isTransform: true, type: this.transformType(this.modelName), source: this.relationInverseSource, field: field })
51
- );
52
- this.relationInverseDecoratorManagers.push(
53
- new OptionalDecoratorManager({ isApplyOptional: true, optional: true, source: this.relationInverseSource, field: field })
54
- );
55
-
56
- }
57
34
  }
58
35
 
59
36
  isString(): boolean {
@@ -92,11 +69,6 @@ export class ManyToManyRelationFieldManagerForDto
92
69
  return this.manyToManyFieldType(this.field.relationModelSingularName)
93
70
  }
94
71
 
95
- // This field type is used in the case of one-to-many relation, when we generate the inverse field in the related entity
96
- additionalFieldType(): FieldType {
97
- return this.manyToManyFieldType(this.modelName)
98
- }
99
-
100
72
  private transformType(forModelName: string): string {
101
73
  return `Update${classify(forModelName)}Dto`;
102
74
  }
@@ -123,44 +95,9 @@ export class ManyToManyRelationFieldManagerForDto
123
95
  // Add the command field to the main entity
124
96
  fieldChanges.push(this.addAdditionalCommandField());
125
97
 
126
- // Add the inverse field to the related entity
127
- if (this.field.relationCreateInverse) {
128
- fieldChanges.push(this.addAdditionalInverseField());
129
- fieldChanges.push(this.addAdditionalInverseIdsField());
130
- fieldChanges.push(this.addAdditionalInverseCommandsField());
131
- }
132
98
  return fieldChanges;
133
99
  }
134
100
 
135
- private addAdditionalInverseIdsField(): FieldChange {
136
- const fieldName = `${this.field.relationModelFieldName}Ids`;
137
- const fieldType = "number[]";
138
- const source= this.relationInverseSource
139
- const field = this.field
140
- const modelName = this.field.relationModelSingularName
141
- const decoratorManagers = [
142
- new OptionalDecoratorManager({ isApplyOptional: true, optional: true, source: source, field: field }),
143
- new ArrayDecoratorManager({ isArray: true, source: source, field: field })
144
- ]
145
-
146
- return this.addFieldInternal(fieldName, fieldType, decoratorManagers, field, modelName, source);
147
- }
148
-
149
- private addAdditionalInverseCommandsField(): FieldChange {
150
- const fieldName = `${this.field.relationModelFieldName}Command`;
151
- const fieldType = "string";
152
- const source= this.relationInverseSource
153
- const field = this.field
154
- const modelName = this.field.relationModelSingularName
155
- const decoratorManagers = [
156
- new StringDecoratorManager({ isString: true, source: source, field: field }),
157
- new OptionalDecoratorManager({ isApplyOptional: true, optional: true, source: source, field: field }),
158
- // TODO pending @IsEnum(RelationFieldsCommand)
159
- ]
160
-
161
- return this.addFieldInternal(fieldName, fieldType, decoratorManagers, field, modelName, source);
162
- }
163
-
164
101
  private addAdditionalIdsField() : FieldChange{
165
102
  const fieldName = `${this.field.name}Ids`;
166
103
  const fieldType = "number[]";
@@ -191,28 +128,22 @@ export class ManyToManyRelationFieldManagerForDto
191
128
 
192
129
  }
193
130
 
194
- private inverseFieldImport(modelName: string, currentModuleName: string, source: ts.SourceFile): Change {
195
- const inverseEntityImportName = `update-${dasherize(modelName)}.dto`;
196
- const modulePath = `src/${currentModuleName}`;
197
- const inverseEntityPath = `${modulePath}/dtos/${inverseEntityImportName}`;
198
- return safeInsertImport(source, `Update${classify(modelName)}Dto`, inverseEntityPath, currentModuleName);
199
- }
200
131
 
201
- private addAdditionalInverseField() : FieldChange {
202
- const fieldName = this.additionalFieldName();
203
- const fieldType = this.additionalFieldType().text;
204
- const source= this.relationInverseSource
205
- const field = this.field
206
- const modelName = this.field.relationModelSingularName
207
- const decoratorManagers = this.relationInverseDecoratorManagers
208
- const fieldChange = this.addFieldInternal(fieldName, fieldType, decoratorManagers, field, modelName, source);
209
- if (this.modelName !== this.field.relationModelSingularName) {
210
- const currentModelName = this.modelName;
211
- const currentModuleName = this.moduleName;
212
- fieldChange.changes.push(this.inverseFieldImport(currentModelName, currentModuleName, source));
213
- }
214
- return fieldChange;
215
- }
132
+ // private addAdditionalInverseField() : FieldChange {
133
+ // const fieldName = this.additionalFieldName();
134
+ // const fieldType = this.additionalFieldType().text;
135
+ // const source= this.relationInverseSource
136
+ // const field = this.field
137
+ // const modelName = this.field.relationModelSingularName
138
+ // const decoratorManagers = this.relationInverseDecoratorManagers
139
+ // const fieldChange = this.addFieldInternal(fieldName, fieldType, decoratorManagers, field, modelName, source);
140
+ // if (this.modelName !== this.field.relationModelSingularName) {
141
+ // const currentModelName = this.modelName;
142
+ // const currentModuleName = this.moduleName;
143
+ // fieldChange.changes.push(this.inverseFieldImport(currentModelName, currentModuleName, source));
144
+ // }
145
+ // return fieldChange;
146
+ // }
216
147
 
217
148
  //TODO: Can be improved to avoid duplicate code
218
149
  override addOrUpdateAdditionalField(): FieldChange[] {
@@ -223,87 +154,9 @@ export class ManyToManyRelationFieldManagerForDto
223
154
  // Add or update the command field
224
155
  fieldChanges.push(this.updateAdditionalCommandField());
225
156
 
226
- if (this.field.relationCreateInverse) {
227
- fieldChanges.push(this.updateAdditionalInverseField());
228
- fieldChanges.push(this.updateAdditionalInverseIdsField());
229
- fieldChanges.push(this.updateAdditionalInverseCommandsField());
230
- }
231
157
  return fieldChanges;
232
158
  }
233
159
 
234
- updateAdditionalInverseField(): FieldChange {
235
- const inverseFieldName = this.field.relationModelFieldName;
236
- const inverseFieldType = this.manyToManyFieldType(this.modelName).node(this.field);
237
- const source= this.relationInverseSource
238
- const field = this.field
239
- const inverseDecoratorManagers = this.relationInverseDecoratorManagers
240
-
241
- const inverseField = this.getFieldIdentifierNode(
242
- inverseFieldName,
243
- source
244
- )?.parent as PropertyDeclaration;
245
- if (inverseField == null) {
246
- return this.addAdditionalInverseField();
247
- }
248
- else {
249
- //Update the inverse field
250
- const fieldChange = this.updateFieldInternal(inverseFieldName, inverseFieldType, inverseDecoratorManagers, field, source)
251
- if (this.modelName !== this.field.relationModelSingularName) {
252
- const currentModelName = this.modelName;
253
- const currentModuleName = this.moduleName;
254
- fieldChange.changes.push(this.inverseFieldImport(currentModelName, currentModuleName, source));
255
- }
256
- return fieldChange;
257
- }
258
- }
259
-
260
- updateAdditionalInverseIdsField(): FieldChange {
261
- const idsFieldName = `${this.field.relationModelFieldName}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
- if (idsField == null) {
275
- return this.addAdditionalInverseIdsField();
276
- }
277
- else {
278
- //Update the ids field
279
- return this.updateFieldInternal(idsFieldName, idsFieldType, decoratorManagers, field, source)
280
- }
281
- }
282
-
283
- updateAdditionalInverseCommandsField(): FieldChange {
284
- const commandFieldName = `${this.field.relationModelFieldName}Command`
285
- const commandFieldType = ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword);
286
- const source = this.relationInverseSource
287
- const field = this.field
288
- const decoratorManagers = [
289
- new StringDecoratorManager({ isString: true, source: source, field: field }),
290
- new OptionalDecoratorManager({ isApplyOptional: true, optional: true, source: source, field: field }),
291
- // TODO pending @IsEnum(RelationFieldsCommand)
292
- ]
293
-
294
- const commandField = this.getFieldIdentifierNode(
295
- commandFieldName,
296
- source
297
- )?.parent as PropertyDeclaration;
298
- if (commandField == null) {
299
- return this.addAdditionalInverseCommandsField();
300
- }
301
- else {
302
- //Update the command field
303
- return this.updateFieldInternal(commandFieldName, commandFieldType, decoratorManagers, field, source)
304
- }
305
- }
306
-
307
160
  private updateAdditionalIdsField(): FieldChange {
308
161
  const idsFieldName = `${this.field.name}Ids`
309
162
  const source = this.source
@@ -364,12 +217,6 @@ export class ManyToManyRelationFieldManagerForDto
364
217
  // Add the command field to the main entity
365
218
  fieldChanges.push(this.removeAdditionalCommandField());
366
219
 
367
- // Add the inverse field to the related entity
368
- if (this.field.relationCreateInverse) {
369
- fieldChanges.push(this.removeAdditionalInverseField());
370
- fieldChanges.push(this.removeAdditionalInverseIdsField());
371
- fieldChanges.push(this.removeAdditionalInverseCommandsField());
372
- }
373
220
  return fieldChanges;
374
221
  }
375
222
 
@@ -383,34 +230,14 @@ export class ManyToManyRelationFieldManagerForDto
383
230
  const source = this.source;
384
231
  return this.removeFieldFor(fieldName, source);
385
232
  }
386
- removeAdditionalInverseField(): FieldChange {
387
- const fieldName = this.additionalFieldName();
388
- const source = this.relationInverseSource;
389
- return this.removeFieldFor(fieldName, source);
390
- }
391
- removeAdditionalInverseIdsField(): FieldChange {
392
- const fieldName = `${this.field.relationModelFieldName}Ids`;
393
- const source = this.relationInverseSource;
394
- return this.removeFieldFor(fieldName, source);
395
- }
396
- removeAdditionalInverseCommandsField(): FieldChange {
397
- const fieldName = `${this.field.relationModelFieldName}Command`;
398
- const source = this.relationInverseSource;
399
- return this.removeFieldFor(fieldName, source);
400
- }
401
233
 
402
234
  //TODO: Need to revise the algorithm to generate multiple fields as part of single solid field type
403
235
  override fieldName(): string {
404
236
  return `${this.field.name}`;
405
237
  }
406
238
 
407
- additionalFieldName(): string {
408
- return this.field.relationModelFieldName;
409
- }
410
-
411
239
  protected isAdditionalFieldRequired(): boolean {
412
240
  return true
413
- // return this.field.relationCreateInverse;
414
241
  }
415
242
 
416
243
  override addField(): FieldChange[] {
@@ -1,10 +1,7 @@
1
1
  import { classify, dasherize } from '@angular-devkit/core/src/utils/strings';
2
- import { Tree } from '@angular-devkit/schematics';
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 { ManyToManyDecoratorManager } from '../../decorator-managers/entity/ManyToManyDecoratorManager';
6
- import { OneToManyDecoratorManager } from '../../decorator-managers/entity/OneToManyDecoratorManager';
7
- import { DecoratorManager, FieldChange, FieldManager, FieldType, createSourceFile, safeInsertImport } from '../../FieldManager';
4
+ import { FieldChange, FieldManager, FieldType, safeInsertImport } from '../../FieldManager';
8
5
  import {
9
6
  BaseFieldManagerForEntity,
10
7
  } from '../base/BaseFieldManagerForEntity';
@@ -12,39 +9,6 @@ import {
12
9
  export class ManyToManyRelationFieldManagerForEntity
13
10
  extends BaseFieldManagerForEntity
14
11
  implements FieldManager {
15
- source: ts.SourceFile;
16
- relationInverseSource: ts.SourceFile;
17
-
18
- oneToManyDecoratorManager: OneToManyDecoratorManager;
19
- inverseManyToManyDecoratorManager: DecoratorManager;
20
-
21
-
22
- constructor(tree: Tree, moduleName: string, modelName: string, field: any) {
23
- super(tree, moduleName, modelName, field);
24
- if (this.isAdditionalFieldRequired()) {
25
- const relatedEntityFileName = `${dasherize(this.field.relationModelSingularName)}.entity.ts`;
26
- const relatedEntityPath = this.field.relationModelModuleName ? `src/${dasherize(this.field.relationModelModuleName)}/entities/${relatedEntityFileName}` : `src/${dasherize(moduleName)}/entities/${relatedEntityFileName}`;
27
- 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.
28
- tree,
29
- relatedEntityPath,
30
- );
31
-
32
- this.inverseManyToManyDecoratorManager = new ManyToManyDecoratorManager(
33
- {
34
- isManyToMany: this.isInverseManyToMany(),
35
- relationModelName: this.modelName,
36
- relationInverseFieldName: this.fieldName(),
37
- relationCascade: this.field.relationCascade,
38
- owner: false,
39
- source: this.relationInverseSource,
40
- field: this.field,
41
- fieldName: this.fieldName(),
42
- modelName: this.modelName,
43
- },
44
- );
45
-
46
- }
47
- }
48
12
 
49
13
  fieldName(): string {
50
14
  return `${this.field.name}`;
@@ -92,113 +56,4 @@ export class ManyToManyRelationFieldManagerForEntity
92
56
  return safeInsertImport(this.source, classify(this.field.relationModelSingularName), relatedEntityPath, this.moduleName);
93
57
  }
94
58
 
95
- override removeAdditionalField(): FieldChange {
96
- const changes: Change[] = [];
97
- if (this.isFieldPresent(this.additionalFieldName(), this.relationInverseSource)) {
98
- console.log(`\nEntity removeField ${this.additionalFieldName()} called ...`);
99
- changes.push(...this.removeFieldFor(this.additionalFieldName(), this.relationInverseSource).changes);
100
- }
101
- return {
102
- filePath: this.relationInverseSource.fileName,
103
- field: this.field,
104
- changes: changes,
105
- };
106
- }
107
-
108
- override isAdditionalFieldRequired(): boolean {
109
- return (this.field.type === 'relation' && this.field.relationCreateInverse);
110
- }
111
-
112
- override addAdditionalField(): FieldChange[] {
113
- const fieldChanges: FieldChange[] = [];
114
-
115
- if (this.field.relationCreateInverse) {
116
- fieldChanges.push(this.addAdditionalInverseField());
117
- }
118
-
119
- return fieldChanges;
120
- }
121
-
122
- private addAdditionalInverseField(): FieldChange {
123
- const fieldName = this.additionalFieldName();
124
- const fieldType = this.additionalFieldType().text;
125
- const source= this.relationInverseSource
126
- const field = this.field
127
- const modelName = this.field.relationModelSingularName
128
- const decoratorManagers = this.additionalDecoratorManagers();
129
-
130
- const fieldChange = this.addFieldInternal(fieldName, fieldType, decoratorManagers, field, modelName, source);
131
- if (this.modelName !== this.field.relationModelSingularName) {
132
- const currentModelName = this.modelName;
133
- const currentModuleName = this.moduleName;
134
- fieldChange.changes.push(this.inverseFieldImport(currentModelName, currentModuleName, source));
135
- }
136
- return fieldChange;
137
- }
138
-
139
- private inverseFieldImport(modelName: string, currentModuleName: string, source: ts.SourceFile): Change {
140
- const inverseEntityImportName = `${dasherize(modelName)}.entity`;
141
- const modulePath = `src/${currentModuleName}`;
142
-
143
- const inverseEntityPath = `${modulePath}/entities/${inverseEntityImportName}`;
144
-
145
- return safeInsertImport(source, `${classify(modelName)}`, inverseEntityPath, currentModuleName);
146
- }
147
-
148
- override addOrUpdateAdditionalField(): FieldChange[] {
149
- const fieldChanges: FieldChange[] = [];
150
-
151
- if (this.field.relationCreateInverse) {
152
- fieldChanges.push(this.updateAdditionalInverseField());
153
- }
154
- return fieldChanges;
155
- }
156
-
157
- private updateAdditionalInverseField() : FieldChange{
158
- const fieldName = this.additionalFieldName();
159
- const fieldType = this.additionalFieldType().node(this.field);
160
- const source = this.relationInverseSource;
161
- const field = this.field;
162
- const decoratorManagers = this.additionalDecoratorManagers();
163
-
164
- const inverseField = this.getFieldIdentifierNode(fieldName, source)?.parent as PropertyDeclaration;
165
- if (inverseField == null) {
166
- return this.addAdditionalInverseField();
167
- }
168
- else {
169
- const fieldChange = this.updateFieldInternal(fieldName, fieldType, decoratorManagers, field, source);
170
- if (this.modelName !== this.field.relationModelSingularName) {
171
- const currentModelName = this.modelName;
172
- const currentModuleName = this.moduleName;
173
- fieldChange.changes.push(this.inverseFieldImport(currentModelName, currentModuleName, source));
174
- }
175
- return fieldChange;
176
- }
177
- }
178
-
179
-
180
- additionalFieldType(): FieldType { // The inverse field type will remain the same for both one-to-many and many-to-many relations
181
- const type = `${classify(this.modelName)}`
182
- const text = `${type}[]`
183
- return {
184
- text: text,
185
- node: (_field: any) =>
186
- ts.factory.createArrayTypeNode(ts.factory.createTypeReferenceNode(
187
- ts.factory.createIdentifier(type),
188
- undefined
189
- )),
190
- };
191
- }
192
-
193
- additionalFieldName(): string { // The inverse field type will remain the same for both one-to-many and many-to-many relations
194
- return this.field.relationModelFieldName;
195
- }
196
-
197
- private additionalDecoratorManagers(): DecoratorManager[] {
198
- return [this.inverseManyToManyDecoratorManager];
199
- }
200
-
201
- private isInverseManyToMany(): boolean {
202
- return (this.isManyToMany() && this.field.relationCreateInverse);
203
- }
204
59
  }