Repository navigation
Conversation
2f6dfdc to
d60a4ee
Compare
Signed-off-by: Muhammad Aaqil <aaqilcs102@gmail.com>
d60a4ee to
a1e950f
Compare
samarpanB
left a comment
There was a problem hiding this comment.
Thanks for working on this. Preserving existing property metadata when converting properties into relations is a useful improvement, especially for belongsTo and referencesMany.
However, the implementation currently applies the same metadata-forwarding approach across relation decorators with different APIs, which can produce invalid generated TypeScript.
There are also concerns around decorator lookup, preservation of optionality and property types, and string-based metadata reconstruction.
I've left inline comments with the specific issues. I think we should address these and add targeted integration coverage before merging.
| if (decoratorArguments.length === 1) { | ||
| decoratorArguments.push('{}'); | ||
| } | ||
| decoratorArguments.push(`{${cleanedPropertyMetadata}}`); |
There was a problem hiding this comment.
I'm concerned about adding property metadata as a third argument here.
Unlike belongsTo and referencesMany, the hasMany and hasOne decorators only accept two arguments (targetResolver and definition). The same issue applies to hasManyThrough.
If the existing property has metadata, this change can generate TypeScript that doesn't compile.
Can we limit third-argument property metadata preservation to decorators that actually support it (belongsTo and referencesMany), rather than applying the same approach across all relation generators?
| const decorators = property.getDecorators(); | ||
| if (decorators.length === 0) return ''; | ||
|
|
||
| const firstArg = decorators[0].getArguments()[0]; |
There was a problem hiding this comment.
Shouldn't we explicitly look for the @property decorator instead of assuming the first decorator contains the property metadata?
Currently, decorators[0].getArguments()[0] could return an argument from another decorator, resulting in incorrect metadata being carried into the generated relation.
The existing doesRelationExist() helper also assumes decorator ordering, so we should make sure both parts of the conversion logic behave consistently.
Can we retrieve the @property decorator by name and handle cases where it doesn't exist?
| let isPropertyOptional = false; | ||
| if ( | ||
| modelPropertyMetadata && | ||
| modelPropertyMetadata.includes('nullable: true') |
There was a problem hiding this comment.
I don't think we should infer TypeScript optionality from the nullable: true string in the property metadata.
For example, an existing declaration such as companyId?: number may not contain nullable: true, but the generated relation property would lose the optional marker.
Similarly, database nullability and TypeScript optionality are not necessarily equivalent.
Can we preserve the original property's question token (?) and declared type directly from the AST, rather than deriving them from metadata strings? We should also reconsider stripping | undefined from the original type.
| if (modelPropertyMetadata && modelPropertyMetadata.trim() !== '') { | ||
| // Strip outer braces if modelPropertyMetadata is passed as a braced string like "{ nullable: true }" | ||
| cleanedPropertyMetadata = modelPropertyMetadata | ||
| .replace(/^\{|\}$/g, '') |
There was a problem hiding this comment.
This assumes that the existing @property() argument is always an inline object literal.
For example, if the model uses @property(companyIdsPropertyDefinition), stripping braces and wrapping the expression in a new object would change its meaning instead of preserving it.
Since we're already using ts-morph, can we preserve the original metadata expression directly or use AST-based handling instead of manipulating the expression as a string?
This would also avoid fragile behavior with more complex property definitions.
| name?: string; | ||
|
|
||
| @referencesMany(() => Account) | ||
| @referencesMany(() => Account, {}, {type: 'array', }) |
There was a problem hiding this comment.
The updated snapshots demonstrate that a third decorator argument is generated, but they don't establish that the metadata is correctly preserved or that the generated model remains valid.
Could we add integration tests covering:
- Existing
@property()with PostgreSQL-specific metadata (e.g.,dataType: 'jsonb') - Optional properties and union types
- Custom relation names and foreign keys
- Metadata defined through a variable rather than an inline object
- All affected relation generators
I'd also like to see TypeScript compilation validation of the generated models, particularly for hasMany and hasOne, where the decorator signatures don't support a third argument.
Ensure property decorator metadata (e.g., column configurations, nullability) is preserved and placed in the correct argument lot across relation generators (hasMany, hasOne, referencesMany, belongsTo).
Checklist
npm testpasses on your machinepackages/cliwere updatedexamples/*were updated👉 Check out how to submit a PR 👈