@omnifyjp/omnify 1.3.3 → 2.0.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omnifyjp/omnify",
3
- "version": "1.3.3",
3
+ "version": "2.0.0",
4
4
  "description": "Schema-driven code generation for Laravel, TypeScript, and SQL",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -36,10 +36,10 @@
36
36
  "zod": "^3.24.0"
37
37
  },
38
38
  "optionalDependencies": {
39
- "@omnifyjp/omnify-darwin-arm64": "1.3.3",
40
- "@omnifyjp/omnify-darwin-x64": "1.3.3",
41
- "@omnifyjp/omnify-linux-x64": "1.3.3",
42
- "@omnifyjp/omnify-linux-arm64": "1.3.3",
43
- "@omnifyjp/omnify-win32-x64": "1.3.3"
39
+ "@omnifyjp/omnify-darwin-arm64": "2.0.0",
40
+ "@omnifyjp/omnify-darwin-x64": "2.0.0",
41
+ "@omnifyjp/omnify-linux-x64": "2.0.0",
42
+ "@omnifyjp/omnify-linux-arm64": "2.0.0",
43
+ "@omnifyjp/omnify-win32-x64": "2.0.0"
44
44
  }
45
45
  }
@@ -1,7 +1,7 @@
1
1
  /**
2
- * Generates the abstract BaseModel class.
2
+ * Generates the BaseModel class.
3
3
  * Morph map registration is handled exclusively by OmnifyServiceProvider.
4
4
  */
5
5
  import type { GeneratedFile, PhpConfig } from './types.js';
6
- /** Generate the abstract BaseModel class. */
6
+ /** Generate the BaseModel class. */
7
7
  export declare function generateBaseModel(config: PhpConfig): GeneratedFile[];
@@ -1,9 +1,9 @@
1
1
  /**
2
- * Generates the abstract BaseModel class.
2
+ * Generates the BaseModel class.
3
3
  * Morph map registration is handled exclusively by OmnifyServiceProvider.
4
4
  */
5
5
  import { baseFile } from './types.js';
6
- /** Generate the abstract BaseModel class. */
6
+ /** Generate the BaseModel class. */
7
7
  export function generateBaseModel(config) {
8
8
  const baseNamespace = config.models.baseNamespace;
9
9
  const content = `<?php
@@ -21,7 +21,7 @@ namespace ${baseNamespace};
21
21
 
22
22
  use Illuminate\\Database\\Eloquent\\Model;
23
23
 
24
- abstract class BaseModel extends Model
24
+ class BaseModel extends Model
25
25
  {
26
26
  }
27
27
  `;
@@ -62,7 +62,7 @@ function generateBaseModel(name, schema, reader, config) {
62
62
  const fillable = buildFillable(properties, expandedProperties, propertyOrder);
63
63
  const hidden = buildHidden(properties, expandedProperties, propertyOrder);
64
64
  const appends = buildAppends(expandedProperties);
65
- const casts = buildCasts(properties, expandedProperties, propertyOrder);
65
+ const casts = buildCasts(properties, expandedProperties, propertyOrder, reader);
66
66
  const relations = buildRelations(name, properties, propertyOrder, modelNamespace, reader);
67
67
  const accessors = buildAccessors(expandedProperties);
68
68
  const nestedSetMethod = buildNestedSetParentIdMethod(nestedSetParentColumn);
@@ -384,15 +384,28 @@ function buildAppends(expandedProperties) {
384
384
  return '';
385
385
  return appends.map(a => ` '${a}',`).join('\n') + '\n';
386
386
  }
387
- function buildCasts(properties, expandedProperties, propertyOrder) {
387
+ function buildCasts(properties, expandedProperties, propertyOrder, reader) {
388
388
  const casts = [];
389
389
  for (const propName of propertyOrder) {
390
390
  const prop = properties[propName];
391
391
  if (!prop)
392
392
  continue;
393
393
  const type = prop['type'] ?? 'String';
394
- if (type === 'Association')
394
+ if (type === 'Association') {
395
+ const relation = prop['relation'] ?? '';
396
+ if (relation === 'ManyToOne') {
397
+ const target = prop['target'] ?? '';
398
+ const targetSchema = reader.getSchema(target);
399
+ const targetIdType = typeof targetSchema?.options?.id === 'string'
400
+ ? targetSchema.options.id
401
+ : 'BigInt';
402
+ if (targetIdType !== 'Uuid' && targetIdType !== 'Ulid') {
403
+ const snakeName = toSnakeCase(propName);
404
+ casts.push(`'${snakeName}_id' => 'integer',`);
405
+ }
406
+ }
395
407
  continue;
408
+ }
396
409
  if (expandedProperties[propName]) {
397
410
  for (const col of expandedProperties[propName].columns ?? []) {
398
411
  const colType = col.type ?? 'String';