Initial commit
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
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;
|
||||
};
|
||||
Reference in New Issue
Block a user