DefineNoAttributes.ts
754 Bytes
/**
* Keep this file in sync with the code in the "Usage of `sequelize.define`"
* that doesn't have attribute types in typescript.md
*/
import { Sequelize, Model, DataTypes } from 'sequelize';
const sequelize = new Sequelize('mysql://root:asd123@localhost:3306/mydb');
// We need to declare an interface for our model that is basically what our class would be
interface UserInstance extends Model {
id: number;
name: string;
}
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);
}