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

Allow for Better Inheritance and Extendability of Generated Entity Models #1373

Closed
filo87 opened this issue Oct 28, 2022 · 5 comments
Closed

Comments

@filo87
Copy link
Contributor

filo87 commented Oct 28, 2022

It would be great to be able to extend the definitions of generated models (from yarn codegen) and instantiate sub-classes with enhanced functionalities and logic by still maintaining the methods of the original parent Entity (like save(), get(), ...).

As an example, given a generated model Entity Account it should be possible to do something like this:

// this is the model generated from yarn codegen
class Account implements Entity {
...
}

// this is a child class extending Account somewhere else in the code
class AccountService extends Account {

async doStuff() {
      const service = await this.get('xyz') // <- Should return an instance of AccountService and NOT of Account
   }
}

for this purpose, all static methods returning an instance of the model Entity class should call the constructor and their own static methods as this and NOT as className.

here some examples:

return <%=props.className %>.create(record as <%= props.className %>Props);

let entity = new <%=props.className %>(record.id);

@filo87 filo87 changed the title Use this in static methods instead of className to enable for better inheritance and extendability of Models Allow for better inheritance and extendability of Generated Entity Models Oct 28, 2022
@filo87 filo87 changed the title Allow for better inheritance and extendability of Generated Entity Models Allow for Better Inheritance and Extendability of Generated Entity Models Oct 28, 2022
@jamesbayly
Copy link
Contributor

Interesting idea, our models are autogenerated so we would need to make sure that we don't overwrite the extended definitions

@stwiname
Copy link
Collaborator

stwiname commented Nov 1, 2022

@filo87, it seems like you have a good understanding of this problem. If you would like to make a PR we would happily add the feature.

@filo87
Copy link
Contributor Author

filo87 commented Nov 1, 2022

Ok great will prepare a PR for this

@filo87
Copy link
Contributor Author

filo87 commented Nov 2, 2022

@stwiname Here the PR: #1377

Not much to change, only in the ejs model template:

  • The .create() static method should use this as reference so that when called from a child class it returns an instance of the child (with all methods in the prototype).
  • All getters should also be adapted to call the this.create() method
  • The entityProps type should be exported so it can be used in the child classes when using the .create() method

this should already do the job!

I quickly tried the template in my project and was OK...what additional testing is required?

@jiqiang90
Copy link
Contributor

Close this now, pr merged

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

4 participants