sequelize / sequelize-typescript

Decorators and some other features for sequelize
MIT License
2.79k stars 280 forks source link

Typescript JSONB datatype on a model? #644

Closed hcabnettek closed 5 years ago

hcabnettek commented 5 years ago

Trying to type a model. How can I use JSONB sequelize postgres type?

Keep in mind sequelize-typescript does not provide typings for sequelize - these are seperate things. A lot of the types in sequelize-typescript augment, refer to, or extend what sequelize already has.

`@Table({ modelName: "Userprefs", tableName: "userprefs" }) export default class UserPrefs extends Model {

@PrimaryKey @Column({ field: "id", autoIncrement: true }) id!: number;

@Column({ field: "docid" }) docId!: string;

@CreatedAt @Column({ field: "created_on" }) createdOn!: Date;

@Column({ field: "prefs" }) prefs!: DataTypes.JSONB;

}`

It doesn't like how I'm referring to JSONB. Can anyone help?

oldmud0 commented 5 years ago
@Column(DataType.JSONB)
prefs!: object;

Naturally, prefs might want to follow a certain structure rather than an arbitrary object (meaning, use a more specific type).

You may also be interested in the underscored option for @Table.

hcabnettek commented 5 years ago

@oldmud0 I am definitely interested in the underscored option. Where can I find more info?

oldmud0 commented 5 years ago

See http://docs.sequelizejs.com/manual/models-definition.html#configuration:


  // Will automatically set field option for all attributes to snake cased name.
  // Does not override attribute with field option already defined
  underscored: true,
RobinBuschmann commented 5 years ago

Closing this since it's solved

hcabnettek commented 5 years ago

Thanks!