@famgia/omnify-laravel 0.0.29 → 0.0.31

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.
@@ -3,8 +3,10 @@ import { readFileSync, existsSync, readdirSync } from "fs";
3
3
  import { join } from "path";
4
4
 
5
5
  // src/migration/schema-builder.ts
6
+ import { resolveLocalizedString } from "@famgia/omnify-types";
6
7
  var TYPE_METHOD_MAP = {
7
8
  String: "string",
9
+ TinyInt: "tinyInteger",
8
10
  Int: "integer",
9
11
  BigInt: "bigInteger",
10
12
  Float: "double",
@@ -19,7 +21,9 @@ var TYPE_METHOD_MAP = {
19
21
  Json: "json",
20
22
  Email: "string",
21
23
  Password: "string",
22
- Enum: "enum"
24
+ Enum: "enum",
25
+ EnumRef: "string"
26
+ // EnumRef stores the enum value as string (lookup via schema)
23
27
  // Note: File type is now polymorphic - no column generated, uses files table
24
28
  };
25
29
  var PK_METHOD_MAP = {
@@ -47,7 +51,7 @@ function toTableName(schemaName) {
47
51
  return snakeCase + "s";
48
52
  }
49
53
  }
50
- function propertyToColumnMethod(propertyName, property) {
54
+ function propertyToColumnMethod(propertyName, property, options = {}) {
51
55
  if (property.type === "Association") {
52
56
  return null;
53
57
  }
@@ -61,6 +65,8 @@ function propertyToColumnMethod(propertyName, property) {
61
65
  const propWithLength = property;
62
66
  if (method === "string" && propWithLength.length) {
63
67
  args.push(propWithLength.length);
68
+ } else if (property.type === "EnumRef") {
69
+ args.push(50);
64
70
  }
65
71
  if (property.type === "Decimal") {
66
72
  const decimalProp = property;
@@ -84,12 +90,15 @@ function propertyToColumnMethod(propertyName, property) {
84
90
  if (baseProp.default !== void 0 && baseProp.default !== null) {
85
91
  modifiers.push({ method: "default", args: [baseProp.default] });
86
92
  }
87
- if (baseProp.unsigned && (method === "integer" || method === "bigInteger")) {
93
+ if (baseProp.unsigned && (method === "tinyInteger" || method === "integer" || method === "bigInteger")) {
88
94
  modifiers.push({ method: "unsigned" });
89
95
  }
90
- const displayName = property.displayName;
91
- if (displayName) {
92
- modifiers.push({ method: "comment", args: [displayName] });
96
+ const rawDisplayName = property.displayName;
97
+ if (rawDisplayName) {
98
+ const displayName = resolveLocalizedString(rawDisplayName, options.locale);
99
+ if (displayName) {
100
+ modifiers.push({ method: "comment", args: [displayName] });
101
+ }
93
102
  }
94
103
  return {
95
104
  name: columnName,
@@ -195,7 +204,7 @@ function generatePolymorphicColumns(propertyName, property, allSchemas) {
195
204
  ];
196
205
  return { typeColumn, idColumn, indexes };
197
206
  }
198
- function generateForeignKey(propertyName, property, allSchemas) {
207
+ function generateForeignKey(propertyName, property, allSchemas, options = {}) {
199
208
  if (property.type !== "Association") {
200
209
  return null;
201
210
  }
@@ -226,7 +235,10 @@ function generateForeignKey(propertyName, property, allSchemas) {
226
235
  modifiers.push({ method: "default", args: [assocProp.default] });
227
236
  }
228
237
  if (assocProp.displayName) {
229
- modifiers.push({ method: "comment", args: [assocProp.displayName] });
238
+ const displayName = resolveLocalizedString(assocProp.displayName, options.locale);
239
+ if (displayName) {
240
+ modifiers.push({ method: "comment", args: [displayName] });
241
+ }
230
242
  }
231
243
  const column = {
232
244
  name: columnName,
@@ -247,7 +259,7 @@ function generateForeignKey(propertyName, property, allSchemas) {
247
259
  };
248
260
  return { column, foreignKey, index };
249
261
  }
250
- function expandCompoundType(propName, property, customTypes) {
262
+ function expandCompoundType(propName, property, customTypes, options = {}) {
251
263
  const typeDef = customTypes.get(property.type);
252
264
  if (!typeDef || !typeDef.compound || !typeDef.expand) {
253
265
  return null;
@@ -261,13 +273,26 @@ function expandCompoundType(propName, property, customTypes) {
261
273
  type: "String"
262
274
  // Default type, will be overridden by sql definition
263
275
  };
264
- if (field.sql) {
276
+ const fieldOverrides = baseProp.fields;
277
+ const fieldOverride = fieldOverrides?.[field.suffix];
278
+ const fieldWithEnumRef = field;
279
+ if (fieldWithEnumRef.enumRef) {
280
+ expandedProp.type = "EnumRef";
281
+ expandedProp.enum = fieldWithEnumRef.enumRef;
282
+ if (fieldOverride?.nullable !== void 0) {
283
+ expandedProp.nullable = fieldOverride.nullable;
284
+ } else if (baseProp.nullable !== void 0) {
285
+ expandedProp.nullable = baseProp.nullable;
286
+ }
287
+ } else if (field.sql) {
265
288
  const sqlType = field.sql.sqlType.toUpperCase();
266
289
  if (sqlType === "VARCHAR" || sqlType === "CHAR" || sqlType === "STRING") {
267
290
  expandedProp.type = "String";
268
291
  if (field.sql.length) {
269
292
  expandedProp.length = field.sql.length;
270
293
  }
294
+ } else if (sqlType === "TINYINT") {
295
+ expandedProp.type = "TinyInt";
271
296
  } else if (sqlType === "INT" || sqlType === "INTEGER") {
272
297
  expandedProp.type = "Int";
273
298
  } else if (sqlType === "BIGINT") {
@@ -285,17 +310,29 @@ function expandCompoundType(propName, property, customTypes) {
285
310
  } else if (sqlType === "TIMESTAMP" || sqlType === "DATETIME") {
286
311
  expandedProp.type = "Timestamp";
287
312
  }
313
+ if (field.sql.unsigned) {
314
+ expandedProp.unsigned = true;
315
+ }
316
+ if (field.sql.default !== void 0) {
317
+ expandedProp.default = field.sql.default;
318
+ }
288
319
  if (field.sql.nullable !== void 0) {
289
320
  expandedProp.nullable = field.sql.nullable;
290
321
  } else if (baseProp.nullable !== void 0) {
291
322
  expandedProp.nullable = baseProp.nullable;
292
323
  }
293
- if (field.sql.default !== void 0) {
294
- expandedProp.default = field.sql.default;
324
+ if (fieldOverride?.nullable !== void 0) {
325
+ expandedProp.nullable = fieldOverride.nullable;
295
326
  }
296
327
  }
297
328
  if (baseProp.displayName) {
298
- expandedProp.displayName = `${baseProp.displayName} (${field.suffix})`;
329
+ const resolvedDisplayName = resolveLocalizedString(
330
+ baseProp.displayName,
331
+ options.locale
332
+ );
333
+ if (resolvedDisplayName) {
334
+ expandedProp.displayName = `${resolvedDisplayName} (${field.suffix})`;
335
+ }
299
336
  }
300
337
  expanded.push({
301
338
  name: columnName,
@@ -305,7 +342,8 @@ function expandCompoundType(propName, property, customTypes) {
305
342
  return expanded;
306
343
  }
307
344
  function schemaToBlueprint(schema, allSchemas, options = {}) {
308
- const { customTypes = /* @__PURE__ */ new Map() } = options;
345
+ const { customTypes = /* @__PURE__ */ new Map(), locale } = options;
346
+ const columnOptions = { locale };
309
347
  const tableName = toTableName(schema.name);
310
348
  const columns = [];
311
349
  const foreignKeys = [];
@@ -316,21 +354,21 @@ function schemaToBlueprint(schema, allSchemas, options = {}) {
316
354
  }
317
355
  if (schema.properties) {
318
356
  for (const [propName, property] of Object.entries(schema.properties)) {
319
- const expandedProps = expandCompoundType(propName, property, customTypes);
357
+ const expandedProps = expandCompoundType(propName, property, customTypes, columnOptions);
320
358
  if (expandedProps) {
321
359
  for (const { name: expandedName, property: expandedProp } of expandedProps) {
322
- const columnMethod2 = propertyToColumnMethod(expandedName, expandedProp);
360
+ const columnMethod2 = propertyToColumnMethod(expandedName, expandedProp, columnOptions);
323
361
  if (columnMethod2) {
324
362
  columns.push(columnMethod2);
325
363
  }
326
364
  }
327
365
  continue;
328
366
  }
329
- const columnMethod = propertyToColumnMethod(propName, property);
367
+ const columnMethod = propertyToColumnMethod(propName, property, columnOptions);
330
368
  if (columnMethod) {
331
369
  columns.push(columnMethod);
332
370
  }
333
- const fkResult = generateForeignKey(propName, property, allSchemas);
371
+ const fkResult = generateForeignKey(propName, property, allSchemas, columnOptions);
334
372
  if (fkResult) {
335
373
  columns.push(fkResult.column);
336
374
  foreignKeys.push(fkResult.foreignKey);
@@ -773,7 +811,8 @@ function generateMigrations(schemas, options = {}) {
773
811
  const offsetTimestamp = incrementTimestamp(timestamp, timestampOffset);
774
812
  timestampOffset++;
775
813
  const blueprint = schemaToBlueprint(schema, schemas, {
776
- customTypes: options.customTypes
814
+ customTypes: options.customTypes,
815
+ locale: options.locale
777
816
  });
778
817
  const migration = generateCreateMigration(blueprint, {
779
818
  ...options,
@@ -1152,7 +1191,8 @@ var DEFAULT_OPTIONS = {
1152
1191
  modelNamespace: "App\\Models",
1153
1192
  baseModelClassName: "BaseModel",
1154
1193
  baseModelPath: "app/Models/OmnifyBase",
1155
- modelPath: "app/Models"
1194
+ modelPath: "app/Models",
1195
+ customTypes: /* @__PURE__ */ new Map()
1156
1196
  };
1157
1197
  function generateLocalizedDisplayNames(displayName, indent = " ") {
1158
1198
  if (displayName === void 0) {
@@ -1199,7 +1239,8 @@ function resolveOptions(options) {
1199
1239
  modelNamespace: options?.modelNamespace ?? DEFAULT_OPTIONS.modelNamespace,
1200
1240
  baseModelClassName: options?.baseModelClassName ?? DEFAULT_OPTIONS.baseModelClassName,
1201
1241
  baseModelPath: options?.baseModelPath ?? DEFAULT_OPTIONS.baseModelPath,
1202
- modelPath: options?.modelPath ?? DEFAULT_OPTIONS.modelPath
1242
+ modelPath: options?.modelPath ?? DEFAULT_OPTIONS.modelPath,
1243
+ customTypes: options?.customTypes ?? /* @__PURE__ */ new Map()
1203
1244
  };
1204
1245
  }
1205
1246
  function getCastType(propDef) {
@@ -1330,7 +1371,10 @@ function generateEntityBaseModel(schema, schemas, options, stubContent, authStub
1330
1371
  }
1331
1372
  }
1332
1373
  } else if (propDef.type === "Password") {
1333
- fillable.push(` '${snakeName}',`);
1374
+ const propWithFillable = propDef;
1375
+ if (propWithFillable.fillable !== false) {
1376
+ fillable.push(` '${snakeName}',`);
1377
+ }
1334
1378
  hidden.push(` '${snakeName}',`);
1335
1379
  const cast = getCastType(propDef);
1336
1380
  if (cast) {
@@ -1340,10 +1384,58 @@ function generateEntityBaseModel(schema, schemas, options, stubContent, authStub
1340
1384
  const relMethod = generateFileRelation(propName, propDef);
1341
1385
  relations.push(relMethod);
1342
1386
  } else {
1343
- fillable.push(` '${snakeName}',`);
1344
- const cast = getCastType(propDef);
1345
- if (cast) {
1346
- casts.push(` '${snakeName}' => '${cast}',`);
1387
+ const propWithOptions = propDef;
1388
+ const isFillable = propWithOptions.fillable !== false;
1389
+ const isHidden = propWithOptions.hidden === true;
1390
+ const typeDef = options.customTypes.get(propDef.type);
1391
+ const isCompoundType = typeDef?.compound && typeDef.expand;
1392
+ if (isCompoundType && typeDef.expand) {
1393
+ const fieldOverrides = propWithOptions.fields ?? {};
1394
+ for (const field of typeDef.expand) {
1395
+ const suffixSnake = toSnakeCase(field.suffix);
1396
+ const fieldName = `${snakeName}_${suffixSnake}`;
1397
+ const override = fieldOverrides[field.suffix];
1398
+ const fieldFillable = override?.fillable !== void 0 ? override.fillable : isFillable;
1399
+ if (fieldFillable) {
1400
+ fillable.push(` '${fieldName}',`);
1401
+ }
1402
+ const fieldHidden = override?.hidden !== void 0 ? override.hidden : isHidden;
1403
+ if (fieldHidden) {
1404
+ hidden.push(` '${fieldName}',`);
1405
+ }
1406
+ }
1407
+ } else {
1408
+ if (isFillable) {
1409
+ fillable.push(` '${snakeName}',`);
1410
+ }
1411
+ const cast = getCastType(propDef);
1412
+ if (cast) {
1413
+ casts.push(` '${snakeName}' => '${cast}',`);
1414
+ }
1415
+ if (isHidden) {
1416
+ hidden.push(` '${snakeName}',`);
1417
+ }
1418
+ }
1419
+ if (typeDef?.compound && typeDef.accessors) {
1420
+ for (const accessor of typeDef.accessors) {
1421
+ const accessorName = `${snakeName}_${toSnakeCase(accessor.name)}`;
1422
+ appends.push(` '${accessorName}',`);
1423
+ const methodName = toPascalCase(accessorName);
1424
+ const separator = accessor.separator ?? " ";
1425
+ const fieldRefs = accessor.fields.map((field) => {
1426
+ const fieldName = `${snakeName}_${toSnakeCase(field)}`;
1427
+ return `$this->${fieldName}`;
1428
+ });
1429
+ const accessorMethod = ` /**
1430
+ * Get the ${accessor.name.replace(/_/g, " ")} attribute.
1431
+ */
1432
+ public function get${methodName}Attribute(): ?string
1433
+ {
1434
+ $parts = array_filter([${fieldRefs.join(", ")}], fn($v) => $v !== null && $v !== '');
1435
+ return count($parts) > 0 ? implode('${separator}', $parts) : null;
1436
+ }`;
1437
+ relations.push(accessorMethod);
1438
+ }
1347
1439
  }
1348
1440
  }
1349
1441
  }
@@ -2550,7 +2642,8 @@ function laravelPlugin(options) {
2550
2642
  modelNamespace: resolved.modelNamespace,
2551
2643
  baseModelNamespace: resolved.baseModelNamespace,
2552
2644
  modelPath: resolved.modelsPath,
2553
- baseModelPath: resolved.baseModelsPath
2645
+ baseModelPath: resolved.baseModelsPath,
2646
+ customTypes: ctx.customTypes
2554
2647
  };
2555
2648
  const models = generateModels(ctx.schemas, modelOptions);
2556
2649
  const outputs = models.map((model) => ({
@@ -2668,4 +2761,4 @@ export {
2668
2761
  generateProviderRegistration,
2669
2762
  laravelPlugin
2670
2763
  };
2671
- //# sourceMappingURL=chunk-4LHIH7NX.js.map
2764
+ //# sourceMappingURL=chunk-BYMCYVUK.js.map