@famgia/omnify-laravel 2.0.31 → 2.0.33

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.
@@ -1461,8 +1461,94 @@ var TYPE_METHOD_MAP2 = {
1461
1461
  MultiFile: "json",
1462
1462
  Enum: "enum",
1463
1463
  Select: "string",
1464
- Lookup: "unsignedBigInteger"
1464
+ Lookup: "unsignedBigInteger",
1465
+ EnumRef: "string"
1466
+ // EnumRef stores the enum value as string
1465
1467
  };
1468
+ function expandCompoundTypeForAlter(propName, prop, options) {
1469
+ const { customTypes, pluginEnums } = options;
1470
+ if (!customTypes) return null;
1471
+ const typeDef = customTypes.get(prop.type);
1472
+ if (!typeDef || !typeDef.compound || !typeDef.expand) {
1473
+ return null;
1474
+ }
1475
+ const expanded = [];
1476
+ const propWithFields = prop;
1477
+ for (const field of typeDef.expand) {
1478
+ const suffixSnake = toColumnName(field.suffix);
1479
+ const columnName = `${toColumnName(propName)}_${suffixSnake}`;
1480
+ const fieldOverride = propWithFields.fields?.[field.suffix];
1481
+ let method = "string";
1482
+ let nullable = prop.nullable ?? false;
1483
+ let length;
1484
+ let precision;
1485
+ let scale;
1486
+ let unsigned;
1487
+ let enumValues;
1488
+ const fieldWithEnumRef = field;
1489
+ if (fieldWithEnumRef.enumRef) {
1490
+ const enumDef = pluginEnums?.get(fieldWithEnumRef.enumRef);
1491
+ if (enumDef && enumDef.values.length > 0) {
1492
+ method = "enum";
1493
+ enumValues = enumDef.values.map((v) => v.value);
1494
+ } else {
1495
+ method = "string";
1496
+ length = 50;
1497
+ }
1498
+ if (fieldOverride?.nullable !== void 0) {
1499
+ nullable = fieldOverride.nullable;
1500
+ }
1501
+ } else if (field.sql) {
1502
+ const sqlType = field.sql.sqlType.toUpperCase();
1503
+ if (sqlType === "VARCHAR" || sqlType === "CHAR" || sqlType === "STRING") {
1504
+ method = "string";
1505
+ length = fieldOverride?.length ?? field.sql.length;
1506
+ } else if (sqlType === "TINYINT") {
1507
+ method = "tinyInteger";
1508
+ } else if (sqlType === "INT" || sqlType === "INTEGER") {
1509
+ method = "integer";
1510
+ } else if (sqlType === "BIGINT") {
1511
+ method = "bigInteger";
1512
+ } else if (sqlType === "TEXT") {
1513
+ method = "text";
1514
+ } else if (sqlType === "BOOLEAN" || sqlType === "BOOL") {
1515
+ method = "boolean";
1516
+ } else if (sqlType === "DECIMAL") {
1517
+ method = "decimal";
1518
+ precision = field.sql.precision;
1519
+ scale = field.sql.scale;
1520
+ } else if (sqlType === "DATE") {
1521
+ method = "date";
1522
+ } else if (sqlType === "TIMESTAMP" || sqlType === "DATETIME") {
1523
+ method = "timestamp";
1524
+ }
1525
+ if (field.sql.unsigned) {
1526
+ unsigned = true;
1527
+ }
1528
+ if (fieldOverride?.nullable !== void 0) {
1529
+ nullable = fieldOverride.nullable;
1530
+ } else if (field.sql.nullable !== void 0) {
1531
+ nullable = field.sql.nullable;
1532
+ }
1533
+ }
1534
+ expanded.push({
1535
+ columnName,
1536
+ method,
1537
+ nullable,
1538
+ length,
1539
+ precision,
1540
+ scale,
1541
+ unsigned,
1542
+ enumValues
1543
+ });
1544
+ }
1545
+ return expanded;
1546
+ }
1547
+ function isCompoundType(propType, customTypes) {
1548
+ if (!customTypes) return false;
1549
+ const typeDef = customTypes.get(propType);
1550
+ return !!(typeDef?.compound && typeDef?.expand);
1551
+ }
1466
1552
  function isAssociationWithFkColumn(prop) {
1467
1553
  if (prop.type !== "Association") return false;
1468
1554
  const relation = prop.relation;
@@ -1489,11 +1575,33 @@ function shouldSkipAssociationColumn(prop) {
1489
1575
  if (isAssociationWithFkColumn(prop)) return false;
1490
1576
  return true;
1491
1577
  }
1492
- function formatAddColumn(columnName, prop) {
1578
+ function formatAddColumn(columnName, prop, options = {}) {
1493
1579
  const lines = [];
1494
1580
  if (shouldSkipAssociationColumn(prop)) {
1495
1581
  return lines;
1496
1582
  }
1583
+ const expandedColumns = expandCompoundTypeForAlter(columnName, prop, options);
1584
+ if (expandedColumns) {
1585
+ for (const col of expandedColumns) {
1586
+ let code2;
1587
+ if (col.method === "decimal") {
1588
+ const precision = col.precision ?? 8;
1589
+ const scale = col.scale ?? 2;
1590
+ code2 = `$table->${col.method}('${col.columnName}', ${precision}, ${scale})`;
1591
+ } else if (col.method === "enum" && col.enumValues) {
1592
+ const enumValuesStr = col.enumValues.map((v) => `'${v}'`).join(", ");
1593
+ code2 = `$table->${col.method}('${col.columnName}', [${enumValuesStr}])`;
1594
+ } else if (col.method === "string" && col.length) {
1595
+ code2 = `$table->${col.method}('${col.columnName}', ${col.length})`;
1596
+ } else {
1597
+ code2 = `$table->${col.method}('${col.columnName}')`;
1598
+ }
1599
+ if (col.nullable) code2 += "->nullable()";
1600
+ if (col.unsigned) code2 += "->unsigned()";
1601
+ lines.push(code2 + ";");
1602
+ }
1603
+ return lines;
1604
+ }
1497
1605
  if (isAssociationWithFkColumn(prop)) {
1498
1606
  const fkColumn = getAssociationFkColumnName(columnName);
1499
1607
  const idType = prop.idType ?? "BigInt";
@@ -1524,11 +1632,23 @@ function formatAddColumn(columnName, prop) {
1524
1632
  const precision = prop.precision ?? 8;
1525
1633
  const scale = prop.scale ?? 2;
1526
1634
  code = `$table->${method}('${snakeColumn}', ${precision}, ${scale})`;
1635
+ } else if (prop.type === "Enum" && prop.enum && prop.enum.length > 0) {
1636
+ const enumValues = prop.enum.map(
1637
+ (v) => typeof v === "string" ? v : v.value
1638
+ );
1639
+ code = `$table->${method}('${snakeColumn}', [${enumValues.map((v) => `'${v}'`).join(", ")}])`;
1640
+ } else if (method === "string" && prop.length) {
1641
+ code = `$table->${method}('${snakeColumn}', ${prop.length})`;
1642
+ } else if (prop.type === "EnumRef") {
1643
+ code = `$table->${method}('${snakeColumn}', 50)`;
1527
1644
  } else {
1528
1645
  code = `$table->${method}('${snakeColumn}')`;
1529
1646
  }
1530
1647
  if (prop.nullable) code += "->nullable()";
1531
1648
  if (prop.unique) code += "->unique()";
1649
+ if (prop.unsigned && (method === "tinyInteger" || method === "integer" || method === "bigInteger")) {
1650
+ code += "->unsigned()";
1651
+ }
1532
1652
  if (prop.default !== void 0) {
1533
1653
  const defaultValue = typeof prop.default === "string" ? `'${prop.default}'` : JSON.stringify(prop.default);
1534
1654
  code += `->default(${defaultValue})`;
@@ -1536,11 +1656,20 @@ function formatAddColumn(columnName, prop) {
1536
1656
  lines.push(code + ";");
1537
1657
  return lines;
1538
1658
  }
1539
- function formatDropColumn(columnName, prop) {
1659
+ function formatDropColumn(columnName, prop, options = {}) {
1540
1660
  const lines = [];
1541
1661
  if (prop && shouldSkipAssociationColumn(prop)) {
1542
1662
  return lines;
1543
1663
  }
1664
+ if (prop && isCompoundType(prop.type, options.customTypes)) {
1665
+ const expandedColumns = expandCompoundTypeForAlter(columnName, prop, options);
1666
+ if (expandedColumns) {
1667
+ for (const col of expandedColumns) {
1668
+ lines.push(`$table->dropColumn('${col.columnName}');`);
1669
+ }
1670
+ return lines;
1671
+ }
1672
+ }
1544
1673
  if (prop && isAssociationWithFkColumn(prop)) {
1545
1674
  const fkColumn = getAssociationFkColumnName(columnName);
1546
1675
  lines.push(`$table->dropForeign(['${fkColumn}']);`);
@@ -1551,17 +1680,55 @@ function formatDropColumn(columnName, prop) {
1551
1680
  lines.push(`$table->dropColumn('${snakeColumn}');`);
1552
1681
  return lines;
1553
1682
  }
1554
- function formatRenameColumn(oldName, newName, prop) {
1683
+ function formatRenameColumn(oldName, newName, prop, options = {}) {
1684
+ const lines = [];
1685
+ if (prop && isCompoundType(prop.type, options.customTypes)) {
1686
+ const typeDef = options.customTypes?.get(prop.type);
1687
+ if (typeDef?.expand) {
1688
+ for (const field of typeDef.expand) {
1689
+ const suffixSnake = toColumnName(field.suffix);
1690
+ const oldColumn = `${toColumnName(oldName)}_${suffixSnake}`;
1691
+ const newColumn = `${toColumnName(newName)}_${suffixSnake}`;
1692
+ lines.push(`$table->renameColumn('${oldColumn}', '${newColumn}');`);
1693
+ }
1694
+ return lines;
1695
+ }
1696
+ }
1555
1697
  if (prop && isAssociationWithFkColumn(prop)) {
1556
1698
  const oldFkColumn = getAssociationFkColumnName(oldName);
1557
1699
  const newFkColumn = getAssociationFkColumnName(newName);
1558
- return `$table->renameColumn('${oldFkColumn}', '${newFkColumn}');`;
1700
+ lines.push(`$table->renameColumn('${oldFkColumn}', '${newFkColumn}');`);
1701
+ return lines;
1559
1702
  }
1560
1703
  const oldSnake = toColumnName(oldName);
1561
1704
  const newSnake = toColumnName(newName);
1562
- return `$table->renameColumn('${oldSnake}', '${newSnake}');`;
1705
+ lines.push(`$table->renameColumn('${oldSnake}', '${newSnake}');`);
1706
+ return lines;
1563
1707
  }
1564
- function formatModifyColumn(columnName, _prevProp, currProp) {
1708
+ function formatModifyColumn(columnName, _prevProp, currProp, options = {}) {
1709
+ const lines = [];
1710
+ const expandedColumns = expandCompoundTypeForAlter(columnName, currProp, options);
1711
+ if (expandedColumns) {
1712
+ for (const col of expandedColumns) {
1713
+ let code2;
1714
+ if (col.method === "decimal") {
1715
+ const precision = col.precision ?? 8;
1716
+ const scale = col.scale ?? 2;
1717
+ code2 = `$table->${col.method}('${col.columnName}', ${precision}, ${scale})`;
1718
+ } else if (col.method === "enum" && col.enumValues) {
1719
+ const enumValuesStr = col.enumValues.map((v) => `'${v}'`).join(", ");
1720
+ code2 = `$table->${col.method}('${col.columnName}', [${enumValuesStr}])`;
1721
+ } else if (col.method === "string" && col.length) {
1722
+ code2 = `$table->${col.method}('${col.columnName}', ${col.length})`;
1723
+ } else {
1724
+ code2 = `$table->${col.method}('${col.columnName}')`;
1725
+ }
1726
+ if (col.nullable) code2 += "->nullable()";
1727
+ if (col.unsigned) code2 += "->unsigned()";
1728
+ lines.push(code2 + "->change();");
1729
+ }
1730
+ return lines;
1731
+ }
1565
1732
  if (isAssociationWithFkColumn(currProp)) {
1566
1733
  const fkColumn = getAssociationFkColumnName(columnName);
1567
1734
  const idType = currProp.idType ?? "BigInt";
@@ -1571,7 +1738,8 @@ function formatModifyColumn(columnName, _prevProp, currProp) {
1571
1738
  else if (idType === "String") columnMethod = "string";
1572
1739
  let code2 = `$table->${columnMethod}('${fkColumn}')`;
1573
1740
  if (currProp.nullable) code2 += "->nullable()";
1574
- return code2 + "->change();";
1741
+ lines.push(code2 + "->change();");
1742
+ return lines;
1575
1743
  }
1576
1744
  const snakeColumn = toColumnName(columnName);
1577
1745
  const method = TYPE_METHOD_MAP2[currProp.type] ?? "string";
@@ -1580,16 +1748,29 @@ function formatModifyColumn(columnName, _prevProp, currProp) {
1580
1748
  const precision = currProp.precision ?? 8;
1581
1749
  const scale = currProp.scale ?? 2;
1582
1750
  code = `$table->${method}('${snakeColumn}', ${precision}, ${scale})`;
1751
+ } else if (currProp.type === "Enum" && currProp.enum && currProp.enum.length > 0) {
1752
+ const enumValues = currProp.enum.map(
1753
+ (v) => typeof v === "string" ? v : v.value
1754
+ );
1755
+ code = `$table->${method}('${snakeColumn}', [${enumValues.map((v) => `'${v}'`).join(", ")}])`;
1756
+ } else if (method === "string" && currProp.length) {
1757
+ code = `$table->${method}('${snakeColumn}', ${currProp.length})`;
1758
+ } else if (currProp.type === "EnumRef") {
1759
+ code = `$table->${method}('${snakeColumn}', 50)`;
1583
1760
  } else {
1584
1761
  code = `$table->${method}('${snakeColumn}')`;
1585
1762
  }
1586
1763
  if (currProp.nullable) code += "->nullable()";
1587
1764
  if (currProp.unique) code += "->unique()";
1765
+ if (currProp.unsigned && (method === "tinyInteger" || method === "integer" || method === "bigInteger")) {
1766
+ code += "->unsigned()";
1767
+ }
1588
1768
  if (currProp.default !== void 0) {
1589
1769
  const defaultValue = typeof currProp.default === "string" ? `'${currProp.default}'` : JSON.stringify(currProp.default);
1590
1770
  code += `->default(${defaultValue})`;
1591
1771
  }
1592
- return code + "->change();";
1772
+ lines.push(code + "->change();");
1773
+ return lines;
1593
1774
  }
1594
1775
  function formatAddIndex(columns, unique) {
1595
1776
  const snakeColumns = columns.map(toColumnName);
@@ -1607,35 +1788,57 @@ function formatDropIndex(tableName, columns, unique) {
1607
1788
  function generateAlterMigrationContent(tableName, change, options = {}) {
1608
1789
  const upLines = [];
1609
1790
  const downLines = [];
1791
+ const compoundOptions = {
1792
+ customTypes: options.customTypes,
1793
+ pluginEnums: options.pluginEnums
1794
+ };
1610
1795
  if (change.columnChanges) {
1611
1796
  for (const col of change.columnChanges) {
1612
1797
  if (col.changeType === "added" && col.currentDef) {
1613
- const addLines = formatAddColumn(col.column, col.currentDef);
1798
+ const addLines = formatAddColumn(col.column, col.currentDef, compoundOptions);
1614
1799
  for (const line of addLines) {
1615
1800
  upLines.push(` ${line}`);
1616
1801
  }
1617
- const dropLines = formatDropColumn(col.column, col.currentDef);
1802
+ const dropLines = formatDropColumn(col.column, col.currentDef, compoundOptions);
1618
1803
  for (const line of dropLines) {
1619
1804
  downLines.push(` ${line}`);
1620
1805
  }
1621
1806
  } else if (col.changeType === "removed" && col.previousDef) {
1622
- const dropLines = formatDropColumn(col.column, col.previousDef);
1807
+ const dropLines = formatDropColumn(col.column, col.previousDef, compoundOptions);
1623
1808
  for (const line of dropLines) {
1624
1809
  upLines.push(` ${line}`);
1625
1810
  }
1626
- const addLines = formatAddColumn(col.column, col.previousDef);
1811
+ const addLines = formatAddColumn(col.column, col.previousDef, compoundOptions);
1627
1812
  for (const line of addLines) {
1628
1813
  downLines.push(` ${line}`);
1629
1814
  }
1630
1815
  } else if (col.changeType === "modified" && col.previousDef && col.currentDef) {
1631
- upLines.push(` ${formatModifyColumn(col.column, col.previousDef, col.currentDef)}`);
1632
- downLines.push(` ${formatModifyColumn(col.column, col.currentDef, col.previousDef)}`);
1816
+ const modifyLines = formatModifyColumn(col.column, col.previousDef, col.currentDef, compoundOptions);
1817
+ for (const line of modifyLines) {
1818
+ upLines.push(` ${line}`);
1819
+ }
1820
+ const revertLines = formatModifyColumn(col.column, col.currentDef, col.previousDef, compoundOptions);
1821
+ for (const line of revertLines) {
1822
+ downLines.push(` ${line}`);
1823
+ }
1633
1824
  } else if (col.changeType === "renamed" && col.previousColumn) {
1634
- upLines.push(` ${formatRenameColumn(col.previousColumn, col.column, col.currentDef)}`);
1635
- downLines.push(` ${formatRenameColumn(col.column, col.previousColumn, col.currentDef)}`);
1825
+ const renameLines = formatRenameColumn(col.previousColumn, col.column, col.currentDef, compoundOptions);
1826
+ for (const line of renameLines) {
1827
+ upLines.push(` ${line}`);
1828
+ }
1829
+ const revertRenameLines = formatRenameColumn(col.column, col.previousColumn, col.currentDef, compoundOptions);
1830
+ for (const line of revertRenameLines) {
1831
+ downLines.push(` ${line}`);
1832
+ }
1636
1833
  if (col.modifications && col.modifications.length > 0 && col.previousDef && col.currentDef) {
1637
- upLines.push(` ${formatModifyColumn(col.column, col.previousDef, col.currentDef)}`);
1638
- downLines.push(` ${formatModifyColumn(col.column, col.currentDef, col.previousDef)}`);
1834
+ const modifyLines = formatModifyColumn(col.column, col.previousDef, col.currentDef, compoundOptions);
1835
+ for (const line of modifyLines) {
1836
+ upLines.push(` ${line}`);
1837
+ }
1838
+ const revertLines = formatModifyColumn(col.column, col.currentDef, col.previousDef, compoundOptions);
1839
+ for (const line of revertLines) {
1840
+ downLines.push(` ${line}`);
1841
+ }
1639
1842
  }
1640
1843
  }
1641
1844
  }
@@ -2097,8 +2300,8 @@ function generateEntityBaseModel(schema, schemas, options, stubContent, authStub
2097
2300
  for (const [propName, propDef] of Object.entries(properties)) {
2098
2301
  const snakeName = toSnakeCase(propName);
2099
2302
  const typeDef = options.customTypes.get(propDef.type);
2100
- const isCompoundType = typeDef?.compound && typeDef.expand;
2101
- if (!isCompoundType) {
2303
+ const isCompoundType2 = typeDef?.compound && typeDef.expand;
2304
+ if (!isCompoundType2) {
2102
2305
  const phpType = getPhpDocType(propDef, schemas);
2103
2306
  docProperties.push(` * @property ${phpType} $${snakeName}`);
2104
2307
  }
@@ -2164,8 +2367,8 @@ function generateEntityBaseModel(schema, schemas, options, stubContent, authStub
2164
2367
  const isFillable = propWithOptions.fillable !== false;
2165
2368
  const isHidden = propWithOptions.hidden === true;
2166
2369
  const typeDef2 = options.customTypes.get(propDef.type);
2167
- const isCompoundType2 = typeDef2?.compound && typeDef2.expand;
2168
- if (isCompoundType2 && typeDef2.expand) {
2370
+ const isCompoundType3 = typeDef2?.compound && typeDef2.expand;
2371
+ if (isCompoundType3 && typeDef2.expand) {
2169
2372
  const fieldOverrides = propWithOptions.fields ?? {};
2170
2373
  const basePropWithNullable = propDef;
2171
2374
  for (const field of typeDef2.expand) {
@@ -5463,4 +5666,4 @@ export {
5463
5666
  getFactoryPath,
5464
5667
  laravelPlugin
5465
5668
  };
5466
- //# sourceMappingURL=chunk-FQYGLKZY.js.map
5669
+ //# sourceMappingURL=chunk-XEXTOJRM.js.map