@apso/cli 0.1.0 → 0.1.4
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/commands/server/scaffold.d.ts +1 -1
- package/dist/commands/server/scaffold.js +5 -6
- package/dist/lib/apsorc-parser.d.ts +27 -1
- package/dist/lib/apsorc-parser.js +97 -3
- package/dist/lib/controller.d.ts +8 -0
- package/dist/lib/controller.js +24 -9
- package/dist/lib/dto.d.ts +13 -7
- package/dist/lib/dto.js +33 -25
- package/dist/lib/entity.d.ts +9 -0
- package/dist/lib/entity.js +13 -3
- package/dist/lib/index-module.d.ts +11 -0
- package/dist/lib/index-module.js +11 -0
- package/dist/lib/module.d.ts +13 -1
- package/dist/lib/module.js +26 -10
- package/dist/lib/service.d.ts +9 -1
- package/dist/lib/service.js +22 -7
- package/dist/lib/templates/rest/service-rest-spec.eta +38 -0
- package/dist/lib/utils/relationships/parse.d.ts +45 -1
- package/dist/lib/utils/relationships/parse.js +226 -55
- package/dist/lib/utils/relationships/parse.spec.d.ts +1 -0
- package/dist/lib/utils/relationships/parse.spec.js +218 -0
- package/dist/tests/utils/relationship-deduplication.spec.d.ts +1 -0
- package/dist/tests/utils/relationship-deduplication.spec.js +97 -0
- package/oclif.manifest.json +1 -1
- package/package.json +3 -1
|
@@ -1,11 +1,55 @@
|
|
|
1
1
|
import { ApsorcRelationship, Relationship, RelationshipMap, RelationshipForTemplate } from "../../types";
|
|
2
|
+
/**
|
|
3
|
+
* Parses a OneToMany relationship definition.
|
|
4
|
+
* These are treated as bidirectional from the .apsorc definition.
|
|
5
|
+
* Only generates the configuration for the 'from' side.
|
|
6
|
+
* @param relationship The .apsorc relationship definition.
|
|
7
|
+
* @returns A RelationshipMap containing only the entry for the 'from' side.
|
|
8
|
+
*/
|
|
2
9
|
export declare const parseOneToMany: (relationship: ApsorcRelationship) => RelationshipMap;
|
|
10
|
+
/**
|
|
11
|
+
* Parses a ManyToOne relationship definition.
|
|
12
|
+
* These are treated as unidirectional from the .apsorc definition.
|
|
13
|
+
* Only generates the configuration for the 'from' side.
|
|
14
|
+
* @param relationship The .apsorc relationship definition.
|
|
15
|
+
* @returns A RelationshipMap containing only the entry for the 'from' side.
|
|
16
|
+
*/
|
|
3
17
|
export declare const parseManytoOne: (relationship: ApsorcRelationship) => RelationshipMap;
|
|
18
|
+
/**
|
|
19
|
+
* Parses a ManyToMany relationship definition.
|
|
20
|
+
* Determines bidirectionality based on flags and inverse definitions.
|
|
21
|
+
* Determines the owning side (which gets the @JoinTable decorator).
|
|
22
|
+
* Calculates reference names (property names), defaulting if 'to_name' is missing.
|
|
23
|
+
* If bidirectional, generates configuration for both 'from' and 'to' sides.
|
|
24
|
+
* @param relationship The .apsorc relationship definition.
|
|
25
|
+
* @param allRelationships The full list of relationship definitions for finding inverses.
|
|
26
|
+
* @returns A RelationshipMap containing entries for one or both sides.
|
|
27
|
+
*/
|
|
4
28
|
export declare const parseManyToMany: (relationship: ApsorcRelationship, allRelationships: ApsorcRelationship[]) => RelationshipMap;
|
|
5
|
-
|
|
29
|
+
/**
|
|
30
|
+
* Parses a single relationship definition from .apsorc.
|
|
31
|
+
* Dispatches to the appropriate type-specific parser (parseManyToMany, parseOneToMany, etc.).
|
|
32
|
+
* For bidirectional relationships, the type-specific parsers handle generating data for both sides.
|
|
33
|
+
* @param relationship The single .apsorc relationship definition to parse.
|
|
34
|
+
* @param allRelationships The full list of relationship definitions (needed for context, e.g., finding inverses).
|
|
35
|
+
* @returns A RelationshipMap, potentially containing entries for both entities involved in the relationship.
|
|
36
|
+
*/
|
|
6
37
|
export declare const parseRelationship: (relationship: ApsorcRelationship, allRelationships: ApsorcRelationship[]) => RelationshipMap;
|
|
38
|
+
/**
|
|
39
|
+
* Parses the entire array of relationship definitions from the .apsorc file.
|
|
40
|
+
* Iterates through each definition, calls parseRelationship, and merges the results.
|
|
41
|
+
* @param relationships The array of relationship definitions from .apsorc.
|
|
42
|
+
* @returns A comprehensive RelationshipMap mapping entity names to their Relationship objects.
|
|
43
|
+
*/
|
|
7
44
|
export declare const parseRelationships: (relationships: ApsorcRelationship[]) => RelationshipMap;
|
|
8
45
|
export declare const getRelationshipIdField: (relationship: Relationship) => string;
|
|
46
|
+
/**
|
|
47
|
+
* Transforms the parsed Relationship data into the final format needed by the entity/controller templates.
|
|
48
|
+
* Calculates additional helper properties like pluralized names and inverse property names.
|
|
49
|
+
* @param entityName The name of the entity for which to generate template data.
|
|
50
|
+
* @param relationships An array of Relationship objects for the given entity.
|
|
51
|
+
* @returns An array of RelationshipForTemplate objects with extra properties for the template.
|
|
52
|
+
*/
|
|
9
53
|
export declare const getRelationshipForTemplate: (entityName: string, relationships: Relationship[]) => RelationshipForTemplate[];
|
|
10
54
|
export declare const getNestedRelationships: (entityName: string, relationshipMap: RelationshipMap, prefix?: string, visited?: Set<string>) => string[];
|
|
11
55
|
export declare const getRelationshipsForImport: (entityName: string, relationships?: Relationship[]) => string[];
|
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getRelationshipsForImport = exports.getNestedRelationships = exports.getRelationshipForTemplate = exports.getRelationshipIdField = exports.parseRelationships = exports.parseRelationship = exports.
|
|
3
|
+
exports.getRelationshipsForImport = exports.getNestedRelationships = exports.getRelationshipForTemplate = exports.getRelationshipIdField = exports.parseRelationships = exports.parseRelationship = exports.parseManyToMany = exports.parseManytoOne = exports.parseOneToMany = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const pluralize_1 = tslib_1.__importDefault(require("pluralize"));
|
|
6
6
|
const casing_1 = require("../../utils/casing");
|
|
7
|
+
/**
|
|
8
|
+
* Parses a OneToMany relationship definition.
|
|
9
|
+
* These are treated as bidirectional from the .apsorc definition.
|
|
10
|
+
* Only generates the configuration for the 'from' side.
|
|
11
|
+
* @param relationship The .apsorc relationship definition.
|
|
12
|
+
* @returns A RelationshipMap containing only the entry for the 'from' side.
|
|
13
|
+
*/
|
|
7
14
|
const parseOneToMany = (relationship) => {
|
|
8
15
|
return {
|
|
9
16
|
[relationship.from]: [
|
|
@@ -26,6 +33,13 @@ const parseOneToMany = (relationship) => {
|
|
|
26
33
|
};
|
|
27
34
|
};
|
|
28
35
|
exports.parseOneToMany = parseOneToMany;
|
|
36
|
+
/**
|
|
37
|
+
* Parses a ManyToOne relationship definition.
|
|
38
|
+
* These are treated as unidirectional from the .apsorc definition.
|
|
39
|
+
* Only generates the configuration for the 'from' side.
|
|
40
|
+
* @param relationship The .apsorc relationship definition.
|
|
41
|
+
* @returns A RelationshipMap containing only the entry for the 'from' side.
|
|
42
|
+
*/
|
|
29
43
|
const parseManytoOne = (relationship) => {
|
|
30
44
|
return {
|
|
31
45
|
[relationship.from]: [
|
|
@@ -40,12 +54,23 @@ const parseManytoOne = (relationship) => {
|
|
|
40
54
|
};
|
|
41
55
|
};
|
|
42
56
|
exports.parseManytoOne = parseManytoOne;
|
|
57
|
+
/**
|
|
58
|
+
* Parses a ManyToMany relationship definition.
|
|
59
|
+
* Determines bidirectionality based on flags and inverse definitions.
|
|
60
|
+
* Determines the owning side (which gets the @JoinTable decorator).
|
|
61
|
+
* Calculates reference names (property names), defaulting if 'to_name' is missing.
|
|
62
|
+
* If bidirectional, generates configuration for both 'from' and 'to' sides.
|
|
63
|
+
* @param relationship The .apsorc relationship definition.
|
|
64
|
+
* @param allRelationships The full list of relationship definitions for finding inverses.
|
|
65
|
+
* @returns A RelationshipMap containing entries for one or both sides.
|
|
66
|
+
*/
|
|
43
67
|
const parseManyToMany = (relationship, allRelationships) => {
|
|
44
68
|
// Find the inverse relationship definition (if exists)
|
|
45
69
|
const inverseDefinition = allRelationships.find(def => def.from === relationship.to &&
|
|
46
70
|
def.to === relationship.from &&
|
|
47
|
-
def.type === 'ManyToMany'
|
|
48
|
-
|
|
71
|
+
def.type === 'ManyToMany');
|
|
72
|
+
// Determine if the relationship should be treated as bidirectional
|
|
73
|
+
const isEffectivelyBiDirectional = Boolean(relationship.bi_directional || (inverseDefinition === null || inverseDefinition === void 0 ? void 0 : inverseDefinition.bi_directional));
|
|
49
74
|
// For self-referencing relationships, find the matching inverse by to_name
|
|
50
75
|
const isSelfReferencing = relationship.from === relationship.to;
|
|
51
76
|
let selfRefInverse = null;
|
|
@@ -57,8 +82,20 @@ const parseManyToMany = (relationship, allRelationships) => {
|
|
|
57
82
|
def.bi_directional &&
|
|
58
83
|
def.to_name !== relationship.to_name);
|
|
59
84
|
}
|
|
60
|
-
let isOwningSide =
|
|
61
|
-
|
|
85
|
+
let isOwningSide = relationship.joinTableName || relationship.joinColumnName || relationship.inverseJoinColumnName
|
|
86
|
+
? true
|
|
87
|
+
: undefined;
|
|
88
|
+
if (isOwningSide === undefined && inverseDefinition) {
|
|
89
|
+
// If current doesn't have details, check if inverse does
|
|
90
|
+
isOwningSide =
|
|
91
|
+
!(inverseDefinition.joinTableName || inverseDefinition.joinColumnName || inverseDefinition.inverseJoinColumnName);
|
|
92
|
+
}
|
|
93
|
+
if (isOwningSide === undefined) {
|
|
94
|
+
// If neither has details, default to the current definition being the owner
|
|
95
|
+
isOwningSide = true;
|
|
96
|
+
}
|
|
97
|
+
// Default currentPropertyName if to_name is not provided
|
|
98
|
+
const currentPropertyName = relationship.to_name || (0, pluralize_1.default)((0, casing_1.camelCase)(relationship.to));
|
|
62
99
|
let inversePropertyName = inverseDefinition === null || inverseDefinition === void 0 ? void 0 : inverseDefinition.to_name;
|
|
63
100
|
// For self-referencing pairs, store the other to_name as inverseReferenceName
|
|
64
101
|
if (isSelfReferencing && selfRefInverse) {
|
|
@@ -68,95 +105,181 @@ const parseManyToMany = (relationship, allRelationships) => {
|
|
|
68
105
|
console.warn(`[APSO WARNING] Missing 'to_name' for ManyToMany: ${relationship.from} -> ${relationship.to}. Skipping this definition.`);
|
|
69
106
|
return {};
|
|
70
107
|
}
|
|
71
|
-
if
|
|
72
|
-
|
|
108
|
+
// Check for missing inverse 'to_name' only if it's effectively bidirectional
|
|
109
|
+
if (isEffectivelyBiDirectional && !inversePropertyName) {
|
|
110
|
+
console.info(`[APSO INFO] No 'to_name' provided for bidirectional ManyToMany: ${relationship.to} -> ${relationship.from}. Using default inverse property name. Add 'to_name' for explicit control.`);
|
|
111
|
+
// Default inverse property name if missing (using calculateInversePropertyName logic later)
|
|
73
112
|
}
|
|
113
|
+
// Calculate reference names, applying defaults if needed
|
|
114
|
+
const fromSideRefName = relationship.to_name || (relationship.to.toLowerCase() + 's');
|
|
115
|
+
const toSideRefName = (inverseDefinition === null || inverseDefinition === void 0 ? void 0 : inverseDefinition.to_name) || (relationship.from.toLowerCase() + 's');
|
|
74
116
|
let joinTableName = relationship.joinTableName;
|
|
75
|
-
let joinColumnName = relationship.joinColumnName;
|
|
76
|
-
let inverseJoinColumnName = relationship.inverseJoinColumnName;
|
|
77
117
|
// For self-referencing, ensure both sides get join=true and the same custom names
|
|
78
118
|
if (isSelfReferencing && relationship.bi_directional) {
|
|
79
119
|
isOwningSide = true; // Both sides get @JoinTable for self-ref
|
|
80
120
|
// If current def doesn't have names, but inverse does, grab them
|
|
81
121
|
if (!joinTableName && (selfRefInverse === null || selfRefInverse === void 0 ? void 0 : selfRefInverse.joinTableName)) {
|
|
82
122
|
joinTableName = selfRefInverse.joinTableName;
|
|
83
|
-
joinColumnName = selfRefInverse.joinColumnName;
|
|
84
|
-
inverseJoinColumnName = selfRefInverse.inverseJoinColumnName;
|
|
85
123
|
}
|
|
86
124
|
}
|
|
87
125
|
const fromSideRelationship = {
|
|
88
126
|
name: relationship.to,
|
|
89
127
|
type: "ManyToMany",
|
|
90
|
-
referenceName:
|
|
128
|
+
referenceName: fromSideRefName,
|
|
91
129
|
join: isOwningSide,
|
|
92
|
-
biDirectional:
|
|
93
|
-
joinTableName: joinTableName,
|
|
94
|
-
joinColumnName: joinColumnName,
|
|
95
|
-
inverseJoinColumnName: inverseJoinColumnName,
|
|
96
|
-
inverseReferenceName:
|
|
130
|
+
biDirectional: isEffectivelyBiDirectional,
|
|
131
|
+
joinTableName: isOwningSide ? (relationship.joinTableName || undefined) : undefined,
|
|
132
|
+
joinColumnName: isOwningSide ? (relationship.joinColumnName || undefined) : undefined,
|
|
133
|
+
inverseJoinColumnName: isOwningSide ? (relationship.inverseJoinColumnName || undefined) : undefined,
|
|
134
|
+
inverseReferenceName: isEffectivelyBiDirectional ? toSideRefName : undefined, // Only add inverse name if bidirectional
|
|
97
135
|
};
|
|
98
136
|
const response = {
|
|
99
137
|
[relationship.from]: [fromSideRelationship],
|
|
100
138
|
};
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
139
|
+
// If bidirectional, add the inverse relationship definition to the map
|
|
140
|
+
if (isEffectivelyBiDirectional) {
|
|
141
|
+
// Calculate the property name for the inverse side
|
|
142
|
+
// If inversePropertyName (from inverse to_name) exists, use it, else default based on 'from' entity
|
|
143
|
+
// const inverseSideReferenceName = inversePropertyName || pluralize(camelCase(relationship.from)); << Old way
|
|
144
|
+
// Determine if the inverse side is the owning side
|
|
145
|
+
const isInverseOwningSide = !isOwningSide;
|
|
146
|
+
const toSideRelationship = {
|
|
147
|
+
name: relationship.from,
|
|
148
|
+
type: "ManyToMany",
|
|
149
|
+
referenceName: toSideRefName,
|
|
150
|
+
join: isInverseOwningSide,
|
|
151
|
+
biDirectional: true,
|
|
152
|
+
// Add join details ONLY if the inverse side IS the owner
|
|
153
|
+
joinTableName: isInverseOwningSide ? ((inverseDefinition === null || inverseDefinition === void 0 ? void 0 : inverseDefinition.joinTableName) || undefined) : undefined,
|
|
154
|
+
joinColumnName: isInverseOwningSide ? ((inverseDefinition === null || inverseDefinition === void 0 ? void 0 : inverseDefinition.joinColumnName) || undefined) : undefined,
|
|
155
|
+
inverseJoinColumnName: isInverseOwningSide ? ((inverseDefinition === null || inverseDefinition === void 0 ? void 0 : inverseDefinition.inverseJoinColumnName) || undefined) : undefined,
|
|
156
|
+
inverseReferenceName: fromSideRefName, // The reference name of the other side
|
|
157
|
+
};
|
|
118
158
|
response[relationship.to] = [
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
type: "OneToOne",
|
|
122
|
-
biDirectional: true,
|
|
123
|
-
},
|
|
159
|
+
...(response[relationship.to] || []),
|
|
160
|
+
toSideRelationship
|
|
124
161
|
];
|
|
125
162
|
}
|
|
126
163
|
return response;
|
|
127
164
|
};
|
|
128
|
-
exports.
|
|
165
|
+
exports.parseManyToMany = parseManyToMany;
|
|
166
|
+
/**
|
|
167
|
+
* Helper function to create the Relationship object for the 'from' side of a OneToOne definition.
|
|
168
|
+
* Determines the owning side (@JoinColumn).
|
|
169
|
+
* @param relationship The .apsorc relationship definition.
|
|
170
|
+
* @param inverseDefinition The found inverse definition, if any.
|
|
171
|
+
* @returns The Relationship object for the 'from' side.
|
|
172
|
+
*/
|
|
173
|
+
const createFromOneToOneRelationship = (relationship, inverseDefinition) => {
|
|
174
|
+
// Determine owner side: if bidirectional, the side *without* to_name is owner.
|
|
175
|
+
// If not bidirectional, the 'from' side is always the owner.
|
|
176
|
+
const isOwner = relationship.bi_directional ? !relationship.to_name : true;
|
|
177
|
+
return {
|
|
178
|
+
name: relationship.to,
|
|
179
|
+
type: "OneToOne",
|
|
180
|
+
join: isOwner,
|
|
181
|
+
nullable: relationship.nullable || false,
|
|
182
|
+
referenceName: relationship.to_name || relationship.to.toLowerCase(),
|
|
183
|
+
biDirectional: relationship.bi_directional || false,
|
|
184
|
+
inverseReferenceName: (inverseDefinition === null || inverseDefinition === void 0 ? void 0 : inverseDefinition.to_name) || relationship.from.toLowerCase(),
|
|
185
|
+
};
|
|
186
|
+
};
|
|
187
|
+
/**
|
|
188
|
+
* Helper function to create the Relationship object for the 'to' side of a bidirectional OneToOne definition.
|
|
189
|
+
* @param relationship The original .apsorc relationship definition.
|
|
190
|
+
* @param inverseDefinition The inverse definition (assumed to exist).
|
|
191
|
+
* @param isOwner Indicates if the 'from' side was determined to be the owner.
|
|
192
|
+
* @returns The Relationship object for the 'to' side.
|
|
193
|
+
*/
|
|
194
|
+
const createToOneToOneRelationship = (relationship, inverseDefinition, // Assumed to exist if this is called
|
|
195
|
+
isOwner // Is the 'from' side the owner?
|
|
196
|
+
) => {
|
|
197
|
+
return {
|
|
198
|
+
name: relationship.from,
|
|
199
|
+
type: "OneToOne",
|
|
200
|
+
biDirectional: true,
|
|
201
|
+
join: !isOwner,
|
|
202
|
+
referenceName: inverseDefinition.to_name || relationship.from.toLowerCase(),
|
|
203
|
+
inverseReferenceName: relationship.to_name || relationship.to.toLowerCase(),
|
|
204
|
+
};
|
|
205
|
+
};
|
|
206
|
+
/**
|
|
207
|
+
* Parses a single relationship definition from .apsorc.
|
|
208
|
+
* Dispatches to the appropriate type-specific parser (parseManyToMany, parseOneToMany, etc.).
|
|
209
|
+
* For bidirectional relationships, the type-specific parsers handle generating data for both sides.
|
|
210
|
+
* @param relationship The single .apsorc relationship definition to parse.
|
|
211
|
+
* @param allRelationships The full list of relationship definitions (needed for context, e.g., finding inverses).
|
|
212
|
+
* @returns A RelationshipMap, potentially containing entries for both entities involved in the relationship.
|
|
213
|
+
*/
|
|
129
214
|
const parseRelationship = (relationship, allRelationships) => {
|
|
130
215
|
if (relationship.type === "ManyToMany") {
|
|
131
216
|
return (0, exports.parseManyToMany)(relationship, allRelationships);
|
|
132
217
|
}
|
|
133
218
|
switch (relationship.type) {
|
|
134
|
-
case "OneToMany":
|
|
219
|
+
case "OneToMany": {
|
|
220
|
+
const fromSideRefName = relationship.to_name || (relationship.to.toLowerCase() + 's');
|
|
221
|
+
const toSideRefName = relationship.from.toLowerCase(); // Default singular name for the inverse side
|
|
135
222
|
return {
|
|
136
|
-
[relationship.from]: [{ name: relationship.to, type: "OneToMany", biDirectional: true, referenceName:
|
|
137
|
-
[relationship.to]: [{ name: relationship.from, type: "ManyToOne", nullable: relationship.nullable || false, biDirectional: true, index: relationship.index || false, cascadeDelete: relationship.cascadeDelete || false }],
|
|
223
|
+
[relationship.from]: [{ name: relationship.to, type: "OneToMany", biDirectional: true, referenceName: fromSideRefName, inverseReferenceName: toSideRefName }],
|
|
224
|
+
[relationship.to]: [{ name: relationship.from, type: "ManyToOne", nullable: relationship.nullable || false, biDirectional: true, index: relationship.index || false, cascadeDelete: relationship.cascadeDelete || false, referenceName: toSideRefName, inverseReferenceName: fromSideRefName }],
|
|
138
225
|
};
|
|
226
|
+
}
|
|
139
227
|
case "ManyToOne":
|
|
140
228
|
return {
|
|
141
|
-
[relationship.from]: [{ name: relationship.to, type: "ManyToOne", referenceName: relationship.to_name ||
|
|
229
|
+
[relationship.from]: [{ name: relationship.to, type: "ManyToOne", referenceName: relationship.to_name || relationship.to.toLowerCase(), nullable: relationship.nullable || false, index: relationship.index || false }],
|
|
142
230
|
};
|
|
143
231
|
case "OneToOne": {
|
|
144
|
-
const inverseOnetoOneDef = allRelationships.find(def => def.from === relationship.to &&
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
232
|
+
const inverseOnetoOneDef = allRelationships.find((def) => def.from === relationship.to &&
|
|
233
|
+
def.to === relationship.from &&
|
|
234
|
+
def.type === "OneToOne");
|
|
235
|
+
// Determine bidirectionality based on current flag or inverse flag
|
|
236
|
+
const isEffectivelyBiDirectional = Boolean(relationship.bi_directional || (inverseOnetoOneDef === null || inverseOnetoOneDef === void 0 ? void 0 : inverseOnetoOneDef.bi_directional));
|
|
237
|
+
// Create the relationship for the 'from' side
|
|
238
|
+
const fromRel = createFromOneToOneRelationship(relationship, inverseOnetoOneDef);
|
|
239
|
+
const responseMap = { [relationship.from]: [fromRel] };
|
|
240
|
+
// If it's effectively bidirectional, create and add the relationship for the 'to' side
|
|
241
|
+
if (isEffectivelyBiDirectional) {
|
|
242
|
+
// Need the inverse definition to exist to correctly create the inverse side
|
|
243
|
+
if (inverseOnetoOneDef) {
|
|
244
|
+
const isFromSideOwner = fromRel.join || false; // Determine owner from the created 'from' side
|
|
245
|
+
const toRel = createToOneToOneRelationship(relationship, inverseOnetoOneDef, isFromSideOwner);
|
|
246
|
+
responseMap[relationship.to] = [
|
|
247
|
+
...(responseMap[relationship.to] || []),
|
|
248
|
+
toRel,
|
|
249
|
+
];
|
|
250
|
+
}
|
|
251
|
+
else {
|
|
252
|
+
// Handle the edge case where one side declares bidirectional but the inverse definition is missing
|
|
253
|
+
// We still need to add *something* for the inverse side, likely without join/owner info
|
|
254
|
+
console.warn(`[APSO WARNING] Bidirectional OneToOne defined for ${relationship.from} -> ${relationship.to}, but inverse definition ${relationship.to} -> ${relationship.from} is missing. Inverse side generation may be incomplete.`);
|
|
255
|
+
const fallbackToRel = {
|
|
256
|
+
name: relationship.from,
|
|
257
|
+
type: "OneToOne",
|
|
258
|
+
biDirectional: true,
|
|
259
|
+
join: !(fromRel.join || false),
|
|
260
|
+
referenceName: relationship.from.toLowerCase(),
|
|
261
|
+
inverseReferenceName: fromRel.referenceName || undefined // Reference the 'from' side's name, default to undefined if null
|
|
262
|
+
};
|
|
263
|
+
responseMap[relationship.to] = [
|
|
264
|
+
...(responseMap[relationship.to] || []),
|
|
265
|
+
fallbackToRel,
|
|
266
|
+
];
|
|
267
|
+
}
|
|
153
268
|
}
|
|
154
269
|
return responseMap;
|
|
155
270
|
}
|
|
156
271
|
}
|
|
272
|
+
// Fallback / Error case?
|
|
273
|
+
console.error(`[APSO ERROR] Unknown relationship type: ${relationship.type}`);
|
|
157
274
|
return {};
|
|
158
275
|
};
|
|
159
276
|
exports.parseRelationship = parseRelationship;
|
|
277
|
+
/**
|
|
278
|
+
* Parses the entire array of relationship definitions from the .apsorc file.
|
|
279
|
+
* Iterates through each definition, calls parseRelationship, and merges the results.
|
|
280
|
+
* @param relationships The array of relationship definitions from .apsorc.
|
|
281
|
+
* @returns A comprehensive RelationshipMap mapping entity names to their Relationship objects.
|
|
282
|
+
*/
|
|
160
283
|
const parseRelationships = (relationships) => {
|
|
161
284
|
const response = {};
|
|
162
285
|
for (const rel of relationships) {
|
|
@@ -180,6 +303,16 @@ const getRelationshipIdField = (relationship) => {
|
|
|
180
303
|
return `${getRelationshipName(relationship)}Id`;
|
|
181
304
|
};
|
|
182
305
|
exports.getRelationshipIdField = getRelationshipIdField;
|
|
306
|
+
/**
|
|
307
|
+
* Calculates the final property name on the inverse side of a relationship.
|
|
308
|
+
* Used by the entity template for the TypeORM decorator's inverse side function.
|
|
309
|
+
* Handles self-referencing cases and defaults.
|
|
310
|
+
* @param relationship The Relationship object for the current side.
|
|
311
|
+
* @param entityName The name of the entity for the current side.
|
|
312
|
+
* @param relationshipName The camelCased property name for the current side.
|
|
313
|
+
* @param needsPluralInverse Whether the inverse property name should be plural.
|
|
314
|
+
* @returns The calculated inverse property name.
|
|
315
|
+
*/
|
|
183
316
|
const calculateInversePropertyName = (relationship, entityName, relationshipName, needsPluralInverse) => {
|
|
184
317
|
const isSelfReferencing = relationship.name === entityName;
|
|
185
318
|
if (isSelfReferencing && relationship.type === 'ManyToMany') {
|
|
@@ -208,18 +341,56 @@ const calculateInversePropertyName = (relationship, entityName, relationshipName
|
|
|
208
341
|
const fallback = (0, casing_1.camelCase)(entityName);
|
|
209
342
|
return needsPluralInverse ? (0, pluralize_1.default)(fallback) : fallback;
|
|
210
343
|
};
|
|
344
|
+
/**
|
|
345
|
+
* Transforms the parsed Relationship data into the final format needed by the entity/controller templates.
|
|
346
|
+
* Calculates additional helper properties like pluralized names and inverse property names.
|
|
347
|
+
* @param entityName The name of the entity for which to generate template data.
|
|
348
|
+
* @param relationships An array of Relationship objects for the given entity.
|
|
349
|
+
* @returns An array of RelationshipForTemplate objects with extra properties for the template.
|
|
350
|
+
*/
|
|
211
351
|
const getRelationshipForTemplate = (entityName, relationships) => {
|
|
212
352
|
if (!relationships) {
|
|
213
353
|
return [];
|
|
214
354
|
}
|
|
215
|
-
|
|
355
|
+
// More comprehensive deduplication to handle all duplicate cases
|
|
356
|
+
const dedupedRelationships = [];
|
|
357
|
+
const processedRefs = new Map();
|
|
358
|
+
// First pass: Group by name and referenceName (more specific)
|
|
359
|
+
relationships.forEach((relationship) => {
|
|
360
|
+
var _a;
|
|
361
|
+
const name = relationship.name;
|
|
362
|
+
const refName = relationship.referenceName || "default";
|
|
363
|
+
const key = `${name}:${refName}`;
|
|
364
|
+
// Use the relationship with join=true if there are duplicates (this is the owning side)
|
|
365
|
+
if (!processedRefs.has(key) || (relationship.join && !((_a = processedRefs.get(key)) === null || _a === void 0 ? void 0 : _a.join))) {
|
|
366
|
+
processedRefs.set(key, relationship);
|
|
367
|
+
}
|
|
368
|
+
});
|
|
369
|
+
// Second pass: Use name-only as a fallback for edge cases
|
|
370
|
+
// This helps with ManyToMany cases where both sides exist with different join flags
|
|
371
|
+
const nameOnlyKeys = new Set();
|
|
372
|
+
processedRefs.forEach((rel) => {
|
|
373
|
+
if (!nameOnlyKeys.has(rel.name)) {
|
|
374
|
+
nameOnlyKeys.add(rel.name);
|
|
375
|
+
dedupedRelationships.push(rel);
|
|
376
|
+
}
|
|
377
|
+
else if (rel.join === true) {
|
|
378
|
+
// If we already have this name but the current one is the owner (join=true),
|
|
379
|
+
// replace the existing one
|
|
380
|
+
const existingIdx = dedupedRelationships.findIndex(r => r.name === rel.name);
|
|
381
|
+
if (existingIdx !== -1 && !dedupedRelationships[existingIdx].join) {
|
|
382
|
+
dedupedRelationships[existingIdx] = rel;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
});
|
|
386
|
+
return dedupedRelationships.map((relationship) => {
|
|
216
387
|
const thisReferenceName = relationship.referenceName;
|
|
217
388
|
const relationshipName = (0, casing_1.camelCase)(thisReferenceName || relationship.name);
|
|
218
389
|
const needsPluralInverse = relationship.type === 'ManyToOne' || relationship.type === 'ManyToMany';
|
|
219
390
|
const inverseSidePropertyName = calculateInversePropertyName(relationship, entityName, relationshipName, needsPluralInverse);
|
|
220
391
|
if (relationship.biDirectional && !relationship.inverseReferenceName &&
|
|
221
392
|
(relationship.type === 'ManyToMany' || relationship.type === 'OneToMany' || relationship.type === 'OneToOne')) {
|
|
222
|
-
console.
|
|
393
|
+
console.info(`[APSO INFO] Using default property name '${inverseSidePropertyName}' for ${relationship.name}->${entityName}. To customize, add 'to_name' in your relationship definition.`);
|
|
223
394
|
}
|
|
224
395
|
return {
|
|
225
396
|
...relationship,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|