Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do you extend the models created? #204

Open
x11joe opened this issue Jan 20, 2018 · 9 comments
Open

How do you extend the models created? #204

x11joe opened this issue Jan 20, 2018 · 9 comments

Comments

@x11joe
Copy link

x11joe commented Jan 20, 2018

Sometimes the models themselves have functions defined, but these get overwritten by the exporter. I was thinking an option to create automatic extend classes could be useful. This is done by the MySQLWorkBench converter for sequelize as well. Can this do something similar? If you have any good suggestions for this let me know.

@x11joe
Copy link
Author

x11joe commented Jan 21, 2018

Another issue is keeping the validations and not overwriting them. How do we do this?

@rainabba
Copy link
Contributor

I'm operating under the assumption that this is more to kick-start a models project rather than something you'd use after establishing existing models. You can control which tables are addressed so perhaps you need to use the exclude feature to ignore tables that have been established?

@steveschmitt
Copy link
Collaborator

Can you provide some example code, showing how you have extended the model files?

@jsau-
Copy link

jsau- commented Feb 21, 2021

@steveschmitt Some examples of things you might want to always add to models:
• custom validation methods (both attribute and model level)
• getters
• type replacements (in the case the library infers the wrong type for a column)
• virtual columns

I've been thinking about this a bit, and how it'd have to look. I can't really see a way to do it besides allowing users to pass in a JSON object or similar that describes replacements/addendums to make to generated models.

eg

{
  models: {
    model_one: {
      columns: {
        column_one: {
          getter?: string,
          type?: string,
          validate?: string,
        }
      },
      imports?: string[],
      validate?: string,
      virtualColumns?: string[]
    }
  }
}

I really can see the value in the concept (if it can be done right). It'd stop library consumers having to either manually patch models after re-generation, or writing their own scripts to do so after the fact.

I'd be happy to put a POC together for it if there's appetite and consensus on how it should be implemented.

@steveschmitt
Copy link
Collaborator

It seems like it could be done with mixins that the developer could apply to the models at run time, similar to the way that Sequelize adds methods to the models when we use hasMany or belongsTo.

@steveschmitt
Copy link
Collaborator

Another idea would be to allow the developer to write a base class that extends the Sequelize Model class, and the generator would use that as the base class for the generated models.

Right now, if you use --l ts for TypeScript output, you might get a class like:

export class Products extends Model<ProductsAttributes, ProductsCreationAttributes> implements ProductsAttributes {

We could change that to extend a developer-defined base class, like MyBaseModel:

export class Products extends MyBaseModel<ProductsAttributes, ProductsCreationAttributes> implements ProductsAttributes {

Or even put some wildcard so that each model gets its own base class, like Base*Model, where the * is replaced by the model name from the table:

export class Products extends BaseProductsModel<ProductsAttributes, ProductsCreationAttributes> implements ProductsAttributes {

It would work about the same for ES6 classes.

The developer would be responsible for writing the base class(es). The generator would have to write the import statements for them, from some known location.

@amrsmind
Copy link

To handle overwriting I used source version control like git ( develop or main branch has the original models )
If I want to auto-generate the models I will just do this on new branch then merge it into the master which will cause conflict at that moment you will be able to accept both changes ( changes happened by auto-sequelize - custom changes you made before )

@patricktyndall
Copy link

Anybody have a solution for this?

My project currently uses custom getters for:

  • coercing numeric cols to JS numbers
  • coercing timestamptz cols to unix seconds (JS number, instead of Date)
  • virtuals (though this seems like a different problem than conversion of an existing col)

Can anybody recommend an approach either within this lib, or on top of it (scripts etc)? Would love to be able to use this lib. TIA!

@patricktyndall
Copy link

Hi all -- wrote down some thoughts on a different approach here. would appreciate any insight!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants