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

Model.create() Values autocomplete is missing? #858

Open
nandox5 opened this issue Dec 1, 2020 · 5 comments
Open

Model.create() Values autocomplete is missing? #858

nandox5 opened this issue Dec 1, 2020 · 5 comments

Comments

@nandox5
Copy link

nandox5 commented Dec 1, 2020

Am i missing something or is there no autocomplete on new Model({...}) ,Mode.create({...}) or Model.build({...})...

I added the columns to my Models as shown in the documentation, but when i go to create a Model and i start typing the Class members, they do not auto complete..

Shouldn't it work like this?

Image of example

This guy did the sequelize tyescript integration manually and it seems like a lot of dirty work to make it work. If this is not something integrated with this package, would it be hard to implement so that we get auto complete?

@CanTheAlmighty
Copy link

It's a bit awkward, since a while ago sequelize-typescript offered said completions (0.6 I think), then they removed when Sequelize@5 added official Typescript support (but without completions) and now Sequelize@6 has full completions (using a hidden property called _attributes).

So currently, the maintainer of this library seems to be inactive, and you get better support of types sticking to the official (ironically).

@abdelrazzaq-dev
Copy link

I had the same issue, and I solved it by creating a type for my model maybe you need two one for TModelAttributes and another for TCreationAttributes, and then do like this (I forgot how it calls in typescript 😅). then the completion works fine.

type User = {
  id: number;
  username: string;
 //...
}
type CUser = Omit<User, 'id'>

class User extends Model<User, CUser> {
 @Column
 //..
}

@CanTheAlmighty
Copy link

CanTheAlmighty commented Jan 15, 2021

@AbderrazzakB That's pretty much how sequelize@6 recommends it, and it properly fills the _attributes underlying property that feeds the autocompletion.

The only issue you'll run is when attempting to do relations, where the, for example, @BelongsToMany(() => Class) does not forward the _attributes information, and causes Typescript to throw a compilation error.

For now, I think the only solution is that, and cast to any to cover it up. For Example:

interface UserAttributes {
    id: string
    name: string
}

type UserCreationAttributes = Omit<UserAttributes, 'id'> // id is auto-generated.

@Table({ ... })
class User extends Model<UserAttributes, UserCreationAttributes> { // <-- we add the type info for Sequelize@6.
    @Column({ type: DataTypes.UUIDV4 })
    id!: string

    @Column({ type: DataTypes.TEXT, allowNull: true })
    name!: string | null

    // Here we cast to any so seq-typescript doesnt cry
    @BelongsToMany(() => Post as any, () => UserPost as any)
    readonly posts?: Array<Post>
}

You can check the types work in VSCode by doing:

type X = User['_attributes']['<autocomplete here>']

The value of _attributes should match the ones passed on the generic via UserAttributes

@nichita-pasecinic
Copy link

No way that the lib that calls itself *-typescript does not provide proper types for most basic Model methods.

That's sad!

@HectorMu
Copy link

No way, is there any another way to solving this? I think that I'm going for prisma

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

5 participants