The IDBAttribute
-
interface IDBAtribute {
readonly id: number;
readonly createdAt: Date;
readonly updatedAt: Date;
}
User attributes defined as IDBMoviesAttributes -
interface IDBMoviesAttributes extends IDBAttribute {
readonly title: string;
readonly description: string;
readonly category: string;
readonly release_date: number;
readonly movie_hour_length: number;
readonly movie_minute_length: number;
readonly image_path: string;
readonly video_path: string;
}
The User model is constructed below -
import { BuildOptions, DataTypes, Model, Sequelize } from "sequelize";
import { IDBUserAttributes } from "./shared/db-table";
interface UserModel extends Model<IDBUserAttributes>, IDBUserAttributes {}
class User extends Model<UserModel, IDBUserAttributes> {}
type UserStatic = typeof Model & {
new (values?: object, options?: BuildOptions): UserModel;
};
const UserFactory = (sequelize: Sequelize): UserStatic => {
return <UserStatic>sequelize.define("users", {
id: {
type: DataTypes.INTEGER.UNSIGNED,
autoIncrement: true,
primaryKey: true,
unique: true,
allowNull: false,
},
email: {
type: DataTypes.STRING(320),
allowNull: false,
unique: true,
},
username: {
type: DataTypes.STRING(26),
allowNull: false,
},
password: {
type: DataTypes.STRING(255),
allowNull: false,
},
createdAt: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
},
updatedAt: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
},
});
}
export {
UserModel,
User,
UserFactory,
UserStatic,
}
The code snippet demonstrates the usage of the User
model with the .create
method in sequelize -
User.create({
email: req.body.email,
username: req.body.username,
password: hashedPassword,
})
An error occurs due to a type mismatch between the input and expected properties -
Argument of type '{ email: string; username: string; password: string; }' is not assignable to parameter of type 'IDBUserAttributes'.
Type '{ email: string; username: string; password: string; }' is missing the following properties from type 'IDBUserAttributes': id, createdAt, updatedAtts(2345)
To resolve this issue without including the id, createdAt, and updatedAt fields, an alternative approach is needed. Is there a different way to utilize the User
model effectively?
How can I correctly define the model in this scenario?