dchester / epilogue

Create flexible REST endpoints and controllers from Sequelize models in your Express app
846 stars 116 forks source link

Association Routes #108

Closed robertminer1 closed 8 years ago

robertminer1 commented 8 years ago

Hi! Sort of new to sequelize, so this could be a problem with the way that I've setup my associations (I apologize in advance if that is the case). So far so good on getting this running with an existing database. I've got an issue with list on an association. Right now, I am doing this:

module.exports = function(sequelize, DataTypes) {
  return sequelize.define('lots', {
    lot_id: {
      type: DataTypes.INTEGER(10),
      allowNull: false,
      primaryKey: true
    },
    lot_event_id: {
      type: DataTypes.INTEGER(10),
      allowNull: false
    },
    lot_category_id: {
      type: DataTypes.INTEGER(10),
      allowNull: false
    },
    lot_number: {
      type: DataTypes.STRING,
      allowNull: false
    }
  });
};

module.exports = function(sequelize, DataTypes) {
  return sequelize.define('events', {
    event_id: {
      type: DataTypes.INTEGER(10),
      allowNull: false,
      primaryKey: true
    },
    event_user_id: {
      type: DataTypes.INTEGER(10),
      allowNull: false
    },
    event_auctioneer_id: {
      type: DataTypes.INTEGER(10),
      allowNull: false
    },
    event_lockcutter_id: {
      type: DataTypes.INTEGER(10),
      allowNull: false
    },
    event_event_category_id: {
      type: DataTypes.INTEGER(10),
      allowNull: false
    },
    event_calendar_id: {
      type: DataTypes.INTEGER(10),
      allowNull: false
    }
  });
};

module.exports = function(sequelize, DataTypes) {
  return sequelize.define('lot_images', {
    lot_image_id: {
      type: DataTypes.INTEGER(10),
      allowNull: false,
      primaryKey: true
    },
    lot_image_event_id: {
      type: DataTypes.INTEGER(10),
      allowNull: false
    },
    lot_image_lot_id: {
      type: DataTypes.INTEGER(10),
      allowNull: false
    },
    lot_image_name: {
      type: DataTypes.STRING,
      allowNull: false
    },
    lot_image_file: {
      type: DataTypes.STRING,
      allowNull: false
    },
    lot_image_main: {
      type: DataTypes.INTEGER(1),
      allowNull: false
    },
    lot_image_deleted: {
      type: DataTypes.BOOLEAN,
      allowNull: false
    }
  });
};

events.hasMany(lot_images, {
  foreignKey: 'lot_image_event_id',
  constraints: false,
});

events.hasMany(lots, {
  foreignKey: 'lot_event_id',
  constraints: false,
});

// Initialize epilogue
epilogue.initialize({
  app: app,
  sequelize: sequelize
});

// Create REST resource
var eventsResource = epilogue.resource({
  model: events,
  associations: true,
  endpoints: ['/events', '/events/:event_id', 'events/:event_id/lot_images', 'events/:event_id/lots'],
  actions: ['read', 'list'],
});    

I'd like for the /events endpoints to return just the events records from the database , but right now when using list /events resource it returns EVERYTHING for the events including all lots & lot_images for each event. How do I get this to just return the events resource without the associations in the response? P.S. I did not architect the DB structure.

robertminer1 commented 8 years ago

Nevermind! I had to do some restructuring and define the endpoints individually (and learn a bit more about sequelize).