Sequelize use camel case in JS but underscores in table names

前端 未结 4 1180
暖寄归人
暖寄归人 2020-12-29 02:36

Is it possible to have column names be underscored (postgres) but have the JavaScript getters be camelCase per language standards?

4条回答
  •  鱼传尺愫
    2020-12-29 03:08

    You can achieve this for both models (tables) and keys (fields) using a newer version of Sequelize.

    I am using 4.29.2 and my models looks like this:

    const Post = sequelize.define('post', {
        id: {
          type: DataTypes.UUID,
          defaultValue: DataTypes.UUIDV4,
          primaryKey: true,
          allowNull: false,
        },
        isActive: {
          type: DataTypes.BOOLEAN,
          defaultValue: true,
          allowNull: false,
          field: 'is_active',
        },
        isDeleted: {
          type: DataTypes.BOOLEAN,
          defaultValue: false,
          allowNull: false,
          field: 'is_deleted',
        },
      }, {
        indexes: [
          {
            unique: false,
            fields: ['is_active'],
          },
          {
            unique: false,
            fields: ['is_deleted'],
          },
        ],
        defaultScope: {
          where: {
            isActive: true,
            isDeleted: false,
          },
        },
      });
    
    const PostComments = sequelize.define('postComments', {
        id: {
          type: DataTypes.UUID,
          defaultValue: DataTypes.UUIDV4,
          primaryKey: true,
          allowNull: false,
        },
        postId: {
          type: DataTypes.UUID,
          allowNull: false,
          field: 'post_id',
        },
        comment: {
          type: DataTypes.STRING,
          allowNull: false,
        },
      }, {
        tableName: 'post_comments',
        indexes: [
          {
            unique: false,
            fields: ['post_id'],
          },
        ],
      });
    
    
      Post.hasMany(PostComments, {
        foreignKey: 'postId',
        constraints: true,
        as: 'comments',
      });
    
      PostComments.belongsTo(Post, {
        foreignKey: 'postId',
        constraints: true,
        as: 'post',
      });
    

    As you can see, I'm setting the tableName value for models (tables) and field values for keys (fields).

    When I run:

    sequelize.query('SET FOREIGN_KEY_CHECKS = 0', { raw: true })
      .then(() => {
        conn.sync({ force: true }).then(() => {
          console.log('DONE');
        });
      });
    

    The result is:

    Executing (default): SET FOREIGN_KEY_CHECKS = 0
    Executing (default): DROP TABLE IF EXISTS `post_comments`;
    Executing (default): DROP TABLE IF EXISTS `post`;
    Executing (default): CREATE TABLE IF NOT EXISTS `post` (`id` CHAR(36) BINARY NOT NULL , `is_active` TINYINT(1) NOT NULL DEFAULT true, `is_deleted` TINYINT(1) NOT NULL DEFAULT false, `created_at` DATETIME NOT NULL, `updated_at` DATETIME NOT NULL, PRIMARY KEY (`id`) ENGINE=InnoDB;
    Executing (default): CREATE TABLE IF NOT EXISTS `post_comments` (`id` CHAR(36) BINARY NOT NULL , `post_id` CHAR(36) BINARY NOT NULL, `comment` VARCHAR(255) NOT NULL, `created_at` DATETIME NOT NULL, `updated_at` DATETIME NOT NULL, PRIMARY KEY (`id`), FOREIGN KEY (`post_id`) REFERENCES `post` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE=InnoDB;
    

提交回复
热议问题