nodejs-book icon indicating copy to clipboard operation
nodejs-book copied to clipboard

ts에 따른 수정사항

Open ZeroCho opened this issue 2 years ago • 0 comments

  res.locals.followerCount = req.user?.Followers?.length || 0;
  res.locals.followingCount = req.user?.Followings?.length || 0;
  res.locals.followingIdList = req.user?.Followings?.map(f => f.id) || [];
import Sequelize, {Model} from 'sequelize';
import Post from './post';

class User extends Model {
  id?: number;
  email?: string;
  nick?: string;
  password?: string;
  provider?: string;
  snsId?: string;
  createdAt?: Date;
  updatedAt?: Date;

  Followers?: User[];
  Followings?: User[];

  static associate() {
    User.hasMany(Post);
    User.belongsToMany(User, {
      foreignKey: 'followingId',
      as: 'Followers',
      through: 'Follow',
    });
    User.belongsToMany(User, {
      foreignKey: 'followerId',
      as: 'Followings',
      through: 'Follow',
    });
  }

  static initiate(sequelize: Sequelize.Sequelize) {

    User.init({
      email: {
        type: Sequelize.STRING(40),
        allowNull: true,
        unique: true,
      },
      nick: {
        type: Sequelize.STRING(15),
        allowNull: false,
      },
      password: {
        type: Sequelize.STRING(100),
        allowNull: true,
      },
      provider: {
        type: Sequelize.ENUM('local', 'kakao'),
        allowNull: false,
        defaultValue: 'local',
      },
      snsId: {
        type: Sequelize.STRING(30),
        allowNull: true,
      },
    }, {
      sequelize,
      timestamps: true,
      underscored: false,
      modelName: 'User',
      tableName: 'users',
      paranoid: true,
      charset: 'utf8',
      collate: 'utf8_general_ci',
    });
  }
}

export default User;

ZeroCho avatar Jun 27 '22 08:06 ZeroCho