Sequelize - отображать поля на псевдоним поля в определении модели - PullRequest
0 голосов
/ 13 июня 2019

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

Можно ли сопоставить имена полей базы данных с псевдонимами в определении модели, чтобы у моей службы было более удобных для разработчиков имен свойств модели для работы?

Пример:

Это ...

// Horrible field names
module.exports = (sequelize, DataTypes) =>
  sequelize.define('Transaction', {
    f_curr_finaccount__amount: DataTypes.DECIMAL,
    f_curr_finaccount__tx_type: DataTypes.STRING,
    f_finaccount__currency_iso_id: DataTypes.STRING,
    f_lex_finaccount__tx_atomic_status: DataTypes.STRING
  }, {
    schema: 'fins',
    tableName: 'fins_financialaccounttransaction',
    timestamps: false
  })

... становится ...

// Developer-friendly field names
module.exports = (sequelize, DataTypes) =>
  sequelize.define('Transaction', {
    amount: {
      type: DataTypes.DECIMAL,
      fieldName: 'f_curr_finaccount__amount'
    },
    type: {
      type: DataTypes.STRING,
      fieldName: 'f_curr_finaccount__tx_type'
    },
    currency: {
      type: DataTypes.STRING,
      fieldName: 'f_finaccount__currency_iso_id'
    },
    status: {
      type: DataTypes.STRING,
      fieldName: 'f_lex_finaccount__tx_atomic_status'
    }
  }, {
    schema: 'fins',
    tableName: 'fins_financialaccounttransaction',
    timestamps: false
  })

1 Ответ

1 голос
/ 13 июня 2019

Точно так же, как вы, но имя атрибута просто field.Он будет одинаковым для всех столбцов, включая внешние ключи.

// Developer-friendly field names
module.exports = (sequelize, DataTypes) =>
  sequelize.define('Transaction', {
    amount: {
      type: DataTypes.DECIMAL,
      field: 'f_curr_finaccount__amount'
    },
    type: {
      type: DataTypes.STRING,
      field: 'f_curr_finaccount__tx_type'
    },
    currency: {
      type: DataTypes.STRING,
      field: 'f_finaccount__currency_iso_id'
    },
    status: {
      type: DataTypes.STRING,
      field: 'f_lex_finaccount__tx_atomic_status'
    }
  }, {
    schema: 'fins',
    tableName: 'fins_financialaccounttransaction',
    timestamps: false
  })
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...