@omnifyjp/ts 5.9.21 → 6.0.1

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.
@@ -719,16 +719,22 @@ function buildRelations(schemaName, properties, propertyOrder, modelNamespace, r
719
719
  // columns the user added are unreachable through the relation.
720
720
  const relation = prop['relation'] ?? '';
721
721
  let pivotProperties;
722
+ let pivotTableName;
722
723
  if (relation === 'ManyToMany' && target) {
723
724
  const pivotName = [schemaName, target].sort().join('');
724
725
  const pivotSchema = reader.getSchema(pivotName);
726
+ const conventionalPivotTable = [toSnakeCase(schemaName), toSnakeCase(target)].sort().join('_');
727
+ const resolvedPivotTable = pivotSchema ? reader.getTableName(pivotName) : '';
728
+ if (resolvedPivotTable && resolvedPivotTable !== conventionalPivotTable) {
729
+ pivotTableName = resolvedPivotTable;
730
+ }
725
731
  if (pivotSchema?.properties) {
726
732
  pivotProperties = Object.entries(pivotSchema.properties).map(([propName, prop]) => prop.type === 'Association' && !prop.mappedBy
727
733
  ? prop.column || toFkColumnName(propName)
728
734
  : toSnakeCase(propName));
729
735
  }
730
736
  }
731
- const result = buildRelation(propName, prop, ns, { sourceTableName, targetTableName, pivotProperties });
737
+ const result = buildRelation(propName, prop, ns, { sourceTableName, targetTableName, pivotProperties, pivotTableName });
732
738
  if (result) {
733
739
  methods.push('\n' + result.method);
734
740
  declaredMethods.add(toCamelCase(propName));
@@ -8,6 +8,9 @@ interface RelationResult {
8
8
  interface RelationContext {
9
9
  sourceTableName: string;
10
10
  targetTableName: string;
11
+ /** Non-conventional physical table resolved from an explicit pivot schema.
12
+ * Omitted when Laravel can infer the joining table from model basenames. */
13
+ pivotTableName?: string;
11
14
  /** Property names declared on the explicit `kind: pivot` schema for
12
15
  * this M2M (e.g. `["position", "notes"]`). Surfaced in the generated
13
16
  * `belongsToMany()` chain via `->withPivot(...)` so consumers can
@@ -17,8 +20,8 @@ interface RelationContext {
17
20
  }
18
21
  /** Build Eloquent relation method PHP code. */
19
22
  export declare function buildRelation(propName: string, property: Record<string, unknown>, modelNamespace: string, context?: RelationContext): RelationResult | null;
20
- /** Generate pivot table name matching Go's GeneratePivotTableName logic. */
21
- export declare function generatePivotTableName(sourceTable: string, targetTable: string): string;
23
+ /** Generate the table name Laravel's joiningTable() infers from model basenames. */
24
+ export declare function generatePivotTableName(sourceModel: string, targetModel: string): string;
22
25
  /** Get the return type hint for a relation. */
23
26
  export declare function getReturnType(relation: string): string;
24
27
  /**
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Port of RelationBuilder.php — generates Eloquent relation methods.
3
3
  */
4
- import { toSnakeCase, toCamelCase, toFkColumnName, singularize } from './naming-helper.js';
4
+ import { toSnakeCase, toCamelCase, toFkColumnName } from './naming-helper.js';
5
5
  /** Build Eloquent relation method PHP code. */
6
6
  export function buildRelation(propName, property, modelNamespace, context) {
7
7
  const relation = property['relation'] ?? '';
@@ -41,10 +41,9 @@ export function buildRelation(propName, property, modelNamespace, context) {
41
41
  return null;
42
42
  return { method, imports };
43
43
  }
44
- /** Generate pivot table name matching Go's GeneratePivotTableName logic. */
45
- export function generatePivotTableName(sourceTable, targetTable) {
46
- const tables = [singularize(sourceTable), singularize(targetTable)].sort();
47
- return tables.join('_') + '_pivot';
44
+ /** Generate the table name Laravel's joiningTable() infers from model basenames. */
45
+ export function generatePivotTableName(sourceModel, targetModel) {
46
+ return [toSnakeCase(sourceModel), toSnakeCase(targetModel)].sort().join('_');
48
47
  }
49
48
  /** Get the return type hint for a relation. */
50
49
  export function getReturnType(relation) {
@@ -122,13 +121,12 @@ export function formatOrderByChain(orderBy) {
122
121
  .join('');
123
122
  }
124
123
  function belongsToMany(propName, property, target, ns, context) {
125
- let joinTable = property['joinTable'] ?? '';
126
- if (!joinTable && context?.sourceTableName && context?.targetTableName) {
127
- joinTable = generatePivotTableName(context.sourceTableName, context.targetTableName);
128
- }
124
+ const joinTable = property['joinTable'] || context?.pivotTableName || '';
129
125
  const methodName = toCamelCase(propName);
130
126
  const fqcn = `\\${ns}\\${target}`;
131
- let chain = `$this->belongsToMany(${fqcn}::class, '${joinTable}')`;
127
+ let chain = joinTable
128
+ ? `$this->belongsToMany(${fqcn}::class, '${joinTable}')`
129
+ : `$this->belongsToMany(${fqcn}::class)`;
132
130
  // withPivot for every column declared on the explicit `kind: pivot`
133
131
  // schema (resolved upstream and passed via context.pivotProperties).
134
132
  // The legacy `pivotFields:` block on the M2M property was removed in
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omnifyjp/ts",
3
- "version": "5.9.21",
3
+ "version": "6.0.1",
4
4
  "description": "TypeScript model type generator from Omnify schemas.json",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",