26 lines
769 B
JavaScript
26 lines
769 B
JavaScript
const { DataTypes } = require('sequelize');
|
|
|
|
module.exports = (sequelize) => {
|
|
const Friendship = sequelize.define('Friendship', {
|
|
id: {
|
|
type: DataTypes.INTEGER,
|
|
autoIncrement: true,
|
|
primaryKey: true
|
|
}
|
|
// Foreign keys 'user1Id' and 'user2Id' are added via associations
|
|
}, {
|
|
timestamps: false,
|
|
tableName: 'friendships',
|
|
hooks: {
|
|
// Ensure user1Id is always less than user2Id to guarantee
|
|
// a unique friendship pair regardless of creation order.
|
|
beforeValidate: (friendship) => {
|
|
if (friendship.user1Id > friendship.user2Id) {
|
|
[friendship.user1Id, friendship.user2Id] = [friendship.user2Id, friendship.user1Id];
|
|
}
|
|
}
|
|
}
|
|
});
|
|
return Friendship;
|
|
};
|