sequelize-auto icon indicating copy to clipboard operation
sequelize-auto copied to clipboard

How do you extend the models created?

Open x11joe opened this issue 7 years ago • 9 comments

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 avatar Jan 20 '18 09:01 x11joe

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

x11joe avatar Jan 21 '18 07:01 x11joe

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?

rainabba avatar Apr 11 '18 22:04 rainabba

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

steveschmitt avatar Nov 19 '20 21:11 steveschmitt

@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.

jsau- avatar Feb 21 '21 22:02 jsau-

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 avatar Feb 23 '21 07:02 steveschmitt

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.

steveschmitt avatar Feb 26 '21 01:02 steveschmitt

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 )

amrsmind avatar Jan 31 '22 18:01 amrsmind

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 avatar Sep 06 '23 15:09 patricktyndall

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

patricktyndall avatar Sep 08 '23 16:09 patricktyndall