@famgia/omnify-laravel 0.0.105 → 0.0.107

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/index.d.cts CHANGED
@@ -198,6 +198,77 @@ declare function formatForeignKey(fk: ForeignKeyDefinition): string;
198
198
  * Formats an index to PHP code.
199
199
  */
200
200
  declare function formatIndex(index: IndexDefinition): string;
201
+ /**
202
+ * Pivot field definition (simplified from PropertyDefinition for pivot tables).
203
+ */
204
+ interface PivotFieldInfo {
205
+ /** Field name in snake_case */
206
+ name: string;
207
+ /** Property type (String, Int, Boolean, Timestamp, etc.) */
208
+ type: string;
209
+ /** Whether the field can be null */
210
+ nullable?: boolean;
211
+ /** Default value for the field */
212
+ default?: unknown;
213
+ /** String length (for String type) */
214
+ length?: number;
215
+ /** Whether the field is unsigned (for numeric types) */
216
+ unsigned?: boolean;
217
+ }
218
+ /**
219
+ * Pivot table information for ManyToMany relationships.
220
+ */
221
+ interface PivotTableInfo {
222
+ tableName: string;
223
+ sourceTable: string;
224
+ targetTable: string;
225
+ sourceColumn: string;
226
+ targetColumn: string;
227
+ sourcePkType: 'Int' | 'BigInt' | 'Uuid' | 'String';
228
+ targetPkType: 'Int' | 'BigInt' | 'Uuid' | 'String';
229
+ onDelete: string | undefined;
230
+ onUpdate: string | undefined;
231
+ /** Additional fields on the pivot table */
232
+ pivotFields?: PivotFieldInfo[];
233
+ }
234
+ /**
235
+ * Polymorphic pivot table information for MorphToMany relationships.
236
+ */
237
+ interface MorphToManyPivotInfo {
238
+ tableName: string;
239
+ /** The fixed target schema that uses MorphToMany */
240
+ targetTable: string;
241
+ targetColumn: string;
242
+ targetPkType: 'Int' | 'BigInt' | 'Uuid' | 'String';
243
+ /** Base name for polymorphic columns (creates {name}_type and {name}_id) */
244
+ morphName: string;
245
+ /** Schema names that can be morphed to */
246
+ morphTargets: readonly string[];
247
+ onDelete: string | undefined;
248
+ onUpdate: string | undefined;
249
+ }
250
+ /**
251
+ * Generates pivot table name for ManyToMany relationship.
252
+ * Uses alphabetical ordering for consistency.
253
+ */
254
+ declare function generatePivotTableName(sourceTable: string, targetTable: string, customName?: string): string;
255
+ /**
256
+ * Extracts ManyToMany relationships from a schema.
257
+ */
258
+ declare function extractManyToManyRelations(schema: LoadedSchema, allSchemas: SchemaCollection): PivotTableInfo[];
259
+ /**
260
+ * Generates blueprint for a pivot table.
261
+ */
262
+ declare function generatePivotTableBlueprint(pivot: PivotTableInfo): TableBlueprint;
263
+ /**
264
+ * Extracts MorphToMany relationships from a schema.
265
+ * MorphToMany creates a pivot table with polymorphic type/id columns.
266
+ */
267
+ declare function extractMorphToManyRelations(schema: LoadedSchema, allSchemas: SchemaCollection): MorphToManyPivotInfo[];
268
+ /**
269
+ * Generates blueprint for a polymorphic pivot table (MorphToMany).
270
+ */
271
+ declare function generateMorphToManyPivotBlueprint(pivot: MorphToManyPivotInfo): TableBlueprint;
201
272
 
202
273
  /**
203
274
  * @famgia/omnify-laravel - Migration Generator
@@ -429,4 +500,4 @@ declare function generateAIGuides(rootDir: string, options?: AIGuidesOptions): A
429
500
  */
430
501
  declare function shouldGenerateAIGuides(rootDir: string): boolean;
431
502
 
432
- export { type AIGuidesOptions, type AIGuidesResult, type ColumnMethod, type ColumnModifier, type FactoryGeneratorOptions, type ForeignKeyDefinition, type GeneratedFactory, type GeneratedModel, type IndexDefinition, type MigrationDefinition, type MigrationFile, type MigrationOperation, type MigrationOptions, type ModelGeneratorOptions, type ProviderRegistrationResult, type TableBlueprint, formatColumnMethod, formatForeignKey, formatIndex, formatMigrationFile, generateAIGuides, generateAlterMigration, generateDropMigrationForTable, generateDropTableMigration, generateFactories, generateForeignKey, generateMigrationFromSchema, generateMigrations, generateMigrationsFromChanges, generateModels, generatePrimaryKeyColumn, generateProviderRegistration, generateSoftDeleteColumn, generateTimestampColumns, getFactoryPath, getMigrationPath, getModelPath, propertyToColumnMethod, schemaToBlueprint, shouldGenerateAIGuides, toColumnName, toTableName };
503
+ export { type AIGuidesOptions, type AIGuidesResult, type ColumnMethod, type ColumnModifier, type FactoryGeneratorOptions, type ForeignKeyDefinition, type GeneratedFactory, type GeneratedModel, type IndexDefinition, type MigrationDefinition, type MigrationFile, type MigrationOperation, type MigrationOptions, type ModelGeneratorOptions, type MorphToManyPivotInfo, type PivotFieldInfo, type PivotTableInfo, type ProviderRegistrationResult, type TableBlueprint, extractManyToManyRelations, extractMorphToManyRelations, formatColumnMethod, formatForeignKey, formatIndex, formatMigrationFile, generateAIGuides, generateAlterMigration, generateDropMigrationForTable, generateDropTableMigration, generateFactories, generateForeignKey, generateMigrationFromSchema, generateMigrations, generateMigrationsFromChanges, generateModels, generateMorphToManyPivotBlueprint, generatePivotTableBlueprint, generatePivotTableName, generatePrimaryKeyColumn, generateProviderRegistration, generateSoftDeleteColumn, generateTimestampColumns, getFactoryPath, getMigrationPath, getModelPath, propertyToColumnMethod, schemaToBlueprint, shouldGenerateAIGuides, toColumnName, toTableName };
package/dist/index.d.ts CHANGED
@@ -198,6 +198,77 @@ declare function formatForeignKey(fk: ForeignKeyDefinition): string;
198
198
  * Formats an index to PHP code.
199
199
  */
200
200
  declare function formatIndex(index: IndexDefinition): string;
201
+ /**
202
+ * Pivot field definition (simplified from PropertyDefinition for pivot tables).
203
+ */
204
+ interface PivotFieldInfo {
205
+ /** Field name in snake_case */
206
+ name: string;
207
+ /** Property type (String, Int, Boolean, Timestamp, etc.) */
208
+ type: string;
209
+ /** Whether the field can be null */
210
+ nullable?: boolean;
211
+ /** Default value for the field */
212
+ default?: unknown;
213
+ /** String length (for String type) */
214
+ length?: number;
215
+ /** Whether the field is unsigned (for numeric types) */
216
+ unsigned?: boolean;
217
+ }
218
+ /**
219
+ * Pivot table information for ManyToMany relationships.
220
+ */
221
+ interface PivotTableInfo {
222
+ tableName: string;
223
+ sourceTable: string;
224
+ targetTable: string;
225
+ sourceColumn: string;
226
+ targetColumn: string;
227
+ sourcePkType: 'Int' | 'BigInt' | 'Uuid' | 'String';
228
+ targetPkType: 'Int' | 'BigInt' | 'Uuid' | 'String';
229
+ onDelete: string | undefined;
230
+ onUpdate: string | undefined;
231
+ /** Additional fields on the pivot table */
232
+ pivotFields?: PivotFieldInfo[];
233
+ }
234
+ /**
235
+ * Polymorphic pivot table information for MorphToMany relationships.
236
+ */
237
+ interface MorphToManyPivotInfo {
238
+ tableName: string;
239
+ /** The fixed target schema that uses MorphToMany */
240
+ targetTable: string;
241
+ targetColumn: string;
242
+ targetPkType: 'Int' | 'BigInt' | 'Uuid' | 'String';
243
+ /** Base name for polymorphic columns (creates {name}_type and {name}_id) */
244
+ morphName: string;
245
+ /** Schema names that can be morphed to */
246
+ morphTargets: readonly string[];
247
+ onDelete: string | undefined;
248
+ onUpdate: string | undefined;
249
+ }
250
+ /**
251
+ * Generates pivot table name for ManyToMany relationship.
252
+ * Uses alphabetical ordering for consistency.
253
+ */
254
+ declare function generatePivotTableName(sourceTable: string, targetTable: string, customName?: string): string;
255
+ /**
256
+ * Extracts ManyToMany relationships from a schema.
257
+ */
258
+ declare function extractManyToManyRelations(schema: LoadedSchema, allSchemas: SchemaCollection): PivotTableInfo[];
259
+ /**
260
+ * Generates blueprint for a pivot table.
261
+ */
262
+ declare function generatePivotTableBlueprint(pivot: PivotTableInfo): TableBlueprint;
263
+ /**
264
+ * Extracts MorphToMany relationships from a schema.
265
+ * MorphToMany creates a pivot table with polymorphic type/id columns.
266
+ */
267
+ declare function extractMorphToManyRelations(schema: LoadedSchema, allSchemas: SchemaCollection): MorphToManyPivotInfo[];
268
+ /**
269
+ * Generates blueprint for a polymorphic pivot table (MorphToMany).
270
+ */
271
+ declare function generateMorphToManyPivotBlueprint(pivot: MorphToManyPivotInfo): TableBlueprint;
201
272
 
202
273
  /**
203
274
  * @famgia/omnify-laravel - Migration Generator
@@ -429,4 +500,4 @@ declare function generateAIGuides(rootDir: string, options?: AIGuidesOptions): A
429
500
  */
430
501
  declare function shouldGenerateAIGuides(rootDir: string): boolean;
431
502
 
432
- export { type AIGuidesOptions, type AIGuidesResult, type ColumnMethod, type ColumnModifier, type FactoryGeneratorOptions, type ForeignKeyDefinition, type GeneratedFactory, type GeneratedModel, type IndexDefinition, type MigrationDefinition, type MigrationFile, type MigrationOperation, type MigrationOptions, type ModelGeneratorOptions, type ProviderRegistrationResult, type TableBlueprint, formatColumnMethod, formatForeignKey, formatIndex, formatMigrationFile, generateAIGuides, generateAlterMigration, generateDropMigrationForTable, generateDropTableMigration, generateFactories, generateForeignKey, generateMigrationFromSchema, generateMigrations, generateMigrationsFromChanges, generateModels, generatePrimaryKeyColumn, generateProviderRegistration, generateSoftDeleteColumn, generateTimestampColumns, getFactoryPath, getMigrationPath, getModelPath, propertyToColumnMethod, schemaToBlueprint, shouldGenerateAIGuides, toColumnName, toTableName };
503
+ export { type AIGuidesOptions, type AIGuidesResult, type ColumnMethod, type ColumnModifier, type FactoryGeneratorOptions, type ForeignKeyDefinition, type GeneratedFactory, type GeneratedModel, type IndexDefinition, type MigrationDefinition, type MigrationFile, type MigrationOperation, type MigrationOptions, type ModelGeneratorOptions, type MorphToManyPivotInfo, type PivotFieldInfo, type PivotTableInfo, type ProviderRegistrationResult, type TableBlueprint, extractManyToManyRelations, extractMorphToManyRelations, formatColumnMethod, formatForeignKey, formatIndex, formatMigrationFile, generateAIGuides, generateAlterMigration, generateDropMigrationForTable, generateDropTableMigration, generateFactories, generateForeignKey, generateMigrationFromSchema, generateMigrations, generateMigrationsFromChanges, generateModels, generateMorphToManyPivotBlueprint, generatePivotTableBlueprint, generatePivotTableName, generatePrimaryKeyColumn, generateProviderRegistration, generateSoftDeleteColumn, generateTimestampColumns, getFactoryPath, getMigrationPath, getModelPath, propertyToColumnMethod, schemaToBlueprint, shouldGenerateAIGuides, toColumnName, toTableName };
package/dist/index.js CHANGED
@@ -1,4 +1,6 @@
1
1
  import {
2
+ extractManyToManyRelations,
3
+ extractMorphToManyRelations,
2
4
  formatColumnMethod,
3
5
  formatForeignKey,
4
6
  formatIndex,
@@ -13,6 +15,9 @@ import {
13
15
  generateMigrations,
14
16
  generateMigrationsFromChanges,
15
17
  generateModels,
18
+ generateMorphToManyPivotBlueprint,
19
+ generatePivotTableBlueprint,
20
+ generatePivotTableName,
16
21
  generatePrimaryKeyColumn,
17
22
  generateProviderRegistration,
18
23
  generateSoftDeleteColumn,
@@ -26,8 +31,10 @@ import {
26
31
  shouldGenerateAIGuides,
27
32
  toColumnName,
28
33
  toTableName
29
- } from "./chunk-XR2DTIIS.js";
34
+ } from "./chunk-7YHLXBF5.js";
30
35
  export {
36
+ extractManyToManyRelations,
37
+ extractMorphToManyRelations,
31
38
  formatColumnMethod,
32
39
  formatForeignKey,
33
40
  formatIndex,
@@ -42,6 +49,9 @@ export {
42
49
  generateMigrations,
43
50
  generateMigrationsFromChanges,
44
51
  generateModels,
52
+ generateMorphToManyPivotBlueprint,
53
+ generatePivotTableBlueprint,
54
+ generatePivotTableName,
45
55
  generatePrimaryKeyColumn,
46
56
  generateProviderRegistration,
47
57
  generateSoftDeleteColumn,
package/dist/plugin.cjs CHANGED
@@ -581,6 +581,9 @@ function extractManyToManyRelations(schema, allSchemas) {
581
581
  if (assocProp.relation !== "ManyToMany") {
582
582
  continue;
583
583
  }
584
+ if (assocProp.mappedBy) {
585
+ continue;
586
+ }
584
587
  const targetName = assocProp.target;
585
588
  if (!targetName) {
586
589
  continue;
@@ -595,6 +598,19 @@ function extractManyToManyRelations(schema, allSchemas) {
595
598
  const pivotTableName = generatePivotTableName(sourceTable, targetTable, assocProp.joinTable);
596
599
  const sourceColumn = sourceTable.replace(/ies$/, "y").replace(/s$/, "") + "_id";
597
600
  const targetColumn = targetTable.replace(/ies$/, "y").replace(/s$/, "") + "_id";
601
+ const pivotFields = [];
602
+ if (assocProp.pivotFields) {
603
+ for (const [fieldName, fieldDef] of Object.entries(assocProp.pivotFields)) {
604
+ pivotFields.push({
605
+ name: toColumnName(fieldName),
606
+ type: fieldDef.type,
607
+ nullable: fieldDef.nullable,
608
+ default: fieldDef.default,
609
+ length: fieldDef.length,
610
+ unsigned: fieldDef.unsigned
611
+ });
612
+ }
613
+ }
598
614
  pivotTables.push({
599
615
  tableName: pivotTableName,
600
616
  sourceTable,
@@ -604,11 +620,35 @@ function extractManyToManyRelations(schema, allSchemas) {
604
620
  sourcePkType,
605
621
  targetPkType,
606
622
  onDelete: assocProp.onDelete,
607
- onUpdate: assocProp.onUpdate
623
+ onUpdate: assocProp.onUpdate,
624
+ pivotFields: pivotFields.length > 0 ? pivotFields : void 0
608
625
  });
609
626
  }
610
627
  return pivotTables;
611
628
  }
629
+ function pivotFieldToColumn(field) {
630
+ const method = TYPE_METHOD_MAP[field.type] ?? "string";
631
+ const args = [field.name];
632
+ const modifiers = [];
633
+ if (method === "string" && field.length) {
634
+ args.push(field.length);
635
+ }
636
+ if (field.nullable) {
637
+ modifiers.push({ method: "nullable" });
638
+ }
639
+ if (field.default !== void 0 && field.default !== null) {
640
+ modifiers.push({ method: "default", args: [field.default] });
641
+ }
642
+ if (field.unsigned && (method === "tinyInteger" || method === "integer" || method === "bigInteger")) {
643
+ modifiers.push({ method: "unsigned" });
644
+ }
645
+ return {
646
+ name: field.name,
647
+ method,
648
+ args,
649
+ modifiers
650
+ };
651
+ }
612
652
  function generatePivotTableBlueprint(pivot) {
613
653
  const columns = [];
614
654
  const foreignKeys = [];
@@ -637,6 +677,11 @@ function generatePivotTableBlueprint(pivot) {
637
677
  args: [pivot.targetColumn],
638
678
  modifiers: []
639
679
  });
680
+ if (pivot.pivotFields && pivot.pivotFields.length > 0) {
681
+ for (const field of pivot.pivotFields) {
682
+ columns.push(pivotFieldToColumn(field));
683
+ }
684
+ }
640
685
  columns.push(...generateTimestampColumns());
641
686
  foreignKeys.push({
642
687
  columns: [pivot.sourceColumn],
@@ -1575,12 +1620,30 @@ function generateRelation(propName, assoc, schema, schemas, options) {
1575
1620
  }
1576
1621
  case "ManyToMany": {
1577
1622
  const pivotTable = assoc.joinTable ?? `${toSnakeCase(propName)}_pivot`;
1623
+ let pivotFieldNames = [];
1624
+ if (assoc.pivotFields && Object.keys(assoc.pivotFields).length > 0) {
1625
+ pivotFieldNames = Object.keys(assoc.pivotFields).map((f) => toSnakeCase(f));
1626
+ } else if (assoc.mappedBy && assoc.target) {
1627
+ const targetSchema = schemas[assoc.target];
1628
+ if (targetSchema?.properties) {
1629
+ const owningProp = targetSchema.properties[assoc.mappedBy];
1630
+ if (owningProp?.type === "Association") {
1631
+ const owningAssoc = owningProp;
1632
+ if (owningAssoc.pivotFields && Object.keys(owningAssoc.pivotFields).length > 0) {
1633
+ pivotFieldNames = Object.keys(owningAssoc.pivotFields).map((f) => toSnakeCase(f));
1634
+ }
1635
+ }
1636
+ }
1637
+ }
1638
+ const pivotFieldsCode = pivotFieldNames.length > 0 ? pivotFieldNames.map((f) => `'${f}'`).join(", ") : null;
1639
+ const withPivotLine = pivotFieldsCode ? `
1640
+ ->withPivot(${pivotFieldsCode})` : "";
1578
1641
  return ` /**
1579
1642
  * The ${propName} that belong to this model.
1580
1643
  */
1581
1644
  public function ${methodName}(): BelongsToMany
1582
1645
  {
1583
- return $this->belongsToMany(${targetClass}::class, '${pivotTable}')
1646
+ return $this->belongsToMany(${targetClass}::class, '${pivotTable}')${withPivotLine}
1584
1647
  ->withTimestamps();
1585
1648
  }`;
1586
1649
  }