Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require( './config.js' );
- var Sequelize = require( $_CONFIG['_module_path'] + "sequelize" );
- var DB = DB || {},
- params = [
- $_CONFIG.db.default.DATABASE,
- $_CONFIG.db.default.USER,
- $_CONFIG.db.default.PASS
- ],
- User,
- Client,
- Project;
- var sequelize = new Sequelize(params);
- DB.createUsersTable = function(){
- User = sequelize.define('User', {
- id : { type: Sequelize.String, primaryKey: true },
- email : { type: Sequelize.STRING, allowNull: false },
- first_name : { type: Sequelize.STRING, allowNull: false },
- last_name : { type: Sequelize.STRING, allowNull: false },
- notifications : { type: Sequelize.BOOLEAN, defaultValue: true },
- last_company_id : { type: Sequelize.INTEGER }
- },
- {
- // don't delete database entries but set the newly added attribute deletedAt
- // to the current date (when deletion was done). paranoid will only work if
- // timestamps are not disabled
- paranoid: true,
- // don't use camelcase for automatically added attributes but underscore style
- // so updatedAt will be updated_at
- underscore: true
- });
- User.sync().on('success', function() {
- // ok ... everything is nice!
- }).on('failure', function(error) {
- console.log( error );
- })
- } // createUsersTable
- DB.createClientsTable = function(){
- Client = sequelize.define('Client', {
- id : { type: Sequelize.String, primaryKey: true },
- company_name: { type: Sequelize.STRING, allowNull: false },
- subdomain : { type: Sequelize.STRING }, // allowNull = false?
- address1 : { type: Sequelize.STRING, allowNull: false },
- address2 : { type: Sequelize.STRING },
- city : { type: Sequelize.STRING, allowNull: false },
- state : { type: Sequelize.STRING, allowNull: false },
- postcode : { type: Sequelize.INTEGER, allowNull: false },
- country : { type: Sequelize.STRING, allowNull: false },
- currency : { type: Sequelize.STRING, allowNull: false },
- phone : { type: Sequelize.INTEGER, allowNull: false },
- signature : { type: Sequelize.STRING },
- language : { type: Sequelize.STRING }, // allowNull = false?
- timezone : { type: Sequelize.STRING }, // allowNull = false?
- date_order : { type: Sequelize.STRING, allowNull: false }, // (e.g. mm/dd)
- round_billable_hours : { type: Sequelize.BOOLEAN, defaultValue: false },
- billing_email : { type: Sequelize.STRING, allowNull: false },
- custom_header_bg : { type: Sequelize.STRING },
- custom_css : { type: Sequelize.STRING },
- invoice_footer : { type: Sequelize.STRING },
- estimate_footer : { type: Sequelize.STRING },
- invoice_email_template : { type: Sequelize.STRING },
- estimate_email_template : { type: Sequelize.STRING }
- },
- {
- // don't delete database entries but set the newly added attribute deletedAt
- // to the current date (when deletion was done). paranoid will only work if
- // timestamps are not disabled
- paranoid: true,
- // don't use camelcase for automatically added attributes but underscore style
- // so updatedAt will be updated_at
- underscore: true
- });
- Client.sync().on('success', function() {
- // ok ... everything is nice!
- }).on('failure', function(error) {
- // oooh, did you entered wrong database credentials?
- })
- } // createClientsTable
- DB.createProjectsTable = function(){
- Project = sequelize.define('Project', {
- id: { type: Sequelize.String, primaryKey: true },
- project_name: { type: Sequelize.STRING, allowNull: false },
- description: { type: Sequelize.TEXT },
- notes: { type: Sequelize.TEXT },
- due_date: { type: Sequelize.DATETIME },
- ongoing: { type: Sequelize.BOOLEAN, defaultValue: false },
- autobill: { type: Sequelize.BOOLEAN, defaultValue: false },
- currency: { type: Sequelize.STRING, allowNull: false },
- billable_hours: { type: Sequelize.STRING }, // @TODO convert column to float
- hourly_rate: { type: Sequelize.STRING }, // @TODO convert column to float
- daily_rate: { type: Sequelize.STRING }, // @TODO convert column to float
- estimated_cost: { type: Sequelize.STRING }, // @TODO convert column to float
- project_order: { type: Sequelize.INTEGER },
- public: { type: Sequelize.BOOLEAN, defaultValue: false },
- active: { type: Sequelize.TINYINT, defaultValue: 0 },
- important: { type: Sequelize.BOOLEAN, defaultValue: false },
- closed: { type: Sequelize.BOOLEAN, defaultValue: false },
- archived: { type: Sequelize.BOOLEAN, defaultValue: false }
- },
- {
- // don't delete database entries but set the newly added attribute deletedAt
- // to the current date (when deletion was done). paranoid will only work if
- // timestamps are not disabled
- paranoid: true,
- // don't use camelcase for automatically added attributes but underscore style
- // so updatedAt will be updated_at
- underscore: true
- });
- Project.sync().on('success', function() {
- // ok ... everything is nice!
- }).on('failure', function(error) {
- // oooh, did you entered wrong database credentials?
- })
- } // createProjectsTable
- DB.createRelations = function(){ // ;D
- // clients
- User.hasMany( Project );
- Client.hasOne( User, {as: 'Superuser', foreignKey: 'superuser_id'} );
- // projects
- User.hasMany( Project );
- Project.belongsTo( Client );
- Project.belongsTo( User );
- sequelize.sync();
- }
- module.exports.DB = DB;
Add Comment
Please, Sign In to add comment