@famgia/omnify-laravel 0.0.106 → 0.0.108

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-GDFAEEEG.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],
@@ -738,6 +783,16 @@ function generateCreateMigration(blueprint, options = {}) {
738
783
  const indexContent = renderIndexes(blueprint);
739
784
  const content = `<?php
740
785
 
786
+ /**
787
+ * DO NOT EDIT - This file is auto-generated by Omnify.
788
+ * Any changes will be overwritten on next generation.
789
+ *
790
+ * To modify the schema, edit the corresponding YAML file in schemas/
791
+ * and run: npx omnify generate
792
+ *
793
+ * @generated by @famgia/omnify-laravel
794
+ */
795
+
741
796
  use Illuminate\\Database\\Migrations\\Migration;
742
797
  use Illuminate\\Database\\Schema\\Blueprint;
743
798
  use Illuminate\\Support\\Facades\\Schema;
@@ -1041,6 +1096,16 @@ function generateAlterMigrationContent(tableName, change, options = {}) {
1041
1096
  ` : "";
1042
1097
  return `<?php
1043
1098
 
1099
+ /**
1100
+ * DO NOT EDIT - This file is auto-generated by Omnify.
1101
+ * Any changes will be overwritten on next generation.
1102
+ *
1103
+ * To modify the schema, edit the corresponding YAML file in schemas/
1104
+ * and run: npx omnify generate
1105
+ *
1106
+ * @generated by @famgia/omnify-laravel
1107
+ */
1108
+
1044
1109
  use Illuminate\\Database\\Migrations\\Migration;
1045
1110
  use Illuminate\\Database\\Schema\\Blueprint;
1046
1111
  use Illuminate\\Support\\Facades\\Schema;
@@ -1098,6 +1163,13 @@ function generateDropTableMigration(schemaName, options = {}) {
1098
1163
  ` : "";
1099
1164
  const content = `<?php
1100
1165
 
1166
+ /**
1167
+ * DO NOT EDIT - This file is auto-generated by Omnify.
1168
+ * Any changes will be overwritten on next generation.
1169
+ *
1170
+ * @generated by @famgia/omnify-laravel
1171
+ */
1172
+
1101
1173
  use Illuminate\\Database\\Migrations\\Migration;
1102
1174
  use Illuminate\\Database\\Schema\\Blueprint;
1103
1175
  use Illuminate\\Support\\Facades\\Schema;
@@ -1575,12 +1647,30 @@ function generateRelation(propName, assoc, schema, schemas, options) {
1575
1647
  }
1576
1648
  case "ManyToMany": {
1577
1649
  const pivotTable = assoc.joinTable ?? `${toSnakeCase(propName)}_pivot`;
1650
+ let pivotFieldNames = [];
1651
+ if (assoc.pivotFields && Object.keys(assoc.pivotFields).length > 0) {
1652
+ pivotFieldNames = Object.keys(assoc.pivotFields).map((f) => toSnakeCase(f));
1653
+ } else if (assoc.mappedBy && assoc.target) {
1654
+ const targetSchema = schemas[assoc.target];
1655
+ if (targetSchema?.properties) {
1656
+ const owningProp = targetSchema.properties[assoc.mappedBy];
1657
+ if (owningProp?.type === "Association") {
1658
+ const owningAssoc = owningProp;
1659
+ if (owningAssoc.pivotFields && Object.keys(owningAssoc.pivotFields).length > 0) {
1660
+ pivotFieldNames = Object.keys(owningAssoc.pivotFields).map((f) => toSnakeCase(f));
1661
+ }
1662
+ }
1663
+ }
1664
+ }
1665
+ const pivotFieldsCode = pivotFieldNames.length > 0 ? pivotFieldNames.map((f) => `'${f}'`).join(", ") : null;
1666
+ const withPivotLine = pivotFieldsCode ? `
1667
+ ->withPivot(${pivotFieldsCode})` : "";
1578
1668
  return ` /**
1579
1669
  * The ${propName} that belong to this model.
1580
1670
  */
1581
1671
  public function ${methodName}(): BelongsToMany
1582
1672
  {
1583
- return $this->belongsToMany(${targetClass}::class, '${pivotTable}')
1673
+ return $this->belongsToMany(${targetClass}::class, '${pivotTable}')${withPivotLine}
1584
1674
  ->withTimestamps();
1585
1675
  }`;
1586
1676
  }