@omnifyjp/ts 6.3.3 → 6.3.4

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.
@@ -62,6 +62,31 @@ function getIdType(options) {
62
62
  return id;
63
63
  return 'BigInt';
64
64
  }
65
+ /**
66
+ * TS type for a MorphTo `<name>_id` column. It was hardcoded to `number`, so a
67
+ * morph over Uuid or Ulid targets — whose ids are strings at runtime — was typed
68
+ * as a number for every consumer (#176, the TS half of the same wrong
69
+ * assumption that emitted `unsignedBigInteger` for an all-Ulid morph).
70
+ *
71
+ * `idType:` on the property wins, as it does on the migration side; otherwise
72
+ * the targets decide, and a set mixing numeric and textual keys is honestly
73
+ * `string | number`.
74
+ */
75
+ function morphIdTsType(property, allSchemas) {
76
+ const override = property.idType;
77
+ if (override)
78
+ return PK_TYPE_MAP[override] ?? 'number';
79
+ const types = new Set();
80
+ for (const target of property.targets ?? []) {
81
+ const schema = allSchemas[target];
82
+ if (!schema)
83
+ continue;
84
+ types.add(PK_TYPE_MAP[getIdType(schema.options)] ?? 'number');
85
+ }
86
+ if (types.size === 0)
87
+ return 'number';
88
+ return [...types].sort().join(' | ');
89
+ }
65
90
  /** Gets TypeScript type for a property. */
66
91
  /**
67
92
  * The interface name to write for a relation target, or `unknown` when the
@@ -193,7 +218,7 @@ export function propertyToTSProperties(propertyName, property, schema, allSchema
193
218
  },
194
219
  {
195
220
  name: `${propertyName}_id`,
196
- type: 'number',
221
+ type: morphIdTsType(property, allSchemas),
197
222
  optional: true,
198
223
  readonly: isReadonly,
199
224
  },
@@ -550,6 +550,29 @@ function buildImports(baseNamespace, modelName, hasSoftDelete, isAuthenticatable
550
550
  }
551
551
  return lines.join('\n') + '\n';
552
552
  }
553
+ /**
554
+ * PHP type for a MorphTo `<name>_id` column: `string` for Uuid/Ulid/String
555
+ * targets, `int` for integer ones, `int|string` when the targets disagree
556
+ * (#176). `idType:` on the property wins, matching the migration side.
557
+ */
558
+ function morphIdPhpType(prop, reader) {
559
+ const phpFor = (idType) => idType === 'Int' || idType === 'BigInt' ? 'int' : 'string';
560
+ const override = prop['idType'];
561
+ if (typeof override === 'string' && override !== '')
562
+ return phpFor(override);
563
+ const targets = prop['targets'] ?? [];
564
+ const kinds = new Set();
565
+ for (const target of targets) {
566
+ const schema = reader.getSchema(target);
567
+ if (!schema)
568
+ continue;
569
+ const id = schema.options?.id;
570
+ kinds.add(phpFor(typeof id === 'string' ? id : 'BigInt'));
571
+ }
572
+ if (kinds.size === 0)
573
+ return 'int';
574
+ return [...kinds].sort().join('|');
575
+ }
553
576
  function buildDocProperties(properties, expandedProperties, propertyOrder, reader, config) {
554
577
  const lines = [];
555
578
  // Mirror buildCasts: EnumRef columns are cast to their generated enum class,
@@ -580,7 +603,10 @@ function buildDocProperties(properties, expandedProperties, propertyOrder, reade
580
603
  const nullable = prop['nullable'] ?? true;
581
604
  const nullSuffix = nullable ? '|null' : '';
582
605
  lines.push(` * @property string${nullSuffix} $${morphName}_type`);
583
- lines.push(` * @property string${nullSuffix} $${morphName}_id`);
606
+ // The id column follows the targets' key types (#176) — documenting it
607
+ // as a string when the morph is over integer keys tells static analysis
608
+ // the opposite of what Eloquent returns.
609
+ lines.push(` * @property ${morphIdPhpType(prop, reader)}${nullSuffix} $${morphName}_id`);
584
610
  }
585
611
  continue;
586
612
  }
package/dist/types.d.ts CHANGED
@@ -335,6 +335,11 @@ export interface PropertyDefinition {
335
335
  * integer primary key WITHOUT it gets `$incrementing = false`.
336
336
  */
337
337
  readonly autoIncrement?: boolean;
338
+ /**
339
+ * Target primary key type for a relation's FK column. Honoured on MorphTo as
340
+ * the explicit override for the `<name>_id` column's type (#176).
341
+ */
342
+ readonly idType?: string;
338
343
  readonly precision?: number;
339
344
  readonly scale?: number;
340
345
  readonly pattern?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omnifyjp/ts",
3
- "version": "6.3.3",
3
+ "version": "6.3.4",
4
4
  "description": "TypeScript model type generator from Omnify schemas.json",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",