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

Use custom abstract class #288

Open
ivawzh opened this issue Jul 19, 2020 · 3 comments · Fixed by offsite-solutions/typeorm-model-generator#1
Open

Use custom abstract class #288

ivawzh opened this issue Jul 19, 2020 · 3 comments · Fixed by offsite-solutions/typeorm-model-generator#1
Labels
enhancement new feature or bug with easy workaround

Comments

@ivawzh
Copy link

ivawzh commented Jul 19, 2020

Hi, thanks for the great work!

I am wondering if it is possible to pass my own abstract class?

I am trying to implement something like ActiveModel's conventional ApplicationRecord. I.e. a base abstract class inherited by all entities.

I'd imagine it will be something similar to the existing activeRecord flag, but instead of boolean, it will let me define a path to my own abstract class.

E.g.

Engine.createModelFromDatabase(
  Engine.createDriver(ormconfig.type),
  {
    ...
  },
  {
    abstractClassPath: './src/custom-base-entity.ts',
  }
)
// at ./src/custom-base-entity.ts

import { BaseEntity } from "typeorm"

export default abstract class CustomBaseEntity extends BaseEntity {
  ...
}
@ivawzh ivawzh changed the title Use custom base class Use custom abstract class Jul 19, 2020
@Kononnable
Copy link
Owner

No, such functionality isn't implemented. The easiest way to achieve what you want would be to replace BaseEntity in your entities through your IDE(or make simple pipeline around typeorm-model-generator).
As for implementing such functionality inside this library - it wouldn't be that easy. You can use named export, or named export etc.

@ivawzh
Copy link
Author

ivawzh commented Aug 1, 2020

@Kononnable Thanks for the answer.
Is it possible to implement this feature?
I had a look at the source code, cannot quite understand how the .mst files are used.
Are they mustache templates? Can you please share a link to its doc?

@Kononnable
Copy link
Owner

I can mark this issue as an enhancement but I don't think it will be implemented any time soon.

.mst files are used by handlebars template engine. https://handlebarsjs.com/

@Kononnable Kononnable added enhancement new feature or bug with easy workaround and removed question labels Aug 1, 2020
ivawzh added a commit to ivawzh/typeorm-model-generator that referenced this issue Aug 10, 2020
The idea is to use the generated classes as abstract classes. Custom domain behaviors (i.e. methods, validations, and lifecycles) may be located in another class that inherits the generated model. This could benefit us from

1. avoiding polluting when running codegen after migration on existing codes.
1. have lean models without the boilerplates.
1. share common behaviors across multiple models.

3 new flags:

- `extendAbstractClass` - string. Similar to `activeRecord` that `extends BaseEntity`. Now it supports custom abstract class inheritance by passing a relative path as string to `extendAbstractClass`.
- `exportAbstractClass` - boolean. Export the generated models as an abstract class without `@Entity`
- `skipRelationships` - boolean. Skip generating relationship columns. Because currently there is a [bug from TypeORM failing at foreign key embedded columns in abstract classes](typeorm/typeorm#3132).

Close: Kononnable#288
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement new feature or bug with easy workaround
Projects
None yet
2 participants