@solidstarters/solid-code-builder 1.0.19 → 1.0.21

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 (98) hide show
  1. package/dist/code-builder/lib/field/decorator-managers/dto/ApiPropertyDecoratorManager.d.ts +4 -0
  2. package/dist/code-builder/lib/field/decorator-managers/dto/ApiPropertyDecoratorManager.js +61 -2
  3. package/dist/code-builder/lib/field/decorator-managers/dto/ApiPropertyDecoratorManager.js.map +1 -1
  4. package/dist/code-builder/lib/field/field-managers/base/BaseFieldManagerForDto.d.ts +1 -0
  5. package/dist/code-builder/lib/field/field-managers/base/BaseFieldManagerForDto.js +13 -2
  6. package/dist/code-builder/lib/field/field-managers/base/BaseFieldManagerForDto.js.map +1 -1
  7. package/dist/code-builder/lib/field/field-managers/base/BaseFieldManagerForEntity.js +1 -1
  8. package/dist/code-builder/lib/field/field-managers/base/BaseFieldManagerForEntity.js.map +1 -1
  9. package/package.json +5 -2
  10. package/publish.js +16 -0
  11. package/src/code-builder/lib/field/decorator-managers/dto/ApiPropertyDecoratorManager.ts +71 -2
  12. package/src/code-builder/lib/field/field-managers/base/BaseFieldManagerForDto.ts +21 -1
  13. package/src/code-builder/lib/field/field-managers/base/BaseFieldManagerForEntity.ts +1 -1
  14. package/dto_additional_fields_duplicate_fix.diff +0 -40
  15. package/src/code-builder/index.js +0 -178
  16. package/src/code-builder/index_spec.js +0 -46
  17. package/src/code-builder/lib/field/FieldManager.js +0 -236
  18. package/src/code-builder/lib/field/decorator-managers/dto/ArrayDecoratorManager.js +0 -107
  19. package/src/code-builder/lib/field/decorator-managers/dto/BigIntDecoratorManager.js +0 -100
  20. package/src/code-builder/lib/field/decorator-managers/dto/BooleanDecoratorManager.js +0 -102
  21. package/src/code-builder/lib/field/decorator-managers/dto/DateDecoratorManager.js +0 -106
  22. package/src/code-builder/lib/field/decorator-managers/dto/DecimalDecoratorManager.js +0 -112
  23. package/src/code-builder/lib/field/decorator-managers/dto/EmailDecoratorManager.js +0 -99
  24. package/src/code-builder/lib/field/decorator-managers/dto/IntDecoratorManager.js +0 -100
  25. package/src/code-builder/lib/field/decorator-managers/dto/JsonDecoratorManager.js +0 -100
  26. package/src/code-builder/lib/field/decorator-managers/dto/LengthDecoratorManager.js +0 -104
  27. package/src/code-builder/lib/field/decorator-managers/dto/MaxDecoratorManager.js +0 -103
  28. package/src/code-builder/lib/field/decorator-managers/dto/MaxLengthDecoratorManager.js +0 -103
  29. package/src/code-builder/lib/field/decorator-managers/dto/MinDecoratorManager.js +0 -102
  30. package/src/code-builder/lib/field/decorator-managers/dto/MinLengthDecoratorManager.js +0 -103
  31. package/src/code-builder/lib/field/decorator-managers/dto/NumberDecoratorManager.js +0 -112
  32. package/src/code-builder/lib/field/decorator-managers/dto/OptionalDecoratorManager.js +0 -104
  33. package/src/code-builder/lib/field/decorator-managers/dto/RegexDecoratorManager.js +0 -102
  34. package/src/code-builder/lib/field/decorator-managers/dto/RequiredDecoratorManager.js +0 -100
  35. package/src/code-builder/lib/field/decorator-managers/dto/StringDecoratorManager.js +0 -99
  36. package/src/code-builder/lib/field/decorator-managers/dto/TransformDecoratorManager.js +0 -102
  37. package/src/code-builder/lib/field/decorator-managers/dto/ValidateNestedDecoratorManager.js +0 -140
  38. package/src/code-builder/lib/field/decorator-managers/entity/ColumnDecoratorManager.js +0 -178
  39. package/src/code-builder/lib/field/decorator-managers/entity/IndexDecoratorManager.js +0 -79
  40. package/src/code-builder/lib/field/decorator-managers/entity/JoinTableDecoratorManager.js +0 -118
  41. package/src/code-builder/lib/field/decorator-managers/entity/ManyToManyDecoratorManager.js +0 -175
  42. package/src/code-builder/lib/field/decorator-managers/entity/ManyToOneDecoratorManager.js +0 -179
  43. package/src/code-builder/lib/field/decorator-managers/entity/OneToManyDecoratorManager.js +0 -155
  44. package/src/code-builder/lib/field/decorator-managers/entity/UniqueIndexDecoratorManager.js +0 -174
  45. package/src/code-builder/lib/field/field-managers/base/BaseFieldManagerForDto.js +0 -345
  46. package/src/code-builder/lib/field/field-managers/base/BaseFieldManagerForEntity.js +0 -373
  47. package/src/code-builder/lib/field/field-managers/base/DeleteType.js +0 -11
  48. package/src/code-builder/lib/field/field-managers/bigint/BigIntFieldHandler.js +0 -33
  49. package/src/code-builder/lib/field/field-managers/bigint/BigIntFieldManagerForDto.js +0 -81
  50. package/src/code-builder/lib/field/field-managers/bigint/BigIntFieldManagerForEntity.js +0 -55
  51. package/src/code-builder/lib/field/field-managers/boolean/BooleanFieldHandler.js +0 -33
  52. package/src/code-builder/lib/field/field-managers/boolean/BooleanFieldManagerForDto.js +0 -75
  53. package/src/code-builder/lib/field/field-managers/boolean/BooleanFieldManagerForEntity.js +0 -51
  54. package/src/code-builder/lib/field/field-managers/computed/ComputedFieldHandler.js +0 -30
  55. package/src/code-builder/lib/field/field-managers/computed/ComputedFieldManagerForEntity.js +0 -33
  56. package/src/code-builder/lib/field/field-managers/date/DateFieldHandler.js +0 -33
  57. package/src/code-builder/lib/field/field-managers/date/DateFieldManagerForDto.js +0 -81
  58. package/src/code-builder/lib/field/field-managers/date/DateFieldManagerForEntity.js +0 -59
  59. package/src/code-builder/lib/field/field-managers/decimal/DecimalFieldHandler.js +0 -33
  60. package/src/code-builder/lib/field/field-managers/decimal/DecimalFieldManagerForDto.js +0 -77
  61. package/src/code-builder/lib/field/field-managers/decimal/DecimalFieldManagerForEntity.js +0 -53
  62. package/src/code-builder/lib/field/field-managers/email/EmailFieldHandler.js +0 -34
  63. package/src/code-builder/lib/field/field-managers/email/EmailFieldManagerForDto.js +0 -72
  64. package/src/code-builder/lib/field/field-managers/email/EmailFieldManagerForEntity.js +0 -44
  65. package/src/code-builder/lib/field/field-managers/int/IntFieldHandler.js +0 -33
  66. package/src/code-builder/lib/field/field-managers/int/IntFieldManagerForDto.js +0 -76
  67. package/src/code-builder/lib/field/field-managers/int/IntFieldManagerForEntity.js +0 -52
  68. package/src/code-builder/lib/field/field-managers/json/JsonFieldHandler.js +0 -34
  69. package/src/code-builder/lib/field/field-managers/json/JsonFieldManagerForDto.js +0 -57
  70. package/src/code-builder/lib/field/field-managers/json/JsonFieldManagerForEntity.js +0 -21
  71. package/src/code-builder/lib/field/field-managers/long-text/LongTextFieldHandler.js +0 -34
  72. package/src/code-builder/lib/field/field-managers/long-text/LongTextFieldManagerForDto.js +0 -63
  73. package/src/code-builder/lib/field/field-managers/long-text/LongTextFieldManagerForEntity.js +0 -34
  74. package/src/code-builder/lib/field/field-managers/media/MediaFieldHandler.js +0 -33
  75. package/src/code-builder/lib/field/field-managers/media/MediaFieldManagerForDto.js +0 -72
  76. package/src/code-builder/lib/field/field-managers/media/MediaFieldManagerForEntity.js +0 -57
  77. package/src/code-builder/lib/field/field-managers/no-ops/NoOpsFieldHandler.js +0 -27
  78. package/src/code-builder/lib/field/field-managers/no-ops/NoOpsFieldManagerForDto.js +0 -75
  79. package/src/code-builder/lib/field/field-managers/no-ops/NoOpsFieldManagerForEntity.js +0 -35
  80. package/src/code-builder/lib/field/field-managers/password/PasswordFieldHandler.js +0 -34
  81. package/src/code-builder/lib/field/field-managers/password/PasswordFieldManagerForDto.js +0 -121
  82. package/src/code-builder/lib/field/field-managers/password/PasswordFieldManagerForEntity.js +0 -37
  83. package/src/code-builder/lib/field/field-managers/relation/ManyToManyRelationFieldHandler.js +0 -33
  84. package/src/code-builder/lib/field/field-managers/relation/ManyToManyRelationFieldManagerForDto.js +0 -366
  85. package/src/code-builder/lib/field/field-managers/relation/ManyToManyRelationFieldManagerForEntity.js +0 -158
  86. package/src/code-builder/lib/field/field-managers/relation/ManyToOneRelationFieldHandler.js +0 -33
  87. package/src/code-builder/lib/field/field-managers/relation/ManyToOneRelationFieldManagerForDto.js +0 -294
  88. package/src/code-builder/lib/field/field-managers/relation/ManyToOneRelationFieldManagerForEntity.js +0 -177
  89. package/src/code-builder/lib/field/field-managers/rich-text/RichTextFieldHandler.js +0 -34
  90. package/src/code-builder/lib/field/field-managers/rich-text/RichTextFieldManagerForDto.js +0 -63
  91. package/src/code-builder/lib/field/field-managers/rich-text/RichTextFieldManagerForEntity.js +0 -34
  92. package/src/code-builder/lib/field/field-managers/short-text/ShortTextFieldHandler.js +0 -34
  93. package/src/code-builder/lib/field/field-managers/short-text/ShortTextFieldManagerForDto.js +0 -63
  94. package/src/code-builder/lib/field/field-managers/short-text/ShortTextFieldManagerForEntity.js +0 -37
  95. package/src/code-builder/lib/field/field-managers/uuid/UUIDFieldHandler.js +0 -34
  96. package/src/code-builder/lib/field/field-managers/uuid/UUIDFieldManagerForDto.js +0 -15
  97. package/src/code-builder/lib/field/field-managers/uuid/UUIDFieldManagerForEntity.js +0 -33
  98. package/src/code-builder/lib/model/helpers.js +0 -407
@@ -1,178 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.ColumnDecoratorManager = void 0;
7
- const typescript_1 = __importDefault(require("@schematics/angular/third_party/github.com/Microsoft/TypeScript/lib/typescript"));
8
- const lodash_1 = __importDefault(require("lodash"));
9
- const ast_utils_1 = require("@schematics/angular/utility/ast-utils");
10
- class ColumnDecoratorManager {
11
- constructor(options, fieldNode) {
12
- this.options = options;
13
- this.fieldNode = fieldNode;
14
- }
15
- isApplyDecorator() {
16
- return this.options.isColumn;
17
- }
18
- decoratorName() {
19
- return 'Column';
20
- }
21
- setFieldNode(fieldNode) {
22
- this.fieldNode = fieldNode;
23
- }
24
- buildDecorator() {
25
- const fieldSourceLines = [];
26
- const changes = [];
27
- //Add the Column decorator to the field
28
- const columnDecoratorLine = `@${this.decoratorName()}(${this.buildColumnOptionsCode()})`;
29
- fieldSourceLines.push(columnDecoratorLine);
30
- changes.push(...this.decoratorImports());
31
- return {
32
- filePath: this.options.source.fileName,
33
- field: this.options.field,
34
- changes: changes,
35
- fieldSourceLines: fieldSourceLines,
36
- };
37
- }
38
- decoratorImports() {
39
- return [(0, ast_utils_1.insertImport)(this.options.source, this.options.source.fileName, this.decoratorName(), 'typeorm')];
40
- }
41
- updateDecorator() {
42
- if (!this.fieldNode)
43
- throw new Error('Field node is required for updating the index decorator');
44
- let newModifiers = [];
45
- let existingModifiers = this.fieldNode.modifiers;
46
- // Check if the field has an Column decorator.
47
- const existingDecorator = this.findDecorator(this.decoratorName(), existingModifiers);
48
- //Remove the column decorator if the column decorator exists
49
- //TODO probably this too can be done in a lesser intrusive way i.e update everything instead of removing and adding
50
- newModifiers = [...this.filterNonDecorators(existingModifiers), ...this.filterOtherDecorators(this.decoratorName(), existingModifiers)];
51
- //Add the column decorator if column decorator is required
52
- const changes = [];
53
- if (this.isApplyDecorator()) {
54
- newModifiers = [...newModifiers, this.createColumnDecorator(existingDecorator)];
55
- changes.push(...this.decoratorImports());
56
- }
57
- const updatedProperty = typescript_1.default.factory.updatePropertyDeclaration(this.fieldNode, newModifiers, //Replace with new modifiers
58
- this.fieldNode.name, this.fieldNode.questionToken, this.fieldNode.type, this.fieldNode.initializer);
59
- return [updatedProperty, changes];
60
- }
61
- findDecorator(name, existingModifiers) {
62
- return existingModifiers ? existingModifiers.filter((m) => (m.kind === typescript_1.default.SyntaxKind.Decorator)).map(m => m).filter(m => this.containsIdentifierName(m, name)).pop() : undefined;
63
- }
64
- filterOtherDecorators(name, existingModifiers) {
65
- return existingModifiers ? existingModifiers.filter((m) => (m.kind === typescript_1.default.SyntaxKind.Decorator)).map(m => m).filter(m => !this.containsIdentifierName(m, name)) : [];
66
- }
67
- filterNonDecorators(existingModifiers) {
68
- return existingModifiers ? existingModifiers.filter((m) => (m.kind !== typescript_1.default.SyntaxKind.Decorator)).map(m => m) : [];
69
- }
70
- containsIdentifierName(m, identifierName) {
71
- const callExpression = m.expression;
72
- const identifier = callExpression.expression;
73
- return identifier.text === identifierName;
74
- }
75
- createColumnDecorator(existingColumnDecorator) {
76
- // Capture the existing column decorator options
77
- let existingColumnDecoratorPropertyAssignments = [];
78
- if (existingColumnDecorator !== undefined) {
79
- //Pre-set the column options from the existing column decorator
80
- const existingCallExpression = existingColumnDecorator.expression;
81
- // Check if call expression has at least 1 arguments
82
- if (existingCallExpression.arguments.length > 0) {
83
- if (!typescript_1.default.isObjectLiteralExpression(existingCallExpression.arguments[0])) {
84
- throw new Error('Column decorator 1st arguments must be an object literal containing the column options');
85
- }
86
- const existingObjectLiteralExpression = existingCallExpression.arguments[0];
87
- existingColumnDecoratorPropertyAssignments.push(...existingObjectLiteralExpression.properties);
88
- }
89
- }
90
- // Create the new column decorator options as property assignments, if the option is not null
91
- const columnDecoratorOptions = this.createColumnDecoratorOptions();
92
- const newPropertyAssignments = Array.from(columnDecoratorOptions.values()).filter(p => p !== null);
93
- // console.log('newPropertyAssignments', newPropertyAssignments.map(p => JSON.stringify(p.name)).join(', '));
94
- // Add the other unhandled column decorator options
95
- const handledColumnDecoratorOptions = Array.from(columnDecoratorOptions.keys());
96
- //@ts-ignore
97
- const otherPropertyAssignments = existingColumnDecoratorPropertyAssignments.filter(ps => { var _a; return !handledColumnDecoratorOptions.includes((_a = ps.name) === null || _a === void 0 ? void 0 : _a.escapedText); });
98
- newPropertyAssignments.push(...otherPropertyAssignments);
99
- // Re-create the column decorator with the merged column decorator options
100
- const decoratorIdentifier = typescript_1.default.factory.createIdentifier(this.decoratorName());
101
- const decoratorOptions = typescript_1.default.factory.createObjectLiteralExpression(newPropertyAssignments);
102
- const call = typescript_1.default.factory.createCallExpression(decoratorIdentifier, undefined, [decoratorOptions]);
103
- return typescript_1.default.factory.createDecorator(call);
104
- }
105
- createColumnDecoratorOptions() {
106
- const options = new Map();
107
- //Set common options
108
- //TODO Proper types to be used e.g ColumnCommonOptions
109
- // if (this.field.defaultValue) {
110
- // options.set('default', this.field.defaultValue);
111
- // }
112
- (!lodash_1.default.isEmpty(this.options.columnName)) ? options.set('name', typescript_1.default.factory.createStringLiteral(this.options.columnName)) : options.set('name', null);
113
- (this.options.type) ? options.set('type', typescript_1.default.factory.createStringLiteral(this.options.type)) : options.set('type', null);
114
- if (!this.options.required) {
115
- options.set('nullable', typescript_1.default.factory.createTrue());
116
- }
117
- //Set additional options
118
- this.options.otherOptionExpressions.forEach((value, key) => {
119
- options.set(key, value);
120
- });
121
- return this.optionsToPropertyAssignmentsOrNull(options);
122
- }
123
- optionsToPropertyAssignmentsOrNull(options) {
124
- const decoratorOptions = new Map();
125
- options.forEach((value, key) => {
126
- if (value !== null) {
127
- decoratorOptions.set(key, typescript_1.default.factory.createPropertyAssignment(typescript_1.default.factory.createIdentifier(key), value));
128
- }
129
- else {
130
- decoratorOptions.set(key, null);
131
- }
132
- });
133
- return decoratorOptions;
134
- }
135
- buildColumnDecoratorOptions() {
136
- const options = new Map();
137
- //Set common options
138
- //TODO Proper types to be used e.g ColumnCommonOptions
139
- // if (this.options.field.defaultValue) {
140
- // options.set('default', this.options.field.defaultValue);
141
- // }
142
- if (!lodash_1.default.isEmpty(this.options.columnName)) {
143
- options.set('name', `${this.options.columnName}`);
144
- }
145
- if (!lodash_1.default.isEmpty(this.options.type)) {
146
- options.set('type', `${this.options.type}`);
147
- }
148
- if (!this.options.required) {
149
- options.set('nullable', true);
150
- }
151
- // Set some keys as null, to indicate they are handled i.e they will be removed & recreated, if required
152
- //Handled keys
153
- // options.set('length', null) //TODO Can be improved, since handled keys needs to be managed in this decorator. Ideally it should be managed in the corresponding field
154
- //Set additional options
155
- this.options.otherOptions.forEach((value, key) => {
156
- options.set(key, value);
157
- });
158
- return options;
159
- }
160
- buildColumnOptionsCode() {
161
- const options = this.buildColumnDecoratorOptions();
162
- const keys = Array.from(options.keys());
163
- const optionsString = keys
164
- .filter((key) => options.get(key) !== null)
165
- .map((key) => {
166
- if (typeof options.get(key) === 'string') {
167
- return `${key}: "${options.get(key)}"`;
168
- }
169
- else {
170
- return `${key}: ${options.get(key)}`;
171
- }
172
- })
173
- .join(', ');
174
- return `{ ${optionsString} }`;
175
- }
176
- }
177
- exports.ColumnDecoratorManager = ColumnDecoratorManager;
178
- //# sourceMappingURL=ColumnDecoratorManager.js.map
@@ -1,79 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.IndexDecoratorManager = void 0;
7
- const typescript_1 = __importDefault(require("@schematics/angular/third_party/github.com/Microsoft/TypeScript/lib/typescript"));
8
- const FieldManager_1 = require("../../FieldManager");
9
- const ast_utils_1 = require("@schematics/angular/utility/ast-utils");
10
- class IndexDecoratorManager {
11
- constructor(options, fieldNode) {
12
- this.options = options;
13
- this.fieldNode = fieldNode;
14
- }
15
- isApplyDecorator() {
16
- return this.options.index && this.options.field.relationType !== FieldManager_1.RelationType.ManyToMany;
17
- }
18
- decoratorName() {
19
- return 'Index';
20
- }
21
- setFieldNode(fieldNode) {
22
- this.fieldNode = fieldNode;
23
- }
24
- buildDecorator() {
25
- const fieldSourceLines = [];
26
- const changes = [];
27
- // if (this.field.index) {
28
- const indexDecoratorLine = `@Index()`;
29
- fieldSourceLines.push(indexDecoratorLine);
30
- changes.push(...this.decoratorImports());
31
- // }
32
- return {
33
- filePath: this.options.source.fileName,
34
- field: this.options.field,
35
- changes: changes,
36
- fieldSourceLines: fieldSourceLines,
37
- };
38
- }
39
- decoratorImports() {
40
- return [(0, ast_utils_1.insertImport)(this.options.source, this.options.source.fileName, this.decoratorName(), 'typeorm')];
41
- }
42
- //Updates the index decorator for the fieldNode based on the options provided
43
- // @returns the updated property declaration
44
- updateDecorator() {
45
- if (!this.fieldNode)
46
- throw new Error('Field node is required for updating the index decorator');
47
- let newModifiers = [];
48
- let existingModifiers = this.fieldNode.modifiers;
49
- //Remove the Index decorator if the Index decorator exists
50
- //TODO probably this too can be done in a lesser intrusive way i.e update everything instead of removing and adding
51
- newModifiers = [...this.filterNonDecorators(existingModifiers), ...this.filterOtherDecorators(this.decoratorName(), existingModifiers)];
52
- //Add the column decorator if column decorator is required
53
- const changes = [];
54
- if (this.isApplyDecorator()) {
55
- newModifiers = [...newModifiers, this.createIndexDecorator()];
56
- changes.push(...this.decoratorImports());
57
- }
58
- //If index decorator is present & index is required, no need to update the index decorator
59
- const updatedProperty = typescript_1.default.factory.updatePropertyDeclaration(this.fieldNode, newModifiers, //newModifiers contains the updated index decorator
60
- this.fieldNode.name, this.fieldNode.questionToken, this.fieldNode.type, this.fieldNode.initializer);
61
- return [updatedProperty, changes];
62
- }
63
- createIndexDecorator() {
64
- return typescript_1.default.factory.createDecorator(typescript_1.default.factory.createCallExpression(typescript_1.default.factory.createIdentifier('Index'), undefined, []));
65
- }
66
- filterOtherDecorators(name, existingModifiers) {
67
- return existingModifiers ? existingModifiers.filter((m) => (m.kind === typescript_1.default.SyntaxKind.Decorator)).map(m => m).filter(m => !this.containsIdentifierName(m, name)) : [];
68
- }
69
- filterNonDecorators(existingModifiers) {
70
- return existingModifiers ? existingModifiers.filter((m) => (m.kind !== typescript_1.default.SyntaxKind.Decorator)).map(m => m) : [];
71
- }
72
- containsIdentifierName(m, identifierName) {
73
- const callExpression = m.expression;
74
- const identifier = callExpression.expression;
75
- return identifier.text === identifierName;
76
- }
77
- }
78
- exports.IndexDecoratorManager = IndexDecoratorManager;
79
- //# sourceMappingURL=IndexDecoratorManager.js.map
@@ -1,118 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.JoinTableDecoratorManager = void 0;
7
- const typescript_1 = __importDefault(require("@schematics/angular/third_party/github.com/Microsoft/TypeScript/lib/typescript"));
8
- const ast_utils_1 = require("@schematics/angular/utility/ast-utils");
9
- class JoinTableDecoratorManager {
10
- constructor(options, fieldNode) {
11
- this.options = options;
12
- this.fieldNode = fieldNode;
13
- }
14
- isApplyDecorator() {
15
- return this.options.isManyToManyRelationOwner;
16
- }
17
- decoratorName() {
18
- return 'JoinTable';
19
- }
20
- buildDecorator() {
21
- const changes = [];
22
- const fieldSourceLines = [];
23
- // Add the many-to-many import
24
- // const decoratorImport = insertImport(
25
- // this.options.source,
26
- // this.options.source.fileName,
27
- // this.decoratorName(),
28
- // 'typeorm',
29
- // );
30
- // if (decoratorImport) {
31
- // changes.push(decoratorImport);
32
- // }
33
- fieldSourceLines.push(`@${this.decoratorName()}()`);
34
- changes.push(...this.decoratorImports());
35
- return {
36
- filePath: this.options.source.fileName,
37
- field: this.options.field,
38
- changes: changes,
39
- fieldSourceLines: fieldSourceLines,
40
- };
41
- }
42
- decoratorImports() {
43
- return [(0, ast_utils_1.insertImport)(this.options.source, this.options.source.fileName, this.decoratorName(), 'typeorm')];
44
- }
45
- setFieldNode(fieldNode) {
46
- this.fieldNode = fieldNode;
47
- }
48
- updateDecorator() {
49
- // Check if the field property declaration exists, if not throw an error
50
- if (!this.fieldNode)
51
- throw new Error('Field node is required for updating the one to many decorator');
52
- let newModifiers = [];
53
- let existingModifiers = this.fieldNode.modifiers;
54
- // Check if field has an existing one-to-many relation decorator
55
- const existingDecorator = this.findDecorator(this.decoratorName(), existingModifiers);
56
- //Remove the many-to-one decorator if it exists
57
- //TODO probably this too can be done in a lesser intrusive way i.e update everything instead of removing and adding
58
- newModifiers = [...this.filterNonDecorators(existingModifiers), ...this.filterOtherDecorators(this.decoratorName(), existingModifiers)];
59
- //Add the one-to-many decorator if it is required
60
- const changes = [];
61
- if (this.isApplyDecorator()) {
62
- newModifiers = [...newModifiers, this.createDecorator(existingDecorator)];
63
- changes.push(...this.decoratorImports());
64
- }
65
- const updatedProperty = typescript_1.default.factory.updatePropertyDeclaration(this.fieldNode, newModifiers, //Replace with new modifiers
66
- this.fieldNode.name, this.fieldNode.questionToken, this.fieldNode.type, this.fieldNode.initializer);
67
- return [updatedProperty, changes];
68
- }
69
- createDecorator(existingDecorator) {
70
- let existingRelationOptions = this.existingDecoratorOptions(existingDecorator);
71
- // const mergedRelationOptions: ObjectLiteralElementLike[] = this.mergeNewAndExistingDecoratorOptions(existingRelationOptions);
72
- // Re-create the decorator with the merged decorator options
73
- const decoratorIdentifier = typescript_1.default.factory.createIdentifier(this.decoratorName());
74
- const argumentsArray = this.createDecoratorArguments(existingRelationOptions);
75
- const call = typescript_1.default.factory.createCallExpression(decoratorIdentifier, undefined, argumentsArray);
76
- return typescript_1.default.factory.createDecorator(call);
77
- }
78
- createDecoratorArguments(joinTableOptions) {
79
- const argumentsArray = [];
80
- if (joinTableOptions.length > 0) {
81
- const validationOptionsObjectLiteral = typescript_1.default.factory.createObjectLiteralExpression(joinTableOptions);
82
- argumentsArray.push(validationOptionsObjectLiteral);
83
- }
84
- return argumentsArray;
85
- }
86
- existingDecoratorOptions(existingDecorator) {
87
- let existingDecoratorPropertyAssignments = [];
88
- if (existingDecorator !== undefined) {
89
- //Pre-set the relation options from the existing relation decorator
90
- const existingCallExpression = existingDecorator.expression;
91
- // Check if call expression has at least 1 arguments
92
- if (existingCallExpression.arguments.length > 0) { //Because we are interested in the 1st argument, we check for length > 0
93
- if (!typescript_1.default.isObjectLiteralExpression(existingCallExpression.arguments[0])) {
94
- throw new Error('JoinTable decorator 1st argument must be an object literal containing the joinTable options');
95
- }
96
- const existingObjectLiteralExpression = existingCallExpression.arguments[0];
97
- existingDecoratorPropertyAssignments.push(...existingObjectLiteralExpression.properties);
98
- }
99
- }
100
- return existingDecoratorPropertyAssignments;
101
- }
102
- findDecorator(name, existingModifiers) {
103
- return existingModifiers ? existingModifiers.filter((m) => (m.kind === typescript_1.default.SyntaxKind.Decorator)).map(m => m).filter(m => this.containsIdentifierName(m, name)).pop() : undefined;
104
- }
105
- filterOtherDecorators(name, existingModifiers) {
106
- return existingModifiers ? existingModifiers.filter((m) => (m.kind === typescript_1.default.SyntaxKind.Decorator)).map(m => m).filter(m => !this.containsIdentifierName(m, name)) : [];
107
- }
108
- filterNonDecorators(existingModifiers) {
109
- return existingModifiers ? existingModifiers.filter((m) => (m.kind !== typescript_1.default.SyntaxKind.Decorator)).map(m => m) : [];
110
- }
111
- containsIdentifierName(m, identifierName) {
112
- const callExpression = m.expression;
113
- const identifier = callExpression.expression;
114
- return identifier.text === identifierName;
115
- }
116
- }
117
- exports.JoinTableDecoratorManager = JoinTableDecoratorManager;
118
- //# sourceMappingURL=JoinTableDecoratorManager.js.map
@@ -1,175 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.ManyToManyDecoratorManager = void 0;
7
- const strings_1 = require("@angular-devkit/core/src/utils/strings");
8
- const typescript_1 = __importDefault(require("@schematics/angular/third_party/github.com/Microsoft/TypeScript/lib/typescript"));
9
- const ast_utils_1 = require("@schematics/angular/utility/ast-utils");
10
- class ManyToManyDecoratorManager {
11
- constructor(options, fieldNode) {
12
- this.options = options;
13
- this.fieldNode = fieldNode;
14
- }
15
- isApplyDecorator() {
16
- return this.options.isManyToMany;
17
- }
18
- decoratorName() {
19
- return 'ManyToMany';
20
- }
21
- buildDecorator() {
22
- const changes = [];
23
- const fieldSourceLines = [];
24
- // Add the many-to-many import
25
- // const decoratorImport = insertImport(
26
- // this.options.source,
27
- // this.options.source.fileName,
28
- // this.decoratorName(),
29
- // 'typeorm',
30
- // );
31
- changes.push(...this.decoratorImports());
32
- // if (decoratorImport) {
33
- // changes.push(decoratorImport);
34
- // }
35
- const fieldSourceLineComponents = [];
36
- fieldSourceLineComponents.push(`() => ${(0, strings_1.classify)(this.options.relationModelName)}`);
37
- this.options.relationInverseFieldName ? fieldSourceLineComponents.push(`${(0, strings_1.camelize)(this.options.relationModelName)} => ${(0, strings_1.camelize)(this.options.relationModelName)}.${this.options.relationInverseFieldName}`) : "no-ops";
38
- fieldSourceLineComponents.push(`${this.buildRelationOptionsCode()}`);
39
- fieldSourceLines.push(`@${this.decoratorName()}(${fieldSourceLineComponents.join(', ')})`);
40
- return {
41
- filePath: this.options.source.fileName,
42
- field: this.options.field,
43
- changes: changes,
44
- fieldSourceLines: fieldSourceLines,
45
- };
46
- }
47
- setFieldNode(fieldNode) {
48
- this.fieldNode = fieldNode;
49
- }
50
- decoratorImports() {
51
- return [(0, ast_utils_1.insertImport)(this.options.source, this.options.source.fileName, this.decoratorName(), 'typeorm')];
52
- }
53
- updateDecorator() {
54
- // Check if the field property declaration exists, if not throw an error
55
- if (!this.fieldNode)
56
- throw new Error('Field node is required for updating the one to many decorator');
57
- let newModifiers = [];
58
- let existingModifiers = this.fieldNode.modifiers;
59
- // Check if field has an existing one-to-many relation decorator
60
- const existingDecorator = this.findDecorator(this.decoratorName(), existingModifiers);
61
- //Remove the many-to-one decorator if it exists
62
- //TODO probably this too can be done in a lesser intrusive way i.e update everything instead of removing and adding
63
- newModifiers = [...this.filterNonDecorators(existingModifiers), ...this.filterOtherDecorators(this.decoratorName(), existingModifiers)];
64
- //Add the one-to-many decorator if it is required
65
- const changes = [];
66
- if (this.isApplyDecorator()) {
67
- newModifiers = [...newModifiers, this.createRelationDecorator(existingDecorator)];
68
- changes.push(...this.decoratorImports());
69
- }
70
- const updatedProperty = typescript_1.default.factory.updatePropertyDeclaration(this.fieldNode, newModifiers, //Replace with new modifiers
71
- this.fieldNode.name, this.fieldNode.questionToken, this.fieldNode.type, this.fieldNode.initializer);
72
- return [updatedProperty, changes];
73
- }
74
- buildRelationOptionsCode() {
75
- const relationOptions = new Map();
76
- // this.options.owner ? relationOptions.set('cascade', true): "no-ops";
77
- this.options.owner ? relationOptions.set('cascade', true) : relationOptions.set('cascade', ['insert', 'update']);
78
- const decoratorArgs = Array.from(relationOptions)
79
- .map(([key, value]) => {
80
- if (Array.isArray(value)) {
81
- return `${key}: [${value.map(v => `'${v}'`).join(', ')}]`;
82
- }
83
- return `${key}: ${value}`;
84
- })
85
- .join(', ');
86
- // Convert the map to a string
87
- return `{ ${decoratorArgs} }`;
88
- }
89
- createRelationDecorator(existingRelationDecorator) {
90
- let existingRelationOptions = this.existingDecoratorOptions(existingRelationDecorator);
91
- const mergedRelationOptions = this.mergeNewAndExistingDecoratorOptions(existingRelationOptions);
92
- // Re-create the decorator with the merged decorator options
93
- const decoratorIdentifier = typescript_1.default.factory.createIdentifier(this.decoratorName());
94
- const argumentsArray = this.createDecoratorArguments(mergedRelationOptions);
95
- const call = typescript_1.default.factory.createCallExpression(decoratorIdentifier, undefined, argumentsArray);
96
- return typescript_1.default.factory.createDecorator(call);
97
- }
98
- mergeNewAndExistingDecoratorOptions(existingRelationOptions) {
99
- const decoratorOptions = this.createRelationDecoratorOptions();
100
- const newPropertyAssignments = Array.from(decoratorOptions.values()).filter(p => p !== null);
101
- // console.log('newPropertyAssignments', newPropertyAssignments.map(p => JSON.stringify(p.name)).join(', '));
102
- // Add the other unhandled column decorator options
103
- const handledDecoratorOptions = Array.from(decoratorOptions.keys());
104
- //@ts-ignore
105
- const otherPropertyAssignments = existingRelationOptions.filter(ps => { var _a; return !handledDecoratorOptions.includes((_a = ps.name) === null || _a === void 0 ? void 0 : _a.escapedText); });
106
- newPropertyAssignments.push(...otherPropertyAssignments);
107
- return newPropertyAssignments;
108
- }
109
- createDecoratorArguments(newPropertyAssignments) {
110
- const argumentsArray = [];
111
- // 1st Argument: Target
112
- const typeFunctionOrTarget = typescript_1.default.factory.createArrowFunction(undefined, undefined, [], undefined, typescript_1.default.factory.createToken(typescript_1.default.SyntaxKind.EqualsGreaterThanToken), typescript_1.default.factory.createIdentifier((0, strings_1.classify)(this.options.relationModelName)));
113
- argumentsArray.push(typeFunctionOrTarget);
114
- if (this.options.relationInverseFieldName) {
115
- // 2nd Argument: Inverse side
116
- const inverseSide = typescript_1.default.factory.createArrowFunction(undefined, undefined, [typescript_1.default.factory.createParameterDeclaration(undefined, undefined, typescript_1.default.factory.createIdentifier((0, strings_1.camelize)(this.options.relationModelName)), undefined, undefined, undefined)], undefined, typescript_1.default.factory.createToken(typescript_1.default.SyntaxKind.EqualsGreaterThanToken), typescript_1.default.factory.createPropertyAccessExpression(typescript_1.default.factory.createIdentifier((0, strings_1.camelize)(this.options.relationModelName)), typescript_1.default.factory.createIdentifier(this.options.relationInverseFieldName)));
117
- argumentsArray.push(inverseSide);
118
- }
119
- // 3rd Argument: Options. This argument is optional
120
- if (newPropertyAssignments.length > 0) {
121
- const decoratorOptions = typescript_1.default.factory.createObjectLiteralExpression(newPropertyAssignments);
122
- argumentsArray.push(decoratorOptions);
123
- }
124
- return argumentsArray;
125
- }
126
- existingDecoratorOptions(existingRelationDecorator) {
127
- let existingRelationDecoratorPropertyAssignments = [];
128
- if (existingRelationDecorator !== undefined) {
129
- //Pre-set the relation options from the existing relation decorator
130
- const existingCallExpression = existingRelationDecorator.expression;
131
- // Check if call expression has at least 1 arguments
132
- if (existingCallExpression.arguments.length > 2) { //Because we are interested in the 2nd argument, we check for length > 1
133
- if (!typescript_1.default.isObjectLiteralExpression(existingCallExpression.arguments[2])) {
134
- throw new Error('Many-to-Many decorator 3rd argument must be an object literal containing the relation options');
135
- }
136
- const existingObjectLiteralExpression = existingCallExpression.arguments[2];
137
- existingRelationDecoratorPropertyAssignments.push(...existingObjectLiteralExpression.properties);
138
- }
139
- }
140
- return existingRelationDecoratorPropertyAssignments;
141
- }
142
- createRelationDecoratorOptions() {
143
- const relationOptions = new Map();
144
- this.options.owner ? relationOptions.set('cascade', typescript_1.default.factory.createTrue()) : "no-ops";
145
- return this.optionsToPropertyAssignmentsOrNull(relationOptions);
146
- }
147
- optionsToPropertyAssignmentsOrNull(options) {
148
- const decoratorOptions = new Map();
149
- options.forEach((value, key) => {
150
- if (value !== null) {
151
- decoratorOptions.set(key, typescript_1.default.factory.createPropertyAssignment(typescript_1.default.factory.createIdentifier(key), value));
152
- }
153
- else {
154
- decoratorOptions.set(key, null);
155
- }
156
- });
157
- return decoratorOptions;
158
- }
159
- findDecorator(name, existingModifiers) {
160
- return existingModifiers ? existingModifiers.filter((m) => (m.kind === typescript_1.default.SyntaxKind.Decorator)).map(m => m).filter(m => this.containsIdentifierName(m, name)).pop() : undefined;
161
- }
162
- filterOtherDecorators(name, existingModifiers) {
163
- return existingModifiers ? existingModifiers.filter((m) => (m.kind === typescript_1.default.SyntaxKind.Decorator)).map(m => m).filter(m => !this.containsIdentifierName(m, name)) : [];
164
- }
165
- filterNonDecorators(existingModifiers) {
166
- return existingModifiers ? existingModifiers.filter((m) => (m.kind !== typescript_1.default.SyntaxKind.Decorator)).map(m => m) : [];
167
- }
168
- containsIdentifierName(m, identifierName) {
169
- const callExpression = m.expression;
170
- const identifier = callExpression.expression;
171
- return identifier.text === identifierName;
172
- }
173
- }
174
- exports.ManyToManyDecoratorManager = ManyToManyDecoratorManager;
175
- //# sourceMappingURL=ManyToManyDecoratorManager.js.map