'use strict'; /** * 用户表模型(users) * * 通用约定在 config/config.default.js 的 sequelize.define 里配置: * freezeTableName / underscored / created_at / updated_at, * 这里通过 paranoid: true 开启软删除(对应 deleted_at 字段)。 */ module.exports = (app) => { const { BIGINT, STRING, TINYINT, DATE } = app.Sequelize; const User = app.model.define('user', { id: { type: BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true, comment: '主键' }, username: { type: STRING(64), comment: '登录名' }, nickname: { type: STRING(64), allowNull: false, defaultValue: '', comment: '昵称' }, email: { type: STRING(128), comment: '邮箱(可选,可作登录账号)' }, avatar: { type: STRING(255), allowNull: false, defaultValue: '', comment: '头像地址' }, phone: { type: STRING(20), comment: '手机号' }, // 密码哈希(bcrypt),接口与模板都不外泄:查询时会显式 exclude passwordHash: { type: STRING(100), comment: '密码哈希(bcrypt)' }, gender: { type: TINYINT.UNSIGNED, allowNull: false, defaultValue: 0, comment: '性别:0未知 1男 2女' }, status: { type: TINYINT.UNSIGNED, allowNull: false, defaultValue: 1, comment: '状态:1正常 0禁用' }, remark: { type: STRING(255), comment: '备注' }, lastLoginAt: { type: DATE, comment: '最近登录时间' }, }, { tableName: 'users', paranoid: true, comment: '用户表', }); User.associate = function() { app.model.User.hasMany(app.model.WechatOauth, { foreignKey: 'user_id', as: 'wechats' }); app.model.User.hasMany(app.model.Scanner, { foreignKey: 'user_id', as: 'scanners' }); }; return User; };