Skip to content

Instantly share code, notes, and snippets.

@keithmichelson
Last active March 30, 2016 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keithmichelson/1d0305886d34b9e1bbc009d45205f235 to your computer and use it in GitHub Desktop.
Save keithmichelson/1d0305886d34b9e1bbc009d45205f235 to your computer and use it in GitHub Desktop.
//Following Model
var _ = require('underscore');
module.exports = function(sequelize, DataTypes) {
return sequelize.define('following', {
following: {
type: DataTypes.INTEGER,
allowNull: false,
validate: {
isBoolean: function (value) {
if (typeof value !== 'number') {
throw new Error('following must be a number');
}
}
}
}
},
{
//instance can be used anywhere in an instance
instanceMethods: {
//return only JSON the public should see
toPublicJSON: function () {
var json = this.toJSON();
json.name = json.user.profile.firstName;
json.job = json.user.profile.job;
json.image = json.user.profile.image;
//console.log(json);
return _.pick(json, 'id', 'createdAt', 'updatedAt', 'job', 'name', 'image');
}
}
});
};
//route
app.get('/followings-people', middleware.requireAuthentication, function(req, res) {
var query = req.query;
var where = {
userId: req.user.get('id')
};
var include = [
{model: db.user, include: [
{model: db.profile}
]}
];
db.following.findAll({where: where, include: include}).then(
function(following) {
res.json(following.map(function(following) {
return following.toPublicJSON();
}));
},
function(e) {
res.status(500).send();
}
);
});
module.exports = function(sequelize, DataTypes) {
return sequelize.define('profiles', {
firstName: {
type: DataTypes.STRING,
allowNull: false,
validate: {
isString: function (value) {
if (typeof value !== 'string') {
throw new Error('Completed must be a string');
}
},
//length 1 or greater or less than 250
len: [1, 250]
}
},
lastName: {
type: DataTypes.STRING,
allowNull: false,
validate: {
isString: function (value) {
if (typeof value !== 'string') {
throw new Error('Completed must be a string');
}
},
//length 1 or greater or less than 250
len: [1, 250]
}
},
job: {
type: DataTypes.STRING,
allowNull: false,
validate: {
isString: function (value) {
if (typeof value !== 'string') {
throw new Error('Completed must be a string');
}
},
//length 1 or greater or less than 250
len: [1, 250]
}
},
image: {
type: DataTypes.STRING,
allowNull: false,
validate: {
isString: function (value) {
if (typeof value !== 'string') {
throw new Error('Completed must be a string');
}
},
//length 1 or greater or less than 250
len: [1, 250]
}
},
website: {
type: DataTypes.STRING,
allowNull: false,
validate: {
isString: function (value) {
if (typeof value !== 'string') {
throw new Error('Completed must be a string');
}
},
//length 1 or greater or less than 250
len: [1, 250]
}
},
completed: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
validate: {
isBoolean: function (value) {
if (typeof value !== 'boolean') {
throw new Error('Completed must be a boolean');
}
}
}
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment