Files
Tasks/backend/models/UserBookmark.js
2025-11-12 23:06:27 +02:00

97 lines
2.1 KiB
JavaScript

/**
* UserBookmark Model
* Junction table for user-saved questions
*/
const { DataTypes } = require('sequelize');
const { v4: uuidv4 } = require('uuid');
module.exports = (sequelize) => {
const UserBookmark = sequelize.define('UserBookmark', {
id: {
type: DataTypes.UUID,
defaultValue: () => uuidv4(),
primaryKey: true,
allowNull: false,
comment: 'Primary key UUID'
},
userId: {
type: DataTypes.UUID,
allowNull: false,
references: {
model: 'users',
key: 'id'
},
onDelete: 'CASCADE',
comment: 'Reference to user who bookmarked'
},
questionId: {
type: DataTypes.UUID,
allowNull: false,
references: {
model: 'questions',
key: 'id'
},
onDelete: 'CASCADE',
comment: 'Reference to bookmarked question'
},
notes: {
type: DataTypes.TEXT,
allowNull: true,
comment: 'Optional notes about the bookmark'
},
createdAt: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
field: 'created_at',
comment: 'When the bookmark was created'
},
updatedAt: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: DataTypes.NOW,
field: 'updated_at',
comment: 'When the bookmark was last updated'
}
}, {
tableName: 'user_bookmarks',
timestamps: true,
underscored: true,
indexes: [
{
unique: true,
fields: ['user_id', 'question_id'],
name: 'idx_user_question_unique'
},
{
fields: ['user_id'],
name: 'idx_user_bookmarks_user'
},
{
fields: ['question_id'],
name: 'idx_user_bookmarks_question'
},
{
fields: ['bookmarked_at'],
name: 'idx_user_bookmarks_date'
}
]
});
// Define associations
UserBookmark.associate = function(models) {
UserBookmark.belongsTo(models.User, {
foreignKey: 'userId',
as: 'User'
});
UserBookmark.belongsTo(models.Question, {
foreignKey: 'questionId',
as: 'Question'
});
};
return UserBookmark;
};