@famgia/omnify-laravel 0.0.106 → 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/{chunk-XR2DTIIS.js → chunk-7YHLXBF5.js} +191 -3
- package/dist/chunk-7YHLXBF5.js.map +1 -0
- package/dist/index.cjs +195 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +72 -1
- package/dist/index.d.ts +72 -1
- package/dist/index.js +11 -1
- package/dist/plugin.cjs +65 -2
- package/dist/plugin.cjs.map +1 -1
- package/dist/plugin.js +1 -1
- package/package.json +4 -4
- package/dist/chunk-XR2DTIIS.js.map +0 -1
|
@@ -556,6 +556,9 @@ function extractManyToManyRelations(schema, allSchemas) {
|
|
|
556
556
|
if (assocProp.relation !== "ManyToMany") {
|
|
557
557
|
continue;
|
|
558
558
|
}
|
|
559
|
+
if (assocProp.mappedBy) {
|
|
560
|
+
continue;
|
|
561
|
+
}
|
|
559
562
|
const targetName = assocProp.target;
|
|
560
563
|
if (!targetName) {
|
|
561
564
|
continue;
|
|
@@ -570,6 +573,19 @@ function extractManyToManyRelations(schema, allSchemas) {
|
|
|
570
573
|
const pivotTableName = generatePivotTableName(sourceTable, targetTable, assocProp.joinTable);
|
|
571
574
|
const sourceColumn = sourceTable.replace(/ies$/, "y").replace(/s$/, "") + "_id";
|
|
572
575
|
const targetColumn = targetTable.replace(/ies$/, "y").replace(/s$/, "") + "_id";
|
|
576
|
+
const pivotFields = [];
|
|
577
|
+
if (assocProp.pivotFields) {
|
|
578
|
+
for (const [fieldName, fieldDef] of Object.entries(assocProp.pivotFields)) {
|
|
579
|
+
pivotFields.push({
|
|
580
|
+
name: toColumnName(fieldName),
|
|
581
|
+
type: fieldDef.type,
|
|
582
|
+
nullable: fieldDef.nullable,
|
|
583
|
+
default: fieldDef.default,
|
|
584
|
+
length: fieldDef.length,
|
|
585
|
+
unsigned: fieldDef.unsigned
|
|
586
|
+
});
|
|
587
|
+
}
|
|
588
|
+
}
|
|
573
589
|
pivotTables.push({
|
|
574
590
|
tableName: pivotTableName,
|
|
575
591
|
sourceTable,
|
|
@@ -579,11 +595,35 @@ function extractManyToManyRelations(schema, allSchemas) {
|
|
|
579
595
|
sourcePkType,
|
|
580
596
|
targetPkType,
|
|
581
597
|
onDelete: assocProp.onDelete,
|
|
582
|
-
onUpdate: assocProp.onUpdate
|
|
598
|
+
onUpdate: assocProp.onUpdate,
|
|
599
|
+
pivotFields: pivotFields.length > 0 ? pivotFields : void 0
|
|
583
600
|
});
|
|
584
601
|
}
|
|
585
602
|
return pivotTables;
|
|
586
603
|
}
|
|
604
|
+
function pivotFieldToColumn(field) {
|
|
605
|
+
const method = TYPE_METHOD_MAP[field.type] ?? "string";
|
|
606
|
+
const args = [field.name];
|
|
607
|
+
const modifiers = [];
|
|
608
|
+
if (method === "string" && field.length) {
|
|
609
|
+
args.push(field.length);
|
|
610
|
+
}
|
|
611
|
+
if (field.nullable) {
|
|
612
|
+
modifiers.push({ method: "nullable" });
|
|
613
|
+
}
|
|
614
|
+
if (field.default !== void 0 && field.default !== null) {
|
|
615
|
+
modifiers.push({ method: "default", args: [field.default] });
|
|
616
|
+
}
|
|
617
|
+
if (field.unsigned && (method === "tinyInteger" || method === "integer" || method === "bigInteger")) {
|
|
618
|
+
modifiers.push({ method: "unsigned" });
|
|
619
|
+
}
|
|
620
|
+
return {
|
|
621
|
+
name: field.name,
|
|
622
|
+
method,
|
|
623
|
+
args,
|
|
624
|
+
modifiers
|
|
625
|
+
};
|
|
626
|
+
}
|
|
587
627
|
function generatePivotTableBlueprint(pivot) {
|
|
588
628
|
const columns = [];
|
|
589
629
|
const foreignKeys = [];
|
|
@@ -612,6 +652,11 @@ function generatePivotTableBlueprint(pivot) {
|
|
|
612
652
|
args: [pivot.targetColumn],
|
|
613
653
|
modifiers: []
|
|
614
654
|
});
|
|
655
|
+
if (pivot.pivotFields && pivot.pivotFields.length > 0) {
|
|
656
|
+
for (const field of pivot.pivotFields) {
|
|
657
|
+
columns.push(pivotFieldToColumn(field));
|
|
658
|
+
}
|
|
659
|
+
}
|
|
615
660
|
columns.push(...generateTimestampColumns());
|
|
616
661
|
foreignKeys.push({
|
|
617
662
|
columns: [pivot.sourceColumn],
|
|
@@ -647,6 +692,126 @@ function generatePivotTableBlueprint(pivot) {
|
|
|
647
692
|
indexes
|
|
648
693
|
};
|
|
649
694
|
}
|
|
695
|
+
function extractMorphToManyRelations(schema, allSchemas) {
|
|
696
|
+
const morphPivotTables = [];
|
|
697
|
+
if (!schema.properties) {
|
|
698
|
+
return morphPivotTables;
|
|
699
|
+
}
|
|
700
|
+
for (const [propName, property] of Object.entries(schema.properties)) {
|
|
701
|
+
if (property.type !== "Association") {
|
|
702
|
+
continue;
|
|
703
|
+
}
|
|
704
|
+
const assocProp = property;
|
|
705
|
+
if (assocProp.relation !== "MorphToMany") {
|
|
706
|
+
continue;
|
|
707
|
+
}
|
|
708
|
+
const targetName = assocProp.target;
|
|
709
|
+
if (!targetName) {
|
|
710
|
+
continue;
|
|
711
|
+
}
|
|
712
|
+
const targetSchema = allSchemas[targetName];
|
|
713
|
+
const targetTable = toTableName(targetName);
|
|
714
|
+
const targetPkType = targetSchema ? getIdType(targetSchema) : "BigInt";
|
|
715
|
+
const isOwningSide = assocProp.owning ?? schema.name < targetName;
|
|
716
|
+
if (!isOwningSide) {
|
|
717
|
+
continue;
|
|
718
|
+
}
|
|
719
|
+
const morphTargets = [];
|
|
720
|
+
morphTargets.push(schema.name);
|
|
721
|
+
for (const [otherName, otherSchema] of Object.entries(allSchemas)) {
|
|
722
|
+
if (otherName === schema.name) continue;
|
|
723
|
+
if (!otherSchema.properties) continue;
|
|
724
|
+
for (const otherProp of Object.values(otherSchema.properties)) {
|
|
725
|
+
if (otherProp.type !== "Association") continue;
|
|
726
|
+
const otherAssoc = otherProp;
|
|
727
|
+
if (otherAssoc.relation === "MorphToMany" && otherAssoc.target === targetName) {
|
|
728
|
+
if (!morphTargets.includes(otherName)) {
|
|
729
|
+
morphTargets.push(otherName);
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
const defaultTableName = targetTable.replace(/s$/, "") + "ables";
|
|
735
|
+
const tableName = assocProp.joinTable ?? defaultTableName;
|
|
736
|
+
const targetColumn = targetTable.replace(/ies$/, "y").replace(/s$/, "") + "_id";
|
|
737
|
+
const morphName = propName.replace(/s$/, "") + "able";
|
|
738
|
+
morphPivotTables.push({
|
|
739
|
+
tableName,
|
|
740
|
+
targetTable,
|
|
741
|
+
targetColumn,
|
|
742
|
+
targetPkType,
|
|
743
|
+
morphName,
|
|
744
|
+
morphTargets,
|
|
745
|
+
onDelete: assocProp.onDelete,
|
|
746
|
+
onUpdate: assocProp.onUpdate
|
|
747
|
+
});
|
|
748
|
+
}
|
|
749
|
+
return morphPivotTables;
|
|
750
|
+
}
|
|
751
|
+
function generateMorphToManyPivotBlueprint(pivot) {
|
|
752
|
+
const columns = [];
|
|
753
|
+
const foreignKeys = [];
|
|
754
|
+
const indexes = [];
|
|
755
|
+
const getMethodForPkType = (pkType) => {
|
|
756
|
+
switch (pkType) {
|
|
757
|
+
case "Int":
|
|
758
|
+
return "unsignedInteger";
|
|
759
|
+
case "Uuid":
|
|
760
|
+
return "uuid";
|
|
761
|
+
case "String":
|
|
762
|
+
return "string";
|
|
763
|
+
default:
|
|
764
|
+
return "unsignedBigInteger";
|
|
765
|
+
}
|
|
766
|
+
};
|
|
767
|
+
columns.push({
|
|
768
|
+
name: pivot.targetColumn,
|
|
769
|
+
method: getMethodForPkType(pivot.targetPkType),
|
|
770
|
+
args: [pivot.targetColumn],
|
|
771
|
+
modifiers: []
|
|
772
|
+
});
|
|
773
|
+
const typeColumnName = `${pivot.morphName}_type`;
|
|
774
|
+
columns.push({
|
|
775
|
+
name: typeColumnName,
|
|
776
|
+
method: "enum",
|
|
777
|
+
args: [typeColumnName, pivot.morphTargets],
|
|
778
|
+
modifiers: []
|
|
779
|
+
});
|
|
780
|
+
const idColumnName = `${pivot.morphName}_id`;
|
|
781
|
+
columns.push({
|
|
782
|
+
name: idColumnName,
|
|
783
|
+
method: "unsignedBigInteger",
|
|
784
|
+
// Default to BigInt for polymorphic IDs
|
|
785
|
+
args: [idColumnName],
|
|
786
|
+
modifiers: []
|
|
787
|
+
});
|
|
788
|
+
foreignKeys.push({
|
|
789
|
+
columns: [pivot.targetColumn],
|
|
790
|
+
references: "id",
|
|
791
|
+
on: [pivot.targetTable],
|
|
792
|
+
onDelete: pivot.onDelete ?? "cascade",
|
|
793
|
+
onUpdate: pivot.onUpdate ?? "cascade"
|
|
794
|
+
});
|
|
795
|
+
indexes.push({
|
|
796
|
+
columns: [pivot.targetColumn, typeColumnName, idColumnName],
|
|
797
|
+
unique: true
|
|
798
|
+
});
|
|
799
|
+
indexes.push({
|
|
800
|
+
columns: [typeColumnName, idColumnName],
|
|
801
|
+
unique: false
|
|
802
|
+
});
|
|
803
|
+
indexes.push({
|
|
804
|
+
columns: [pivot.targetColumn],
|
|
805
|
+
unique: false
|
|
806
|
+
});
|
|
807
|
+
return {
|
|
808
|
+
tableName: pivot.tableName,
|
|
809
|
+
columns,
|
|
810
|
+
primaryKey: [pivot.targetColumn, typeColumnName, idColumnName],
|
|
811
|
+
foreignKeys,
|
|
812
|
+
indexes
|
|
813
|
+
};
|
|
814
|
+
}
|
|
650
815
|
|
|
651
816
|
// src/migration/generator.ts
|
|
652
817
|
function generateTimestamp() {
|
|
@@ -1600,12 +1765,30 @@ function generateRelation(propName, assoc, schema, schemas, options) {
|
|
|
1600
1765
|
}
|
|
1601
1766
|
case "ManyToMany": {
|
|
1602
1767
|
const pivotTable = assoc.joinTable ?? `${toSnakeCase(propName)}_pivot`;
|
|
1768
|
+
let pivotFieldNames = [];
|
|
1769
|
+
if (assoc.pivotFields && Object.keys(assoc.pivotFields).length > 0) {
|
|
1770
|
+
pivotFieldNames = Object.keys(assoc.pivotFields).map((f) => toSnakeCase(f));
|
|
1771
|
+
} else if (assoc.mappedBy && assoc.target) {
|
|
1772
|
+
const targetSchema = schemas[assoc.target];
|
|
1773
|
+
if (targetSchema?.properties) {
|
|
1774
|
+
const owningProp = targetSchema.properties[assoc.mappedBy];
|
|
1775
|
+
if (owningProp?.type === "Association") {
|
|
1776
|
+
const owningAssoc = owningProp;
|
|
1777
|
+
if (owningAssoc.pivotFields && Object.keys(owningAssoc.pivotFields).length > 0) {
|
|
1778
|
+
pivotFieldNames = Object.keys(owningAssoc.pivotFields).map((f) => toSnakeCase(f));
|
|
1779
|
+
}
|
|
1780
|
+
}
|
|
1781
|
+
}
|
|
1782
|
+
}
|
|
1783
|
+
const pivotFieldsCode = pivotFieldNames.length > 0 ? pivotFieldNames.map((f) => `'${f}'`).join(", ") : null;
|
|
1784
|
+
const withPivotLine = pivotFieldsCode ? `
|
|
1785
|
+
->withPivot(${pivotFieldsCode})` : "";
|
|
1603
1786
|
return ` /**
|
|
1604
1787
|
* The ${propName} that belong to this model.
|
|
1605
1788
|
*/
|
|
1606
1789
|
public function ${methodName}(): BelongsToMany
|
|
1607
1790
|
{
|
|
1608
|
-
return $this->belongsToMany(${targetClass}::class, '${pivotTable}')
|
|
1791
|
+
return $this->belongsToMany(${targetClass}::class, '${pivotTable}')${withPivotLine}
|
|
1609
1792
|
->withTimestamps();
|
|
1610
1793
|
}`;
|
|
1611
1794
|
}
|
|
@@ -4578,6 +4761,11 @@ export {
|
|
|
4578
4761
|
formatColumnMethod,
|
|
4579
4762
|
formatForeignKey,
|
|
4580
4763
|
formatIndex,
|
|
4764
|
+
generatePivotTableName,
|
|
4765
|
+
extractManyToManyRelations,
|
|
4766
|
+
generatePivotTableBlueprint,
|
|
4767
|
+
extractMorphToManyRelations,
|
|
4768
|
+
generateMorphToManyPivotBlueprint,
|
|
4581
4769
|
generateMigrations,
|
|
4582
4770
|
generateMigrationFromSchema,
|
|
4583
4771
|
generateDropMigrationForTable,
|
|
@@ -4595,4 +4783,4 @@ export {
|
|
|
4595
4783
|
shouldGenerateAIGuides,
|
|
4596
4784
|
laravelPlugin
|
|
4597
4785
|
};
|
|
4598
|
-
//# sourceMappingURL=chunk-
|
|
4786
|
+
//# sourceMappingURL=chunk-7YHLXBF5.js.map
|