Skip to content

Instantly share code, notes, and snippets.

@mickhansen
Created December 15, 2013 17:47
Show Gist options
  • Save mickhansen/7975865 to your computer and use it in GitHub Desktop.
Save mickhansen/7975865 to your computer and use it in GitHub Desktop.
var Sequelize = require('sequelize'),
sequelize = new Sequelize('sequelize_test', 'sequelize_test', 'YDLvjZJv9GdvbM8G', {
logging: console.log
});
var User = sequelize.define('User', {}),
Post = sequelize.define('Post', {})
Post.belongsTo(User, {as: 'Creator', foreignKey: 'creator_id', foreignKeyConstraint: true});
User.hasMany(Post, {foreignKey: 'creator_id'});
User.hasMany(Post, {as: 'UserPostShared', joinTableName: 'SharedPosts', foreignKey: 'user_id'}); // , foreignKey: 'user_id', foreignKeyConstraint: true
Post.hasMany(User, {as: 'UserPostShared', joinTableName: 'SharedPosts', foreignKey: 'post_id'}); // , foreignKey: 'post_id', foreignKeyConstraint: true
User.DAO.prototype.getSharedPosts = function() {
return this.getUserPostShared.apply(this, arguments);
};
Post.DAO.prototype.getSharedWithUsers = function() {
return this.getUserPostShared.apply(this, arguments);
};
sequelize.sync({force: true}).done(function (err) {
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment