Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I do a group by across 3 tables with Sequelize?

Tags:

sequelize.js

My models are:

module.exports = function(sequelize, DataTypes) {
  var CommitFileStatistic;
  return CommitFileStatistic = sequelize.define('CommitFileStatistic', {
    additions: {
      type: DataTypes.INTEGER,
      allowNull: false
    },
    deletions: {
      type: DataTypes.INTEGER,
      allowNull: false
    },
    status: {
      type: DataTypes.STRING,
      allowNull: false
    },
    fileSize: {
      type: DataTypes.INTEGER,
      allowNull: true
    },
    levenshteinDelta: {
      type: DataTypes.INTEGER,
      allowNull: true
    },
    fileHash: {
      type: DataTypes.STRING,
      allowNull: true
    }
  }, {
    classMethods: {
      associate: function(models) {
        CommitFileStatistic.belongsTo(models.Commit);
        return CommitFileStatistic.belongsTo(models.SourceFile);
      }
    }
  });
};

module.exports = function(sequelize, DataTypes) {
  var SourceFile;
  return SourceFile = sequelize.define('SourceFile', {
    filename: {
      type: DataTypes.STRING,
      allowNull: false
    }
  }, {
    classMethods: {
      associate: function(models) {
        return SourceFile.belongsTo(models.Repository);
      }
    }
  });
};

module.exports = function(sequelize, DataTypes) {
  var Commit;
  return Commit = sequelize.define('Commit', {
    sha: {
      type: DataTypes.STRING,
      allowNull: false
    },
    commitTime: {
      type: DataTypes.INTEGER,
      allowNull: false
    },
    message: {
      type: DataTypes.TEXT,
      allowNull: false
    },
    isParsed: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: false
    }
  }, {
    classMethods: {
      associate: function(models) {
        Commit.hasMany(models.Branch);
        return Commit.hasMany(models.Commit, {
          as: 'Parent',
          through: 'ParentCommit'
        });
      }
    }
  });
};

I want to do a query that would basically do: SELECT COUNT(*) AS fileCount, sf.* FROM CommitFileStatistics cfs, Commits c, SourceFiles sf WHERE cfs.CommitId = c.id AND cfs.SourceFileId = sf.id AND c.RepositoryId = 2 GROUP BY cfs.SourceFileId ORDER BY fileCount DESC however I want to use the ORM instead of a raw query. Is this possible?

like image 635
Shamoon Avatar asked Sep 06 '14 12:09

Shamoon


1 Answers

Something like this should be what you are looking for:

return CommitFileStatistic.findAll({
  attributes: [[Sequelize.fn('COUNT', '*'), 'fileCount']],
  include: [
    { model: Commit, attributes: [] },
    { model: SourceFile, attributes: [] }
  ],
  group: ['SourceFileId'],
  order: [['fileCount', 'DESC']]
});

Which will result in the following query:

SELECT 
    `CommitFileStatistic`.`id`, 
    COUNT('*') AS `fileCount`, 
    `Commit`.`id` AS `Commit.id`, 
    `SourceFile`.`id` AS `SourceFile.id` 
FROM 
    `commit_file_statistics` AS `CommitFileStatistic` 
LEFT OUTER JOIN `commits` AS `Commit` 
    ON `Commit`.`id` = `CommitFileStatistic`.`CommitId` 
LEFT OUTER JOIN `source_files` AS `SourceFile` 
    ON `SourceFile`.`id` = `CommitFileStatistic`.`SourceFileId` 
GROUP BY `SourceFileId` 
ORDER BY `fileCount` DESC;

This will probably only work on sequelize version 2.0 :)

like image 87
Jan Aagaard Meier Avatar answered Jan 03 '23 23:01

Jan Aagaard Meier