Node.js Sequelize значение извлечения виртуального столбца из другой модели - PullRequest
0 голосов
/ 15 мая 2019

Я работаю с Sequelize 5.7, пытаюсь использовать виртуальный тип данных,
для извлечения связанной информации в модель.

Учитывая упрощенные модели company и user, как мне получить company.name
в user.companyname?

компания

  let Schema = sequelize.define(
    "company",
    {
      id: {
        type: DataTypes.INTEGER.UNSIGNED,
        autoIncrement: true,
        primaryKey: true
      },
      name: {
        type: DataTypes.STRING(45)
      }
    }
  );

пользователь

  let Schema = sequelize.define(
    "user",
    {
      id: {
        type: DataTypes.INTEGER.UNSIGNED,
        autoIncrement: true,
        primaryKey: true
      },
      login: {
        type: DataTypes.STRING(45),
        unique: true
      },
      company: {
          type: DataTypes.INTEGER.UNSIGNED,
          references: {
            model: sequelize.model('company'),
            key: 'id'
          }
      },

      /* This companyname contruct is pure fantasy, and the target of my question */

      companyname: {
        type: new DataTypes.VIRTUAL(DataTypes.STRING,['company']),
          references: {
            model: 'company',
            key: 'name'
          }
      }
    }
  );
...