Skip to content

Instantly share code, notes, and snippets.

@tanepiper
Created March 24, 2014 15:32
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 tanepiper/9742552 to your computer and use it in GitHub Desktop.
Save tanepiper/9742552 to your computer and use it in GitHub Desktop.
var Sequelize = require('sequelize');
var HashID = require('hashids');
module.exports = function(Instance) {
Instance.db.Campaign = Instance.db.sequelize.define('campaign', {
name: {
type: Sequelize.STRING,
allowNull: false
},
shortcode: {
type: Sequelize.STRING,
allowNull: true
},
url: {
type: Sequelize.STRING,
allowNull: true
},
projectId: {
type: Sequelize.INTEGER,
references: 'projects',
referenceKey: 'id',
onDelete: 'cascade'
}
}, {
instanceMethods: {
generateShortcode: function(salt, minLength) {
var hash = new HashID(salt || 'this is a salt', minLength || 8);
var shortcode = HashID.encrypt(new Date().getUTCMilliseconds());
this.shortcode = shortcode;
}
}
});
};
var Sequelize = require('sequelize');
var HashID = require('hashids');
module.exports = function(Instance) {
Instance.db.Client = Instance.db.sequelize.define('client', {
name: {
type: Sequelize.STRING,
allowNull: false
},
slug: {
type: Sequelize.STRING(20),
allowNull: true,
defaultValue: ''
},
description: {
type: Sequelize.TEXT,
allowNull: true,
defaultValue: ''
},
isActive: {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: true
},
userId: {
type: Sequelize.INTEGER,
references: 'users',
referenceKey: 'id',
onDelete: 'cascade'
}
}, {
classMethods: {
},
instanceMethods: {
generateSlug: function(minLength, salt) {
var hash = new HashID(salt || 'this is a salt', minLength || 20);
var slug = HashID.encrypt(this.id);
this.slug = slug;
return this;
}
}
});
};
var Sequelize = require('sequelize');
module.exports = function(Instance) {
'use strict';
Instance.db = {
sequelize: new Sequelize('adon-platform', 'development', 'dev', {
dialect: 'postgres'
})
};
require('./User.js')(Instance);
require('./Client.js')(Instance);
require('./Project.js')(Instance);
require('./Campaign.js')(Instance);
Instance.db.User.hasMany(Instance.db.Client);
Instance.db.Client.belongsTo(Instance.db.User);
Instance.db.Client.hasMany(Instance.db.Project);
Instance.db.Client.hasMany(Instance.db.Campaign);
Instance.db.Project.belongsTo(Instance.db.Client);
Instance.db.Project.hasMany(Instance.db.Campaign);
Instance.db.Campaign.belongsTo(Instance.db.Client);
Instance.db.Campaign.belongsTo(Instance.db.Project);
};
var Sequelize = require('sequelize');
module.exports = function(Instance) {
Instance.db.Project = Instance.db.sequelize.define('project', {
name: {
type: Sequelize.STRING,
allowNull: false,
},
clientId: {
type: Sequelize.INTEGER,
references: 'clients',
referenceKey: 'id',
onDelete: 'cascade'
}
});
};
var async = require('async');
var Instance = {};
require('./../lib/db')(Instance);
async.series([
function(done) {
Instance.db.User.sync();
done();
},
function(done) {
Instance.db.Client.sync();
done();
},
function(done) {
Instance.db.Project.sync();
done();
},
function(done) {
Instance.db.Campaign.sync();
done();
}
], function(err, result) {
console.log('done');
})
var Sequelize = require('sequelize');
var Bcrypt = require('bcrypt');
module.exports = function(Instance) {
Instance.db.User = Instance.db.sequelize.define('user', {
email: {
type: Sequelize.STRING,
allowNull: false,
validate: {
isEmail: true
}
},
password: {
type: Sequelize.STRING,
allowNull: false
}
}, {
classMethods: {},
instanceMethods: {
hashPassword: function(password) {
this.password = Bcrypt.hashSync(this.password, 10);
return this;
},
checkPassword: function(password) {
return Bcrypt.compareSync(password, this.password);
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment