Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Sequelize associated data many-many
// Simple example where you have a many-many relationship with Categories and Posts
// in sequelizejs and want to send the associated data as part of the response
Category.findAll({include: ['posts'] })
.success(function(categories) {
var results = {
var temp = mapAttributes(category);
var p_results ={
var t = mapAttributes(post);
return t;
temp.posts = p_results;
return temp;
// this is for an expressjs app to send the results as json
function mapAttributes(instance) {
var obj = new Object(),
ctx = instance;
ctx.attributes.forEach(function(attr) {
obj[attr] = ctx[attr];
return obj;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment