@omnifyjp/ts 6.0.0 → 6.0.2
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.
|
@@ -712,19 +712,19 @@ function buildRelations(schemaName, properties, propertyOrder, modelNamespace, r
|
|
|
712
712
|
const target = prop['target'] ?? '';
|
|
713
713
|
const ns = target ? resolveRelatedModelNamespace(target, modelNamespace, config, reader) : modelNamespace;
|
|
714
714
|
const targetTableName = target ? reader.getTableName(target) : '';
|
|
715
|
-
// For ManyToMany, look up the explicit `kind: pivot` schema
|
|
716
|
-
//
|
|
717
|
-
//
|
|
715
|
+
// For ManyToMany, look up the explicit `kind: pivot` schema by pivotFor
|
|
716
|
+
// pair (user-authored filenames may predate the sorted-name convention)
|
|
717
|
+
// and pass its property names so belongsToMany
|
|
718
718
|
// emits `->withPivot('col1', 'col2')`. Without this, custom pivot
|
|
719
719
|
// columns the user added are unreachable through the relation.
|
|
720
720
|
const relation = prop['relation'] ?? '';
|
|
721
721
|
let pivotProperties;
|
|
722
722
|
let pivotTableName;
|
|
723
723
|
if (relation === 'ManyToMany' && target) {
|
|
724
|
-
const
|
|
725
|
-
const pivotSchema =
|
|
724
|
+
const pivotMatch = reader.findPivotSchema(schemaName, target);
|
|
725
|
+
const pivotSchema = pivotMatch?.schema;
|
|
726
726
|
const conventionalPivotTable = [toSnakeCase(schemaName), toSnakeCase(target)].sort().join('_');
|
|
727
|
-
const resolvedPivotTable =
|
|
727
|
+
const resolvedPivotTable = pivotMatch ? reader.getTableName(pivotMatch.name) : '';
|
|
728
728
|
if (resolvedPivotTable && resolvedPivotTable !== conventionalPivotTable) {
|
|
729
729
|
pivotTableName = resolvedPivotTable;
|
|
730
730
|
}
|
|
@@ -11,6 +11,13 @@ export declare class SchemaReader {
|
|
|
11
11
|
getRoot(): SchemasJson;
|
|
12
12
|
getSchemas(): Record<string, SchemaDefinition>;
|
|
13
13
|
getSchema(name: string): SchemaDefinition | undefined;
|
|
14
|
+
/** Find a pivot by its logical pair, independent of the pivot schema's
|
|
15
|
+
* filename/name. User-authored pivots predate the sorted `<A><B>` naming
|
|
16
|
+
* convention and may be called `CouponBranch` for `[Coupon, Branch]`. */
|
|
17
|
+
findPivotSchema(a: string, b: string): {
|
|
18
|
+
name: string;
|
|
19
|
+
schema: SchemaDefinition;
|
|
20
|
+
} | undefined;
|
|
14
21
|
getObjectSchemas(): Record<string, SchemaDefinition>;
|
|
15
22
|
getVisibleObjectSchemas(): Record<string, SchemaDefinition>;
|
|
16
23
|
getEnumSchemas(): Record<string, SchemaDefinition>;
|
|
@@ -23,6 +23,20 @@ export class SchemaReader {
|
|
|
23
23
|
getSchema(name) {
|
|
24
24
|
return this.data.schemas?.[name];
|
|
25
25
|
}
|
|
26
|
+
/** Find a pivot by its logical pair, independent of the pivot schema's
|
|
27
|
+
* filename/name. User-authored pivots predate the sorted `<A><B>` naming
|
|
28
|
+
* convention and may be called `CouponBranch` for `[Coupon, Branch]`. */
|
|
29
|
+
findPivotSchema(a, b) {
|
|
30
|
+
const wanted = [a, b].sort().join(':');
|
|
31
|
+
for (const [name, schema] of Object.entries(this.getSchemas())) {
|
|
32
|
+
if (schema.kind !== 'pivot' || schema.pivotFor?.length !== 2)
|
|
33
|
+
continue;
|
|
34
|
+
if ([...schema.pivotFor].sort().join(':') === wanted) {
|
|
35
|
+
return { name, schema };
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
26
40
|
getObjectSchemas() {
|
|
27
41
|
const result = {};
|
|
28
42
|
for (const [name, schema] of Object.entries(this.getSchemas())) {
|