Skip to content

Instantly share code, notes, and snippets.

@ciberch
Created May 22, 2012 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ciberch/2770735 to your computer and use it in GitHub Desktop.
Save ciberch/2770735 to your computer and use it in GitHub Desktop.
Activity Query with Relational DB
User.hasMany("bugs", Bug, "creatorId");
Bug.belongsTo("user", User, "creatorId");
Project.hasMany("bugs", Bug, "projectId");
Bug.belongsTo("project", Project, "projectId");
User.hasMany("commits", Commit, "creatorId");
Commit.belongsTo("user", User, "creatorId");
Project.hasMany("commits", Commit, "projectId");
Commit.belongsTo("project", Project, "projectId");
Bug
.select('createdAt', 'creatorId', 'name', 'url', 'description')
.page(0, 10)
.order('createdAt DESC')
.all(function(err, bugs) {
Commit
.select('createdAt', 'creatorId', 'name', 'url', 'description')
.page(0, 10)
.order('createdAt DESC')
.all(function(err, commits) {
// coalesce
ordered = coalesce(bugs, commits, 10);
uniqueUsers = findUniqueUsers(ordered);
uniqueProjects = findUniqueProjects(ordered);
User
.select('id', 'name', 'url', 'avatar_url')
.where({ 'id.in': uniqueUsers })
.all(function(err, users) {
Project
.select('id', 'name', 'url', 'avatar_url')
.where({ 'id.in': uniqueProjects })
.all(function(err, projects) {
// finally, now correlate all the data back together
var activities = [];
//...
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment