Initial commit
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
module.exports = (sequelize) => {
|
||||
const { User, Group, FriendRequest, Friendship } = sequelize.models;
|
||||
|
||||
// User <-> Group: Many-to-Many
|
||||
// A User can be in many Groups, and a Group can have many Users (members).
|
||||
const UserGroup = sequelize.define('UserGroup', {}, { timestamps: false });
|
||||
User.belongsToMany(Group, { through: UserGroup, foreignKey: 'userId' });
|
||||
Group.belongsToMany(User, { through: UserGroup, as: 'members', foreignKey: 'groupId' });
|
||||
|
||||
// Friendship is a direct link between two users.
|
||||
// user1Id will always be less than user2Id to ensure uniqueness.
|
||||
Friendship.belongsTo(User, { as: 'user1', foreignKey: 'user1Id' });
|
||||
Friendship.belongsTo(User, { as: 'user2', foreignKey: 'user2Id' });
|
||||
|
||||
// User <-> FriendRequest: One-to-Many
|
||||
// A User can send many friend requests and receive many friend requests.
|
||||
User.hasMany(FriendRequest, { as: 'sentRequests', foreignKey: 'fromUserId', onDelete: 'CASCADE' });
|
||||
User.hasMany(FriendRequest, { as: 'receivedRequests', foreignKey: 'toUserId', onDelete: 'CASCADE' });
|
||||
FriendRequest.belongsTo(User, { as: 'fromUser', foreignKey: 'fromUserId' });
|
||||
FriendRequest.belongsTo(User, { as: 'toUser', foreignKey: 'toUserId' });
|
||||
};
|
||||
Reference in New Issue
Block a user