@omnifyjp/ts 6.0.5 → 6.0.7

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.
@@ -17,7 +17,6 @@ export declare function toSnakeCase(str: string): string;
17
17
  * unconditionally produced `table_session_id_id` (issue #149).
18
18
  */
19
19
  export declare function toFkColumnName(propertyName: string): string;
20
- /** Gets TypeScript type for a property. */
21
20
  export declare function getPropertyType(property: PropertyDefinition, allSchemas: Record<string, SchemaDefinition>): string;
22
21
  /**
23
22
  * Convert a property to TSProperty array.
@@ -63,12 +63,29 @@ function getIdType(options) {
63
63
  return 'BigInt';
64
64
  }
65
65
  /** Gets TypeScript type for a property. */
66
+ /**
67
+ * The interface name to write for a relation target, or `unknown` when the
68
+ * target lives outside the generated schema set (external `targetNamespace`
69
+ * models). Imports are collected from the same schema set, so anything absent
70
+ * here would be referenced without ever being imported.
71
+ */
72
+ function resolveTargetName(target, allSchemas) {
73
+ if (!target || !(target in allSchemas)) {
74
+ return 'unknown';
75
+ }
76
+ return target;
77
+ }
66
78
  export function getPropertyType(property, allSchemas) {
67
79
  if (property.type === 'File') {
68
80
  return property.multiple ? 'OmnifyFile[]' : 'OmnifyFile | null';
69
81
  }
70
82
  if (property.type === 'Association') {
71
- const targetName = property.target ?? 'unknown';
83
+ // A target outside the schema set (an external model reached through
84
+ // `targetNamespace`, e.g. the app's own User) has no generated interface
85
+ // and therefore no import. Naming it anyway emits `user?: User` against an
86
+ // identifier nothing declares — TS2304 in every consumer. `unknown` is the
87
+ // honest type for a shape this generator cannot see.
88
+ const targetName = resolveTargetName(property.target, allSchemas);
72
89
  switch (property.relation) {
73
90
  case 'OneToOne':
74
91
  case 'ManyToOne':
@@ -78,7 +95,9 @@ export function getPropertyType(property, allSchemas) {
78
95
  return `${targetName}[]`;
79
96
  case 'MorphTo':
80
97
  if (property.targets && property.targets.length > 0) {
81
- return property.targets.join(' | ');
98
+ return property.targets
99
+ .map(target => resolveTargetName(target, allSchemas))
100
+ .join(' | ');
82
101
  }
83
102
  return 'unknown';
84
103
  case 'MorphOne':
@@ -162,7 +181,9 @@ export function propertyToTSProperties(propertyName, property, schema, allSchema
162
181
  // Handle Association: MorphTo — creates _type, _id, and relation properties
163
182
  if (property.type === 'Association' && property.relation === 'MorphTo' && property.targets && property.targets.length > 0) {
164
183
  const targetUnion = property.targets.map(t => `'${t}'`).join(' | ');
165
- const relationUnion = property.targets.join(' | ');
184
+ const relationUnion = property.targets
185
+ .map(target => resolveTargetName(target, allSchemas))
186
+ .join(' | ');
166
187
  return [
167
188
  {
168
189
  name: `${propertyName}_type`,
@@ -186,7 +207,7 @@ export function propertyToTSProperties(propertyName, property, schema, allSchema
186
207
  }
187
208
  // Handle Association: ManyToOne / OneToOne (owning side) — creates FK _id and relation
188
209
  if (property.type === 'Association' && (property.relation === 'ManyToOne' || property.relation === 'OneToOne') && !property.mappedBy) {
189
- const targetName = property.target ?? 'unknown';
210
+ const targetName = resolveTargetName(property.target, allSchemas);
190
211
  // Determine FK type from target schema
191
212
  let fkType = 'number';
192
213
  const targetSchema = property.target ? allSchemas[property.target] : undefined;
@@ -393,8 +393,15 @@ function buildImports(baseNamespace, modelName, hasSoftDelete, isAuthenticatable
393
393
  // not under per-module `Modules/File/Traits/`. The import statement
394
394
  // here must match, otherwise generated models crash with
395
395
  // `Trait "App\Omnify\Modules\File\Traits\HasFiles" not found`.
396
+ //
397
+ // The legacy fallback must use the GLOBAL `config.models.baseNamespace`,
398
+ // exactly like the writer in file-trait-generator.ts and like the audit
399
+ // trait below. The per-model `baseNamespace` argument carries the group
400
+ // (e.g. ...\Base\Space), which yielded `Base\Space\Traits\HasFiles`
401
+ // — a namespace nothing ever writes — so every model holding a File
402
+ // property fataled on load outside the modular structure.
396
403
  const fileTraitsNs = config
397
- ? resolveGlobalTraitNamespace(config, baseNamespace + '\\Traits')
404
+ ? resolveGlobalTraitNamespace(config, config.models.baseNamespace + '\\Traits')
398
405
  : (traitsNamespace || baseNamespace + '\\Traits');
399
406
  lines.push(`use ${fileTraitsNs}\\HasFiles;`);
400
407
  if (modelNamespace) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omnifyjp/ts",
3
- "version": "6.0.5",
3
+ "version": "6.0.7",
4
4
  "description": "TypeScript model type generator from Omnify schemas.json",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",