Skip to content

Instantly share code, notes, and snippets.

@jrm2k6
Created October 29, 2017 01:57
Show Gist options
  • Save jrm2k6/4d808c8210ca1f0d57a7a6f885f58da8 to your computer and use it in GitHub Desktop.
Save jrm2k6/4d808c8210ca1f0d57a7a6f885f58da8 to your computer and use it in GitHub Desktop.
'use strict';
module.exports = {
up: function(queryInterface, Sequelize) {
return queryInterface.createTable('pictures', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
path: {
type: Sequelize.STRING
},
userId: {
type: Sequelize.INTEGER,
allowNull: true,
references: {
model: 'users',
key: 'id'
}
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: function(queryInterface, Sequelize) {
return queryInterface.dropTable('pictures');
}
};
'use strict';
module.exports = {
up: function (queryInterface, Sequelize) {
return queryInterface.createTable('users', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
thirdPartyUserId: Sequelize.INTEGER,
name: Sequelize.STRING,
oauthToken: Sequelize.STRING,
oauthTokenSecret: Sequelize.STRING,
provider: Sequelize.STRING,
createdAt: {
type: Sequelize.DATE
},
updatedAt: {
type: Sequelize.DATE
}
});
},
down: function (queryInterface, Sequelize) {
return queryInterface.dropTable('users');
}
};
'use strict';
module.exports = function(sequelize, DataTypes) {
var Picture = sequelize.define('Picture', {
path: DataTypes.STRING,
uploadedByUserId: DataTypes.INTEGER
});
Picture.associate = function(models) {
Picture.belongsTo(models.User, {
foreignKey: {
allowNull: true
}
});
}
return Picture;
};
module.exports = function(sequelize, Sequelize) {
const User = sequelize.define('User', {
thirdPartyUserId: Sequelize.INTEGER,
name: Sequelize.STRING,
oauthToken: Sequelize.STRING,
oauthTokenSecret: Sequelize.STRING,
provider: Sequelize.STRING
});
User.prototype.asSafeUser = function() {
return {
id: this.getDataValue('id'),
name: this.getDataValue('name'),
provider: this.getDataValue('provider')
}
}
User.associate = function(models) {
User.hasMany(models.Picture);
}
return User;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment