4
我的車型有:我如何通過3個表和Sequelize做一個組?
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'
});
}
}
});
};
我想要做一個查詢,就基本上做到: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
但我想使用ORM代替原始查詢。這可能嗎?