Created
August 17, 2014 15:14
-
-
Save calvintwr/17840d8b02edb41b5313 to your computer and use it in GitHub Desktop.
models
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/***** models/index.js */ | |
var fs = require('fs') | |
, path = require('path') | |
, Sequelize = require('sequelize') | |
, lodash = require('../node_modules/sequelize/node_modules/lodash') | |
//'name','username','password' | |
, sequelize = new Sequelize('postgres', 'postgres', 'password' | |
,{ | |
host: "localhost", | |
dialect: "postgres", // or 'sqlite', 'postgres', 'mariadb' | |
port: 5432, // or 5432 (for postgres) | |
native: false, //change settings to localhost and turn native to false if unable to compile native C bindings | |
}) | |
, db = {} | |
fs | |
.readdirSync(__dirname) | |
.filter(function(file) { | |
return (file.indexOf('.') !== 0) && (file !== 'index.js') | |
}) | |
.forEach(function(file) { | |
var model = sequelize.import(path.join(__dirname, file)) | |
db[model.name] = model | |
}) | |
//console.log(db); | |
Object.keys(db).forEach(function(modelName) { | |
if ('associate' in db[modelName]) { | |
db[modelName].associate(db) | |
} | |
}) | |
// db.User.sync(); | |
// db.Post.sync(); | |
// db.Comment.sync(); | |
module.exports = lodash.extend({ | |
sequelize: sequelize, | |
Sequelize: Sequelize | |
}, db) | |
/****** models/User.js */ | |
var moment = require('moment'); | |
module.exports = function(sequelize, DataTypes) { | |
//note: jQuery validation rules applied at clientside should sync with this | |
var User = sequelize.define('User', | |
{ | |
userId: { | |
type: DataTypes.INTEGER, | |
primaryKey: true, | |
allowNull: false, | |
autoIncrement: true | |
}, | |
userName: { | |
type: DataTypes.STRING, | |
allowNull: false, | |
validate: { | |
max: 15, | |
min: 6, | |
is: ["^[a-z0-9_]+$", "i"] | |
} | |
}, | |
userNameDisp: { | |
type: DataTypes.STRING, | |
allowNull: false | |
}, | |
email: { | |
type: DataTypes.STRING, | |
allowNull: false, | |
validate: { | |
isEmail: true | |
} | |
}, | |
password: { | |
type: DataTypes.STRING, | |
allowNull: false, | |
validate: { | |
min: 6 | |
} | |
} | |
}, { | |
timestamps: true, | |
tableName: 'Users', | |
classMethods: { | |
associate: function(models) { | |
User.hasMany(models.Post, {foreignKey: 'User_userId', foreignKeyConstraint: true }); | |
//following | |
User.hasMany(models.User, {as: 'Follows', foreignKey: 'UserId', through: 'Following' }); | |
User.hasMany(models.User, {as: 'Followers', foreignKey: 'FollowId', through: 'Following'}); | |
//comment | |
User.hasMany(models.Comment, {foreignKey: 'User_userId', foreignKeyConstraint: true }); | |
} | |
} | |
} | |
); | |
return User; | |
}; | |
/****** models/Post.js */ | |
var moment = require('moment'); | |
module.exports = function(sequelize, DataTypes) { | |
//note: jQuery validation rules applied at clientside should sync with this | |
var Post = sequelize.define('Post', | |
{ | |
//camelCase | |
postId: { | |
type: DataTypes.INTEGER, | |
primaryKey: true, | |
allowNull: false, | |
autoIncrement: true | |
}, | |
desc: { | |
type: DataTypes.STRING, | |
allowNull: false, | |
validate: { | |
} | |
//get: function() { return this.getDataValue('desc') } | |
}, | |
User_userId: { | |
type: DataTypes.INTEGER | |
} | |
}, { | |
timestamps: true, | |
tableName: 'Posts', //PascalCase | |
classMethods: { | |
associate: function(models) { | |
Post.belongsTo(models.User, {foreignKey: 'User_userId', foreignKeyConstraint: true }); | |
//comment | |
Post.hasMany(models.Comment, {foreignKey: 'Post_postId', foreignKeyConstraint: true }); | |
} | |
} | |
} | |
); | |
return Post; | |
}; | |
/***** models/Comment.js */ | |
module.exports = function(sequelize, DataTypes) { | |
//note: jQuery validation rules applied at clientside should sync with this | |
var Comment = sequelize.define('Comment', | |
{ | |
//camelCase | |
commentId: { | |
type: DataTypes.INTEGER, | |
primaryKey: true, | |
allowNull: false, | |
autoIncrement: true | |
}, | |
comment: { | |
type: DataTypes.STRING, | |
allowNull: false, | |
validate: { | |
} | |
}, | |
}, { | |
timestamps: true, | |
tableName: 'Comments', //PascalCase | |
classMethods: { | |
associate: function(models) { | |
Comment.belongsTo(models.Post, {foreignKey: 'Post_postId', foreignKeyConstraint: true }); | |
Comment.belongsTo(models.User, {foreignKey: 'User_userId', foreignKeyConstraint: true }); | |
} | |
} | |
} | |
); | |
return Comment; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment