sequelize / sequelize-typescript

Decorators and some other features for sequelize
MIT License
2.79k stars 282 forks source link

More useful possibility to define own method for model or instance #684

Closed frct1 closed 5 years ago

frct1 commented 5 years ago

Versions

I'm submitting a ...

[ ] bug report [x] feature request

Actual behavior:

Many options to create instance methods through a lot of hard to read code Expected behavior:

Defining instance methods inside model definition with decorator or something like that Creating own decorator from lib to define instance or model method like:

Related code:

@InstanceMethod
public doSomethingWithInstance(){}

@ModelMethod
public doSomethingWithModel(){}
RobinBuschmann commented 5 years ago

Hey @frct1, it's already possible to define model and instance methods easily. Just like in any normal class:

@Table 
class User extends Model<User> {
  instanceMethod() {}
  static modelMethod() {}
}

So no need for defining them in the table options like @Table({instanceMethods: {}, model...})

Does this solve your issue?

frct1 commented 5 years ago

Hello @RobinBuschmann WOOOOOOAH Incredible Got it, yes it solve the question Awesome! Thank you for answer Looked for this for a week, maybe more :D

frct1 commented 5 years ago

I think i can create PR for updating README.md about instance/model methods (currently in web a lot of creepy solutions. Actually i'm didn't test them, but look like)

If you don't mind it

RobinBuschmann commented 5 years ago

Thanks for asking, but it's already part of the sequelize documentation: https://sequelize.org/master/manual/models-definition.html#expansion-of-models.

frct1 commented 5 years ago

image

Oh, it's become to be more sad I missed it in official docs :(