File: //home/arjun/projects/good-life-be/models/Questions.js
import { Sequelize, DataTypes } from 'sequelize';
import sequelize from '../config/sequelize-config.js';
const Question = sequelize.define(
'questions',
{
id: {
primaryKey: true,
type: DataTypes.INTEGER,
autoIncrement: true,
},
parent_id: {
type: DataTypes.INTEGER,
allowNull: true,
},
category_id: {
type: DataTypes.INTEGER,
allowNull: true,
},
subcategory_id: {
type: DataTypes.INTEGER,
allowNull: true,
},
question: {
type: DataTypes.TEXT,
allowNull: false,
},
answer: {
type: DataTypes.JSON,
allowNull: false,
},
answer_type: {
type: DataTypes.ENUM(
'text',
'dropdown',
'radio',
'checkbox',
'time_range',
'slider',
'time',
'duration',
'date',
'date_range',
'button'
),
},
createdAt: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
},
updatedAt: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
onUpdate: Sequelize.literal('CURRENT_TIMESTAMP'),
},
},
{
timestamps: true,
}
);
Question.associate = (models) => {
Question.belongsTo(models.questions, {
as: 'parentQuestion',
foreignKey: 'parent_id',
});
Question.hasMany(models.questions, {
as: 'childQuestions',
foreignKey: 'parent_id',
});
Question.belongsTo(models.sub_category, {
foreignKey: 'subcategory_id',
as: 'subcategory',
});
};
export default Question;