Sequelize typescript

Creator
Creator
Seonglae ChoSeonglae Cho
Created
Created
2021 Dec 6 2:15
Editor
Edited
Edited
2023 Sep 13 12:33
Refs
Refs
import { Sequelize, Model, DataTypes, Optional } from "sequelize"; const sequelize = new Sequelize("mysql://root:asd123@localhost:3306/mydb"); // We recommend you declare an interface for the attributes, for stricter typechecking interface UserAttributes { id: number; name: string; } // Some fields are optional when calling UserModel.create() or UserModel.build() interface UserCreationAttributes extends Optional<UserAttributes, "id"> {} // We need to declare an interface for our model that is basically what our class would be interface UserInstance extends Model<UserAttributes, UserCreationAttributes>, UserAttributes {} const UserModel = sequelize.define<UserInstance>("User", { id: { primaryKey: true, type: DataTypes.INTEGER.UNSIGNED, }, name: { type: DataTypes.STRING, }, }); async function doStuff() { const instance = await UserModel.findByPk(1, { rejectOnEmpty: true, }); console.log(instance.id); }
 
 
 
 
 
 
 
 
 

Recommendations