Skip to content

virtual attributes are always return by model #5566

@p2227

Description

@p2227
var pinyin = require('pinyin');

module.exports = function(sequelize, DataTypes) {
  var moneyType = sequelize.options.dialect === 'mssql' ? 'MONEY' : DataTypes.INTEGER;
  return sequelize.define('Pt', {
    usertype:{
      type:DataTypes.VIRTUAL,
      get(){
        return '0'
      }
    },
    pinyin:{
      type:DataTypes.VIRTUAL,
      scopes:['user'],
      get(){
        return pinyin(this.getDataValue('TX_Name'),{
          segment:false,
          style:pinyin.STYLE_NORMAL
        }).join('')
      }
    },
    ID_User: {
      primaryKey: true,
      type: DataTypes.STRING,
      allowNull: false
    },
    TX_Name: {
      type: DataTypes.STRING,
      allowNull: true
    },
    DT_Birth: {
      type: DataTypes.DATE,
      get:function(){
        var d = this.getDataValue('DT_Birth')
        return d ? d.toLocaleString() : d; 
      },
      allowNull: true
    },
    TX_IC: {
      type: DataTypes.STRING,
      allowNull: true
    },
    TX_IMG: {
      type: DataTypes.STRING,
      allowNull: true
    },
    TX_Location: {
      type: DataTypes.STRING,
      allowNull: true
    },
    TX_Signature: {
      type: DataTypes.STRING,
      allowNull: true
    },
    TX_Username: {
      type: DataTypes.STRING,
      allowNull: false
    },
    TX_Password: {
      type: DataTypes.STRING,
      allowNull: true
    },
    TX_Contact: {
      type: DataTypes.STRING,
      allowNull: true
    },
    TX_QQ: {
      type: DataTypes.STRING,
      allowNull: true
    },
    TX_WeChat: {
      type: DataTypes.STRING,
      allowNull: true
    },
    TX_QR: {
      type: DataTypes.STRING,
      allowNull: true
    },
    DT_Delivery: {
      type: DataTypes.DATE,
      get:function(){
        var d = this.getDataValue('DT_Delivery')
        return d ? d.toLocaleString() : d; 
      },
      allowNull: true
    },
    TX_Name2: {
      type: DataTypes.STRING,
      allowNull: true
    },
    TX_Contact2: {
      type: DataTypes.STRING,
      allowNull: true
    },
    MN_Credit: {
      type: moneyType,
      allowNull: true
    },
    MN_Deposit: {
      type: moneyType,
      allowNull: true
    },
  }, {
    defaultScope: {
      attributes: { exclude: ['TX_Password'] }
    },
    scopes:{
      'user':{
        attributes:['ID_User','TX_Name','DT_Birth','TX_IC','TX_IMG','TX_Location','TX_Signature','TX_Username','TX_Password','TX_QR','TX_Contact','TX_QQ','TX_WeChat']
      }
    },
    updatedAt:'DTTM_Updated',
    createdAt:'DTTM_Created',
    tableName: 'T_Pt',
    freezeTableName: true
  });
};

the attributes usertype and pinyin are always return where i findOne/findAll event i use model.scope('xxx')

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugDEPRECATED: replace with the "bug" issue type

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions