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

Very general feedback (or why using template engines would be a better implementation) #40

Open
hotaru355 opened this issue Oct 11, 2020 · 1 comment

Comments

@hotaru355
Copy link

Hi!

Fist of all, thank you for creating this repo! This is definitely more useful that the stale sequelize-auto repo out there.
Unfortunately, it does not fully fit my bill. To give you some background: my project has about 300 MySql tables, with tables relating to each other in many-to-one and many-to-many relationships and the code is purely in typescript using sequelize version 6 (!). Using your very nicely written (:clap:) module gets me maybe 80% to were I would like to be, but unfortunately it lacks some flexibility. Here a short list:

  • sequelize 6 is not supported. For example, DataTypes.INTEGER(10) is DataTypes.INTEGER({ length: 10}) in V6
  • since I have so many tables, I would need the associations being generated as well.
  • .d.ts files are actually required for JS modules that can be consumed by typescript code. If the code is already in TS, the interface definitions would go directly into the .ts files
  • the models are lacking the generic type. sequelize.define(..) returns a model with its internal type being any. A slightly better solution would be to pass in the generics that are generated in the .d.ts files, so sequelize.define<MyModelAttributes,>(..), BUT ...
  • sequelize.define() is one of the two ways you can define a model, but since I want to add custom functions to my models, I actually want to have a class on which I would call init:
// I have timestamps on most of my tables, so I have an interface for the timestamp fields
export interface UserAttributes extends CreateableModel, ModifiableModel {
  userId: number
  name: string
}

// Your code is aware of optional fields, since it puts a "?" on those in the interface
// It would be nice if it would create this second interface for sequelize
export interface UserCreationAttributes extends Optional<UserAttributes, 'userId'> {}

// this is how I need the initialisation to work: create a class and call init() on it:
class User extends Model<UserAttributes, UserCreationAttributes> implements UserAttributes {
  public userId!: number
  public name!: string
  public created!: Date
  public modified!: Date

  public static function myCustomStaticFunc() {}
  public function myCustomInstanceFunc() {}
}

// small improvement: sequelize provides a wrapper to create an interface for the static parts of the model.
// Important though, it needs to be exported, since this is what I need everywhere in the code
export type UserModel = ModelCtor<User>

// the initialiser that is called on app startup 
export default (sequelize: Sequelize) => {
  const attributes = {...}
  const options = {...} // unfortunately, your script does not give my any control over these options like the `timestamp` for example
  User.init(attributes, options)
}

Long story short, I would need some tweaks that are currently not supported. But instead of adding support for a lot of flags and switches to the sequelize-automate.config.json, I instead propose the following:

Why not use a templating engine like handlebars or EJS internally and allow the user to provide their own templates!

I believe this would give users the biggest flexibility, would be future-proof for new sequelize versions and any other changes, since it would just involve creating a new template to use.

What do you think ?! (sorry for my length explanations, I hope I did not waste your time)

@nodejh
Copy link
Owner

nodejh commented Oct 12, 2020

Hi @hotaru355 , thanks for your feedback, they are very valuable.

  • sequelize 6 is not supported. Yes.
  • associations is a bit diffcult, so it's not support now.
  • any type, there are something wrong with sequelize 5 types, see also Problems when trying to use TypeScript #11 , I will check in sequelize 6.
  • templating engine is a good idea.

I will fix these issues, but may take a while.

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

2 participants