sequelize-typescript
sequelize-typescript copied to clipboard
Model.create() Values autocomplete is missing?
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?
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?
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).
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
//..
}
@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
No way that the lib that calls itself *-typescript
does not provide proper types for most basic Model methods.
That's sad!
No way, is there any another way to solving this? I think that I'm going for prisma