@famgia/omnify-laravel 0.0.17 → 0.0.19
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/README.md +130 -0
- package/dist/{chunk-52JAFQBQ.js → chunk-ILSNN7UV.js} +403 -14
- package/dist/chunk-ILSNN7UV.js.map +1 -0
- package/dist/index.cjs +401 -12
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +12 -2
- package/dist/index.d.ts +12 -2
- package/dist/index.js +1 -1
- package/dist/plugin.cjs +682 -12
- package/dist/plugin.cjs.map +1 -1
- package/dist/plugin.js +1 -1
- package/package.json +2 -2
- package/dist/chunk-52JAFQBQ.js.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -55,6 +55,7 @@ var TYPE_METHOD_MAP = {
|
|
|
55
55
|
LongText: "longText",
|
|
56
56
|
Date: "date",
|
|
57
57
|
Time: "time",
|
|
58
|
+
DateTime: "dateTime",
|
|
58
59
|
Timestamp: "timestamp",
|
|
59
60
|
Json: "json",
|
|
60
61
|
Email: "string",
|
|
@@ -127,6 +128,10 @@ function propertyToColumnMethod(propertyName, property) {
|
|
|
127
128
|
if (baseProp.unsigned && (method === "integer" || method === "bigInteger")) {
|
|
128
129
|
modifiers.push({ method: "unsigned" });
|
|
129
130
|
}
|
|
131
|
+
const displayName = property.displayName;
|
|
132
|
+
if (displayName) {
|
|
133
|
+
modifiers.push({ method: "comment", args: [displayName] });
|
|
134
|
+
}
|
|
130
135
|
return {
|
|
131
136
|
name: columnName,
|
|
132
137
|
method,
|
|
@@ -261,6 +266,9 @@ function generateForeignKey(propertyName, property, allSchemas) {
|
|
|
261
266
|
if (assocProp.default !== void 0 && assocProp.default !== null) {
|
|
262
267
|
modifiers.push({ method: "default", args: [assocProp.default] });
|
|
263
268
|
}
|
|
269
|
+
if (assocProp.displayName) {
|
|
270
|
+
modifiers.push({ method: "comment", args: [assocProp.displayName] });
|
|
271
|
+
}
|
|
264
272
|
const column = {
|
|
265
273
|
name: columnName,
|
|
266
274
|
method,
|
|
@@ -280,7 +288,65 @@ function generateForeignKey(propertyName, property, allSchemas) {
|
|
|
280
288
|
};
|
|
281
289
|
return { column, foreignKey, index };
|
|
282
290
|
}
|
|
283
|
-
function
|
|
291
|
+
function expandCompoundType(propName, property, customTypes) {
|
|
292
|
+
const typeDef = customTypes.get(property.type);
|
|
293
|
+
if (!typeDef || !typeDef.compound || !typeDef.expand) {
|
|
294
|
+
return null;
|
|
295
|
+
}
|
|
296
|
+
const expanded = [];
|
|
297
|
+
const baseProp = property;
|
|
298
|
+
for (const field of typeDef.expand) {
|
|
299
|
+
const suffixSnake = toColumnName(field.suffix);
|
|
300
|
+
const columnName = `${propName}_${suffixSnake}`;
|
|
301
|
+
const expandedProp = {
|
|
302
|
+
type: "String"
|
|
303
|
+
// Default type, will be overridden by sql definition
|
|
304
|
+
};
|
|
305
|
+
if (field.sql) {
|
|
306
|
+
const sqlType = field.sql.sqlType.toUpperCase();
|
|
307
|
+
if (sqlType === "VARCHAR" || sqlType === "CHAR" || sqlType === "STRING") {
|
|
308
|
+
expandedProp.type = "String";
|
|
309
|
+
if (field.sql.length) {
|
|
310
|
+
expandedProp.length = field.sql.length;
|
|
311
|
+
}
|
|
312
|
+
} else if (sqlType === "INT" || sqlType === "INTEGER") {
|
|
313
|
+
expandedProp.type = "Int";
|
|
314
|
+
} else if (sqlType === "BIGINT") {
|
|
315
|
+
expandedProp.type = "BigInt";
|
|
316
|
+
} else if (sqlType === "TEXT") {
|
|
317
|
+
expandedProp.type = "Text";
|
|
318
|
+
} else if (sqlType === "BOOLEAN" || sqlType === "BOOL") {
|
|
319
|
+
expandedProp.type = "Boolean";
|
|
320
|
+
} else if (sqlType === "DECIMAL") {
|
|
321
|
+
expandedProp.type = "Decimal";
|
|
322
|
+
if (field.sql.precision) expandedProp.precision = field.sql.precision;
|
|
323
|
+
if (field.sql.scale) expandedProp.scale = field.sql.scale;
|
|
324
|
+
} else if (sqlType === "DATE") {
|
|
325
|
+
expandedProp.type = "Date";
|
|
326
|
+
} else if (sqlType === "TIMESTAMP" || sqlType === "DATETIME") {
|
|
327
|
+
expandedProp.type = "Timestamp";
|
|
328
|
+
}
|
|
329
|
+
if (field.sql.nullable !== void 0) {
|
|
330
|
+
expandedProp.nullable = field.sql.nullable;
|
|
331
|
+
} else if (baseProp.nullable !== void 0) {
|
|
332
|
+
expandedProp.nullable = baseProp.nullable;
|
|
333
|
+
}
|
|
334
|
+
if (field.sql.default !== void 0) {
|
|
335
|
+
expandedProp.default = field.sql.default;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
if (baseProp.displayName) {
|
|
339
|
+
expandedProp.displayName = `${baseProp.displayName} (${field.suffix})`;
|
|
340
|
+
}
|
|
341
|
+
expanded.push({
|
|
342
|
+
name: columnName,
|
|
343
|
+
property: expandedProp
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
return expanded;
|
|
347
|
+
}
|
|
348
|
+
function schemaToBlueprint(schema, allSchemas, options = {}) {
|
|
349
|
+
const { customTypes = /* @__PURE__ */ new Map() } = options;
|
|
284
350
|
const tableName = toTableName(schema.name);
|
|
285
351
|
const columns = [];
|
|
286
352
|
const foreignKeys = [];
|
|
@@ -291,6 +357,16 @@ function schemaToBlueprint(schema, allSchemas) {
|
|
|
291
357
|
}
|
|
292
358
|
if (schema.properties) {
|
|
293
359
|
for (const [propName, property] of Object.entries(schema.properties)) {
|
|
360
|
+
const expandedProps = expandCompoundType(propName, property, customTypes);
|
|
361
|
+
if (expandedProps) {
|
|
362
|
+
for (const { name: expandedName, property: expandedProp } of expandedProps) {
|
|
363
|
+
const columnMethod2 = propertyToColumnMethod(expandedName, expandedProp);
|
|
364
|
+
if (columnMethod2) {
|
|
365
|
+
columns.push(columnMethod2);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
continue;
|
|
369
|
+
}
|
|
294
370
|
const columnMethod = propertyToColumnMethod(propName, property);
|
|
295
371
|
if (columnMethod) {
|
|
296
372
|
columns.push(columnMethod);
|
|
@@ -737,7 +813,9 @@ function generateMigrations(schemas, options = {}) {
|
|
|
737
813
|
const timestamp = options.timestamp ?? generateTimestamp();
|
|
738
814
|
const offsetTimestamp = incrementTimestamp(timestamp, timestampOffset);
|
|
739
815
|
timestampOffset++;
|
|
740
|
-
const blueprint = schemaToBlueprint(schema, schemas
|
|
816
|
+
const blueprint = schemaToBlueprint(schema, schemas, {
|
|
817
|
+
customTypes: options.customTypes
|
|
818
|
+
});
|
|
741
819
|
const migration = generateCreateMigration(blueprint, {
|
|
742
820
|
...options,
|
|
743
821
|
timestamp: offsetTimestamp
|
|
@@ -815,6 +893,7 @@ var TYPE_METHOD_MAP2 = {
|
|
|
815
893
|
LongText: "longText",
|
|
816
894
|
Date: "date",
|
|
817
895
|
Time: "time",
|
|
896
|
+
DateTime: "dateTime",
|
|
818
897
|
Timestamp: "timestamp",
|
|
819
898
|
Json: "json",
|
|
820
899
|
Email: "string",
|
|
@@ -1088,6 +1167,9 @@ function generateMigrationsFromChanges(changes, options = {}) {
|
|
|
1088
1167
|
var import_node_fs = require("fs");
|
|
1089
1168
|
var import_node_path = require("path");
|
|
1090
1169
|
|
|
1170
|
+
// src/model/generator.ts
|
|
1171
|
+
var import_omnify_types = require("@famgia/omnify-types");
|
|
1172
|
+
|
|
1091
1173
|
// src/utils.ts
|
|
1092
1174
|
function toSnakeCase(str) {
|
|
1093
1175
|
return str.replace(/([A-Z])/g, "_$1").replace(/^_/, "").toLowerCase();
|
|
@@ -1117,6 +1199,45 @@ var DEFAULT_OPTIONS = {
|
|
|
1117
1199
|
baseModelPath: "app/Models/OmnifyBase",
|
|
1118
1200
|
modelPath: "app/Models"
|
|
1119
1201
|
};
|
|
1202
|
+
function generateLocalizedDisplayNames(displayName, indent = " ") {
|
|
1203
|
+
if (displayName === void 0) {
|
|
1204
|
+
return "";
|
|
1205
|
+
}
|
|
1206
|
+
if (typeof displayName === "string") {
|
|
1207
|
+
return `${indent}'en' => '${escapePhpString(displayName)}',`;
|
|
1208
|
+
}
|
|
1209
|
+
if ((0, import_omnify_types.isLocaleMap)(displayName)) {
|
|
1210
|
+
const entries = Object.entries(displayName).map(([locale, value]) => `${indent}'${locale}' => '${escapePhpString(value)}',`).join("\n");
|
|
1211
|
+
return entries;
|
|
1212
|
+
}
|
|
1213
|
+
return "";
|
|
1214
|
+
}
|
|
1215
|
+
function generatePropertyLocalizedDisplayNames(schema, indent = " ") {
|
|
1216
|
+
const properties = schema.properties ?? {};
|
|
1217
|
+
const entries = [];
|
|
1218
|
+
for (const [propName, propDef] of Object.entries(properties)) {
|
|
1219
|
+
const snakeName = toSnakeCase(propName);
|
|
1220
|
+
const displayName = propDef.displayName;
|
|
1221
|
+
if (displayName === void 0) {
|
|
1222
|
+
continue;
|
|
1223
|
+
}
|
|
1224
|
+
const innerIndent = indent + " ";
|
|
1225
|
+
if (typeof displayName === "string") {
|
|
1226
|
+
entries.push(`${indent}'${snakeName}' => [
|
|
1227
|
+
${innerIndent}'en' => '${escapePhpString(displayName)}',
|
|
1228
|
+
${indent}],`);
|
|
1229
|
+
} else if ((0, import_omnify_types.isLocaleMap)(displayName)) {
|
|
1230
|
+
const localeEntries = Object.entries(displayName).map(([locale, value]) => `${innerIndent}'${locale}' => '${escapePhpString(value)}',`).join("\n");
|
|
1231
|
+
entries.push(`${indent}'${snakeName}' => [
|
|
1232
|
+
${localeEntries}
|
|
1233
|
+
${indent}],`);
|
|
1234
|
+
}
|
|
1235
|
+
}
|
|
1236
|
+
return entries.join("\n");
|
|
1237
|
+
}
|
|
1238
|
+
function escapePhpString(str) {
|
|
1239
|
+
return str.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
|
|
1240
|
+
}
|
|
1120
1241
|
function resolveOptions(options) {
|
|
1121
1242
|
return {
|
|
1122
1243
|
baseModelNamespace: options?.baseModelNamespace ?? DEFAULT_OPTIONS.baseModelNamespace,
|
|
@@ -1499,11 +1620,14 @@ use Illuminate\\Database\\Eloquent\\Relations\\MorphOne;
|
|
|
1499
1620
|
use Illuminate\\Database\\Eloquent\\Relations\\MorphMany;
|
|
1500
1621
|
use Illuminate\\Database\\Eloquent\\Relations\\MorphToMany;
|
|
1501
1622
|
use Illuminate\\Database\\Eloquent\\Collection as EloquentCollection;
|
|
1623
|
+
use {{BASE_MODEL_NAMESPACE}}\\Traits\\HasLocalizedDisplayName;
|
|
1624
|
+
use {{BASE_MODEL_NAMESPACE}}\\Locales\\{{CLASS_NAME}}Locales;
|
|
1502
1625
|
{{IMPORTS}}
|
|
1503
1626
|
|
|
1504
1627
|
{{DOC_COMMENT}}
|
|
1505
1628
|
class {{CLASS_NAME}}BaseModel extends {{BASE_MODEL_CLASS}}
|
|
1506
1629
|
{
|
|
1630
|
+
use HasLocalizedDisplayName;
|
|
1507
1631
|
{{TRAITS}}
|
|
1508
1632
|
/**
|
|
1509
1633
|
* The table associated with the model.
|
|
@@ -1522,6 +1646,20 @@ class {{CLASS_NAME}}BaseModel extends {{BASE_MODEL_CLASS}}
|
|
|
1522
1646
|
*/
|
|
1523
1647
|
public $timestamps = {{TIMESTAMPS}};
|
|
1524
1648
|
|
|
1649
|
+
/**
|
|
1650
|
+
* Localized display names for this model.
|
|
1651
|
+
*
|
|
1652
|
+
* @var array<string, string>
|
|
1653
|
+
*/
|
|
1654
|
+
protected static array $localizedDisplayNames = {{CLASS_NAME}}Locales::DISPLAY_NAMES;
|
|
1655
|
+
|
|
1656
|
+
/**
|
|
1657
|
+
* Localized display names for properties.
|
|
1658
|
+
*
|
|
1659
|
+
* @var array<string, array<string, string>>
|
|
1660
|
+
*/
|
|
1661
|
+
protected static array $localizedPropertyDisplayNames = {{CLASS_NAME}}Locales::PROPERTY_DISPLAY_NAMES;
|
|
1662
|
+
|
|
1525
1663
|
/**
|
|
1526
1664
|
* The attributes that are mass assignable.
|
|
1527
1665
|
*/
|
|
@@ -1578,12 +1716,15 @@ use Illuminate\\Database\\Eloquent\\Relations\\MorphMany;
|
|
|
1578
1716
|
use Illuminate\\Database\\Eloquent\\Relations\\MorphToMany;
|
|
1579
1717
|
use Illuminate\\Database\\Eloquent\\Collection as EloquentCollection;
|
|
1580
1718
|
use Illuminate\\Notifications\\Notifiable;
|
|
1719
|
+
use {{BASE_MODEL_NAMESPACE}}\\Traits\\HasLocalizedDisplayName;
|
|
1720
|
+
use {{BASE_MODEL_NAMESPACE}}\\Locales\\{{CLASS_NAME}}Locales;
|
|
1581
1721
|
{{IMPORTS}}
|
|
1582
1722
|
|
|
1583
1723
|
{{DOC_COMMENT}}
|
|
1584
1724
|
class {{CLASS_NAME}}BaseModel extends Authenticatable
|
|
1585
1725
|
{
|
|
1586
1726
|
use Notifiable;
|
|
1727
|
+
use HasLocalizedDisplayName;
|
|
1587
1728
|
{{TRAITS}}
|
|
1588
1729
|
/**
|
|
1589
1730
|
* The table associated with the model.
|
|
@@ -1602,6 +1743,20 @@ class {{CLASS_NAME}}BaseModel extends Authenticatable
|
|
|
1602
1743
|
*/
|
|
1603
1744
|
public $timestamps = {{TIMESTAMPS}};
|
|
1604
1745
|
|
|
1746
|
+
/**
|
|
1747
|
+
* Localized display names for this model.
|
|
1748
|
+
*
|
|
1749
|
+
* @var array<string, string>
|
|
1750
|
+
*/
|
|
1751
|
+
protected static array $localizedDisplayNames = {{CLASS_NAME}}Locales::DISPLAY_NAMES;
|
|
1752
|
+
|
|
1753
|
+
/**
|
|
1754
|
+
* Localized display names for properties.
|
|
1755
|
+
*
|
|
1756
|
+
* @var array<string, array<string, string>>
|
|
1757
|
+
*/
|
|
1758
|
+
protected static array $localizedPropertyDisplayNames = {{CLASS_NAME}}Locales::PROPERTY_DISPLAY_NAMES;
|
|
1759
|
+
|
|
1605
1760
|
/**
|
|
1606
1761
|
* The attributes that are mass assignable.
|
|
1607
1762
|
*/
|
|
@@ -1706,6 +1861,132 @@ class OmnifyServiceProvider extends ServiceProvider
|
|
|
1706
1861
|
]);
|
|
1707
1862
|
}
|
|
1708
1863
|
}
|
|
1864
|
+
`,
|
|
1865
|
+
"has-localized-display-name": `<?php
|
|
1866
|
+
|
|
1867
|
+
namespace {{BASE_MODEL_NAMESPACE}}\\Traits;
|
|
1868
|
+
|
|
1869
|
+
/**
|
|
1870
|
+
* Trait for localized display names.
|
|
1871
|
+
* Uses Laravel's app()->getLocale() for locale resolution.
|
|
1872
|
+
*
|
|
1873
|
+
* DO NOT EDIT - This file is auto-generated by Omnify.
|
|
1874
|
+
* Any changes will be overwritten on next generation.
|
|
1875
|
+
*
|
|
1876
|
+
* @generated by @famgia/omnify-laravel
|
|
1877
|
+
*/
|
|
1878
|
+
trait HasLocalizedDisplayName
|
|
1879
|
+
{
|
|
1880
|
+
/**
|
|
1881
|
+
* Get the localized display name for this model.
|
|
1882
|
+
*
|
|
1883
|
+
* @param string|null $locale Locale code (defaults to app locale)
|
|
1884
|
+
* @return string
|
|
1885
|
+
*/
|
|
1886
|
+
public static function displayName(?string $locale = null): string
|
|
1887
|
+
{
|
|
1888
|
+
$locale = $locale ?? app()->getLocale();
|
|
1889
|
+
$displayNames = static::$localizedDisplayNames ?? [];
|
|
1890
|
+
|
|
1891
|
+
return $displayNames[$locale]
|
|
1892
|
+
?? $displayNames[config('app.fallback_locale', 'en')]
|
|
1893
|
+
?? $displayNames[array_key_first($displayNames) ?? 'en']
|
|
1894
|
+
?? class_basename(static::class);
|
|
1895
|
+
}
|
|
1896
|
+
|
|
1897
|
+
/**
|
|
1898
|
+
* Get all localized display names for this model.
|
|
1899
|
+
*
|
|
1900
|
+
* @return array<string, string>
|
|
1901
|
+
*/
|
|
1902
|
+
public static function allDisplayNames(): array
|
|
1903
|
+
{
|
|
1904
|
+
return static::$localizedDisplayNames ?? [];
|
|
1905
|
+
}
|
|
1906
|
+
|
|
1907
|
+
/**
|
|
1908
|
+
* Get the localized display name for a property.
|
|
1909
|
+
*
|
|
1910
|
+
* @param string $property Property name
|
|
1911
|
+
* @param string|null $locale Locale code (defaults to app locale)
|
|
1912
|
+
* @return string
|
|
1913
|
+
*/
|
|
1914
|
+
public static function propertyDisplayName(string $property, ?string $locale = null): string
|
|
1915
|
+
{
|
|
1916
|
+
$locale = $locale ?? app()->getLocale();
|
|
1917
|
+
$displayNames = static::$localizedPropertyDisplayNames[$property] ?? [];
|
|
1918
|
+
|
|
1919
|
+
return $displayNames[$locale]
|
|
1920
|
+
?? $displayNames[config('app.fallback_locale', 'en')]
|
|
1921
|
+
?? $displayNames[array_key_first($displayNames) ?? 'en']
|
|
1922
|
+
?? $property;
|
|
1923
|
+
}
|
|
1924
|
+
|
|
1925
|
+
/**
|
|
1926
|
+
* Get all localized display names for a property.
|
|
1927
|
+
*
|
|
1928
|
+
* @param string $property Property name
|
|
1929
|
+
* @return array<string, string>
|
|
1930
|
+
*/
|
|
1931
|
+
public static function allPropertyDisplayNames(string $property): array
|
|
1932
|
+
{
|
|
1933
|
+
return static::$localizedPropertyDisplayNames[$property] ?? [];
|
|
1934
|
+
}
|
|
1935
|
+
|
|
1936
|
+
/**
|
|
1937
|
+
* Get all property display names for a given locale.
|
|
1938
|
+
*
|
|
1939
|
+
* @param string|null $locale Locale code (defaults to app locale)
|
|
1940
|
+
* @return array<string, string>
|
|
1941
|
+
*/
|
|
1942
|
+
public static function allPropertyDisplayNamesForLocale(?string $locale = null): array
|
|
1943
|
+
{
|
|
1944
|
+
$locale = $locale ?? app()->getLocale();
|
|
1945
|
+
$result = [];
|
|
1946
|
+
|
|
1947
|
+
foreach (static::$localizedPropertyDisplayNames ?? [] as $property => $displayNames) {
|
|
1948
|
+
$result[$property] = $displayNames[$locale]
|
|
1949
|
+
?? $displayNames[config('app.fallback_locale', 'en')]
|
|
1950
|
+
?? $displayNames[array_key_first($displayNames) ?? 'en']
|
|
1951
|
+
?? $property;
|
|
1952
|
+
}
|
|
1953
|
+
|
|
1954
|
+
return $result;
|
|
1955
|
+
}
|
|
1956
|
+
}
|
|
1957
|
+
`,
|
|
1958
|
+
"locales": `<?php
|
|
1959
|
+
|
|
1960
|
+
namespace {{BASE_MODEL_NAMESPACE}}\\Locales;
|
|
1961
|
+
|
|
1962
|
+
/**
|
|
1963
|
+
* Localized display names for {{CLASS_NAME}}.
|
|
1964
|
+
*
|
|
1965
|
+
* DO NOT EDIT - This file is auto-generated by Omnify.
|
|
1966
|
+
* Any changes will be overwritten on next generation.
|
|
1967
|
+
*
|
|
1968
|
+
* @generated by @famgia/omnify-laravel
|
|
1969
|
+
*/
|
|
1970
|
+
class {{CLASS_NAME}}Locales
|
|
1971
|
+
{
|
|
1972
|
+
/**
|
|
1973
|
+
* Localized display names for the model.
|
|
1974
|
+
*
|
|
1975
|
+
* @var array<string, string>
|
|
1976
|
+
*/
|
|
1977
|
+
public const DISPLAY_NAMES = [
|
|
1978
|
+
{{LOCALIZED_DISPLAY_NAMES}}
|
|
1979
|
+
];
|
|
1980
|
+
|
|
1981
|
+
/**
|
|
1982
|
+
* Localized display names for properties.
|
|
1983
|
+
*
|
|
1984
|
+
* @var array<string, array<string, string>>
|
|
1985
|
+
*/
|
|
1986
|
+
public const PROPERTY_DISPLAY_NAMES = [
|
|
1987
|
+
{{LOCALIZED_PROPERTY_DISPLAY_NAMES}}
|
|
1988
|
+
];
|
|
1989
|
+
}
|
|
1709
1990
|
`
|
|
1710
1991
|
};
|
|
1711
1992
|
return stubs[stubName] ?? "";
|
|
@@ -1725,15 +2006,42 @@ function generateServiceProvider(schemas, options, stubContent) {
|
|
|
1725
2006
|
schemaName: "__service_provider__"
|
|
1726
2007
|
};
|
|
1727
2008
|
}
|
|
2009
|
+
function generateLocalizedDisplayNameTrait(options, stubContent) {
|
|
2010
|
+
const content = stubContent.replace(/\{\{BASE_MODEL_NAMESPACE\}\}/g, options.baseModelNamespace);
|
|
2011
|
+
return {
|
|
2012
|
+
path: `${options.baseModelPath}/Traits/HasLocalizedDisplayName.php`,
|
|
2013
|
+
content,
|
|
2014
|
+
type: "trait",
|
|
2015
|
+
overwrite: true,
|
|
2016
|
+
// Always overwrite trait
|
|
2017
|
+
schemaName: "__trait__"
|
|
2018
|
+
};
|
|
2019
|
+
}
|
|
2020
|
+
function generateLocalesClass(schema, options, stubContent) {
|
|
2021
|
+
const className = toPascalCase(schema.name);
|
|
2022
|
+
const localizedDisplayNames = generateLocalizedDisplayNames(schema.displayName);
|
|
2023
|
+
const localizedPropertyDisplayNames = generatePropertyLocalizedDisplayNames(schema);
|
|
2024
|
+
const content = stubContent.replace(/\{\{BASE_MODEL_NAMESPACE\}\}/g, options.baseModelNamespace).replace(/\{\{CLASS_NAME\}\}/g, className).replace(/\{\{LOCALIZED_DISPLAY_NAMES\}\}/g, localizedDisplayNames).replace(/\{\{LOCALIZED_PROPERTY_DISPLAY_NAMES\}\}/g, localizedPropertyDisplayNames);
|
|
2025
|
+
return {
|
|
2026
|
+
path: `${options.baseModelPath}/Locales/${className}Locales.php`,
|
|
2027
|
+
content,
|
|
2028
|
+
type: "locales",
|
|
2029
|
+
overwrite: true,
|
|
2030
|
+
// Always overwrite locales
|
|
2031
|
+
schemaName: schema.name
|
|
2032
|
+
};
|
|
2033
|
+
}
|
|
1728
2034
|
function generateModels(schemas, options) {
|
|
1729
2035
|
const resolved = resolveOptions(options);
|
|
1730
2036
|
const models = [];
|
|
1731
2037
|
models.push(generateBaseModel(schemas, resolved, getStubContent("base-model")));
|
|
2038
|
+
models.push(generateLocalizedDisplayNameTrait(resolved, getStubContent("has-localized-display-name")));
|
|
1732
2039
|
models.push(generateServiceProvider(schemas, resolved, getStubContent("service-provider")));
|
|
1733
2040
|
for (const schema of Object.values(schemas)) {
|
|
1734
2041
|
if (schema.kind === "enum") {
|
|
1735
2042
|
continue;
|
|
1736
2043
|
}
|
|
2044
|
+
models.push(generateLocalesClass(schema, resolved, getStubContent("locales")));
|
|
1737
2045
|
models.push(generateEntityBaseModel(
|
|
1738
2046
|
schema,
|
|
1739
2047
|
schemas,
|
|
@@ -2099,6 +2407,24 @@ function getFactoryPath(factory) {
|
|
|
2099
2407
|
}
|
|
2100
2408
|
|
|
2101
2409
|
// src/plugin.ts
|
|
2410
|
+
function getExistingMigrationTables(migrationsDir) {
|
|
2411
|
+
const existingTables = /* @__PURE__ */ new Set();
|
|
2412
|
+
if (!(0, import_node_fs.existsSync)(migrationsDir)) {
|
|
2413
|
+
return existingTables;
|
|
2414
|
+
}
|
|
2415
|
+
try {
|
|
2416
|
+
const files = (0, import_node_fs.readdirSync)(migrationsDir);
|
|
2417
|
+
const createMigrationPattern = /^\d{4}_\d{2}_\d{2}_\d{6}_create_(.+)_table\.php$/;
|
|
2418
|
+
for (const file of files) {
|
|
2419
|
+
const match = file.match(createMigrationPattern);
|
|
2420
|
+
if (match) {
|
|
2421
|
+
existingTables.add(match[1]);
|
|
2422
|
+
}
|
|
2423
|
+
}
|
|
2424
|
+
} catch {
|
|
2425
|
+
}
|
|
2426
|
+
return existingTables;
|
|
2427
|
+
}
|
|
2102
2428
|
var LARAVEL_CONFIG_SCHEMA = {
|
|
2103
2429
|
fields: [
|
|
2104
2430
|
{
|
|
@@ -2182,18 +2508,81 @@ function laravelPlugin(options) {
|
|
|
2182
2508
|
generate: async (ctx) => {
|
|
2183
2509
|
const migrationOptions = {
|
|
2184
2510
|
connection: resolved.connection,
|
|
2185
|
-
timestamp: resolved.timestamp
|
|
2511
|
+
timestamp: resolved.timestamp,
|
|
2512
|
+
customTypes: ctx.customTypes
|
|
2186
2513
|
};
|
|
2187
|
-
const
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
tableName: migration.tables[0],
|
|
2194
|
-
migrationType: migration.type
|
|
2514
|
+
const outputs = [];
|
|
2515
|
+
const migrationsDir = (0, import_node_path.join)(ctx.cwd, resolved.migrationsPath);
|
|
2516
|
+
const existingTables = getExistingMigrationTables(migrationsDir);
|
|
2517
|
+
if (ctx.changes !== void 0) {
|
|
2518
|
+
if (ctx.changes.length === 0) {
|
|
2519
|
+
return outputs;
|
|
2195
2520
|
}
|
|
2196
|
-
|
|
2521
|
+
const addedSchemaNames = new Set(
|
|
2522
|
+
ctx.changes.filter((c) => c.changeType === "added").map((c) => c.schemaName)
|
|
2523
|
+
);
|
|
2524
|
+
if (addedSchemaNames.size > 0) {
|
|
2525
|
+
const addedSchemas = Object.fromEntries(
|
|
2526
|
+
Object.entries(ctx.schemas).filter(([name]) => addedSchemaNames.has(name))
|
|
2527
|
+
);
|
|
2528
|
+
const createMigrations = generateMigrations(addedSchemas, migrationOptions);
|
|
2529
|
+
for (const migration of createMigrations) {
|
|
2530
|
+
const tableName = migration.tables[0];
|
|
2531
|
+
if (existingTables.has(tableName)) {
|
|
2532
|
+
ctx.logger.debug(`Skipping CREATE for ${tableName} (already exists)`);
|
|
2533
|
+
continue;
|
|
2534
|
+
}
|
|
2535
|
+
outputs.push({
|
|
2536
|
+
path: getMigrationPath(migration, resolved.migrationsPath),
|
|
2537
|
+
content: migration.content,
|
|
2538
|
+
type: "migration",
|
|
2539
|
+
metadata: {
|
|
2540
|
+
tableName,
|
|
2541
|
+
migrationType: "create"
|
|
2542
|
+
}
|
|
2543
|
+
});
|
|
2544
|
+
}
|
|
2545
|
+
}
|
|
2546
|
+
const alterChanges = ctx.changes.filter(
|
|
2547
|
+
(c) => c.changeType === "modified" || c.changeType === "removed"
|
|
2548
|
+
);
|
|
2549
|
+
if (alterChanges.length > 0) {
|
|
2550
|
+
const alterMigrations = generateMigrationsFromChanges(
|
|
2551
|
+
alterChanges,
|
|
2552
|
+
migrationOptions
|
|
2553
|
+
);
|
|
2554
|
+
for (const migration of alterMigrations) {
|
|
2555
|
+
outputs.push({
|
|
2556
|
+
path: getMigrationPath(migration, resolved.migrationsPath),
|
|
2557
|
+
content: migration.content,
|
|
2558
|
+
type: "migration",
|
|
2559
|
+
metadata: {
|
|
2560
|
+
tableName: migration.tables[0],
|
|
2561
|
+
migrationType: migration.type
|
|
2562
|
+
}
|
|
2563
|
+
});
|
|
2564
|
+
}
|
|
2565
|
+
}
|
|
2566
|
+
} else {
|
|
2567
|
+
const migrations = generateMigrations(ctx.schemas, migrationOptions);
|
|
2568
|
+
for (const migration of migrations) {
|
|
2569
|
+
const tableName = migration.tables[0];
|
|
2570
|
+
if (migration.type === "create" && existingTables.has(tableName)) {
|
|
2571
|
+
ctx.logger.debug(`Skipping migration for ${tableName} (already exists)`);
|
|
2572
|
+
continue;
|
|
2573
|
+
}
|
|
2574
|
+
outputs.push({
|
|
2575
|
+
path: getMigrationPath(migration, resolved.migrationsPath),
|
|
2576
|
+
content: migration.content,
|
|
2577
|
+
type: "migration",
|
|
2578
|
+
metadata: {
|
|
2579
|
+
tableName,
|
|
2580
|
+
migrationType: migration.type
|
|
2581
|
+
}
|
|
2582
|
+
});
|
|
2583
|
+
}
|
|
2584
|
+
}
|
|
2585
|
+
return outputs;
|
|
2197
2586
|
}
|
|
2198
2587
|
};
|
|
2199
2588
|
const modelGenerator = {
|