Skip to content
This repository was archived by the owner on Oct 10, 2022. It is now read-only.
This repository was archived by the owner on Oct 10, 2022. It is now read-only.

Add custom decorators (as extra templates) #229

@boenrobot

Description

@boenrobot

Right now, if I want to add anything to an entity that requires a decorator, I have to either edit the generated result, or inherit from the entity, only to redefine all affected properties.

This is not at all scalable. I think a better approach might be to enable the ability of custom additional handlerbars templates that would receive the item (entity, column, relation) being processed, and have their output inserted at the assigned spot (one before the generated decorators, one afer the generated decorators, one after the property/class)

That would allow custom decorators, as well as comments or even additional classes (instead of entity decorators), additional properties (instead of property decorators). There could be default empty templates, used when ones are not provided on the command line.

It may also be good to pass the handlebars instance to a file (similarly to the naming strategy), to allow definitions of additional handlebar helpers.

Use cases in point: nestjs CRUD, swagger and class validator. It would be much neater to define validation rules in such custom templates that would look at what the target entity/column is, and output the expected validation decorators. In my case, some of my validation rules can be applied based on the column options (e.g. max length, whether it is required, range of numeric columns, etc.), and others by my own convention (e.g. if I have DATE, DATETIME, INT, FLOAT or DECIMAL columns ending in "from" and "to" in the same table, then "from" must not be greater than "to", and "to" must not be less than "from"), which would make the DB model significantly scalable for such typical cases.

On an even more superficial level, adding TypeORM's @CreateDateColumn(), @UpdateDateColumn() and @VersionColumn decorators would also become possible, and be more flexible than an option about them.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementnew feature or bug with easy workaround

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions