【问题标题】:How to return the result set of sequelize query in camel case fashion?如何以驼峰式方式返回 sequelize 查询的结果集?
【发布时间】:2019-03-11 05:55:11
【问题描述】:

我已在数据库中以下划线方式定义了一个表架构,但我想以驼峰式方式返回结果集 API 响应。我知道我可以处理 sequelize 返回的下划线对象并将其转换为驼峰式样式。是否有任何功能可以在 sequelize 本身中以驼峰方式返回查询的响应?

【问题讨论】:

  • 你能分享你的模型spinet或例子吗?

标签: node.js express sequelize.js


【解决方案1】:

要存档,您需要在定义模型时使用field

module.exports = (sequelize, DataTypes) => {
  const yourTable = sequelize.define('yourTable', { // table name use it for Sequelize
    camelCase: { //camelCase name that you'll use with sequelize.
      field: 'under_score', //underscore name on yor database.
      type: DataTypes.STRING
    },
    keyId: { //need to the same with association 
      field: 'key_id',
      type: DataTypes.INTEGER
    },
  }, {
    tableName: 'your_table', // then name of the table on the db
    underscored: true,
  });

  yourTable.associate = (models) => {
    yourTable.belongsTo(models.otherTable, {   
      as: 'Something',   
      foreignKey: 'key_id', //put attention here and keyId above.
      onDelete: 'cascade'
    }); 
  }
}

【讨论】:

    猜你喜欢
    • 2019-04-16
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    • 1970-01-01
    • 2019-01-16
    • 1970-01-01
    • 1970-01-01
    • 2010-12-09
    相关资源
    最近更新 更多