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

Alternate model definition proposal #1010

Open
michan85 opened this issue May 13, 2021 · 1 comment
Open

Alternate model definition proposal #1010

michan85 opened this issue May 13, 2021 · 1 comment

Comments

@michan85
Copy link

Hello,

I managed to reduce a bit of boiler plate with a simple type definition:

export type ModelAttrs<T> = Omit<T,  keyof Model>

// example

// not required
export interface BaseModelAttrs{
  id: number
  createdAt: Date;
  updatedAt: Date;
}

export type ModelAttrs<T> = Omit<T,  keyof Model> & BaseModelAttrs

export default class BaseModel<T> extends Model<ModelAttrs<T>, Partial<ModelAttrs<T>>>{}

@Table
class Person extends BaseModel<Person> {
  @Column
  name!: string

  @Column
  birthday!: Date
}

this gives the proper auto complete when creating a new instance:

Screenshot 2021-05-13 at 11 50 01

without the attrs you get all the model props

Screenshot 2021-05-13 at 11 52 27

unfortunately this requires a base class to work

Screenshot 2021-05-13 at 11 53 54

Im not exactly sure how this could be best integrated into the library (if at all) but here are some options.
Im happy to pr them but just wanted to get your feelings first.

Options:

  • docs: just a some docs and people can copy and paste the snippets
  • TypedModel base class
export abstract class TypedModel<T,C=T> extends Model<ModelAttrs<T>,ModelAttrs<C>>{}
@ephys
Copy link
Member

ephys commented Apr 7, 2022

This has actually been improved recently in Sequelize, and can be used in sequelize-typescript too, see InferAttributes & InferCreationAttributes here https://sequelize.org/docs/v6/other-topics/typescript/

It should get even better in a future major release: sequelize/sequelize#14091

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