Guest User

Untitled

a guest
Jan 13th, 2018
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. require( './config.js' );
  2. var Sequelize = require( $_CONFIG['_module_path'] + "sequelize" );
  3.  
  4. var DB = DB || {},
  5. params = [
  6. $_CONFIG.db.default.DATABASE,
  7. $_CONFIG.db.default.USER,
  8. $_CONFIG.db.default.PASS
  9. ],
  10. User,
  11. Client,
  12. Project;
  13.  
  14. var sequelize = new Sequelize(params);
  15.  
  16. DB.createUsersTable = function(){
  17.  
  18. User = sequelize.define('User', {
  19.  
  20. id : { type: Sequelize.String, primaryKey: true },
  21. email : { type: Sequelize.STRING, allowNull: false },
  22. first_name : { type: Sequelize.STRING, allowNull: false },
  23. last_name : { type: Sequelize.STRING, allowNull: false },
  24. notifications : { type: Sequelize.BOOLEAN, defaultValue: true },
  25. last_company_id : { type: Sequelize.INTEGER }
  26. },
  27. {
  28. // don't delete database entries but set the newly added attribute deletedAt
  29. // to the current date (when deletion was done). paranoid will only work if
  30. // timestamps are not disabled
  31. paranoid: true,
  32.  
  33. // don't use camelcase for automatically added attributes but underscore style
  34. // so updatedAt will be updated_at
  35. underscore: true
  36.  
  37. });
  38.  
  39. User.sync().on('success', function() {
  40. // ok ... everything is nice!
  41. }).on('failure', function(error) {
  42. console.log( error );
  43. })
  44.  
  45. } // createUsersTable
  46.  
  47. DB.createClientsTable = function(){
  48. Client = sequelize.define('Client', {
  49.  
  50. id : { type: Sequelize.String, primaryKey: true },
  51. company_name: { type: Sequelize.STRING, allowNull: false },
  52. subdomain : { type: Sequelize.STRING }, // allowNull = false?
  53. address1 : { type: Sequelize.STRING, allowNull: false },
  54. address2 : { type: Sequelize.STRING },
  55. city : { type: Sequelize.STRING, allowNull: false },
  56. state : { type: Sequelize.STRING, allowNull: false },
  57. postcode : { type: Sequelize.INTEGER, allowNull: false },
  58. country : { type: Sequelize.STRING, allowNull: false },
  59. currency : { type: Sequelize.STRING, allowNull: false },
  60. phone : { type: Sequelize.INTEGER, allowNull: false },
  61. signature : { type: Sequelize.STRING },
  62. language : { type: Sequelize.STRING }, // allowNull = false?
  63. timezone : { type: Sequelize.STRING }, // allowNull = false?
  64. date_order : { type: Sequelize.STRING, allowNull: false }, // (e.g. mm/dd)
  65.  
  66. round_billable_hours : { type: Sequelize.BOOLEAN, defaultValue: false },
  67. billing_email : { type: Sequelize.STRING, allowNull: false },
  68. custom_header_bg : { type: Sequelize.STRING },
  69. custom_css : { type: Sequelize.STRING },
  70. invoice_footer : { type: Sequelize.STRING },
  71. estimate_footer : { type: Sequelize.STRING },
  72. invoice_email_template : { type: Sequelize.STRING },
  73. estimate_email_template : { type: Sequelize.STRING }
  74. },
  75. {
  76. // don't delete database entries but set the newly added attribute deletedAt
  77. // to the current date (when deletion was done). paranoid will only work if
  78. // timestamps are not disabled
  79. paranoid: true,
  80.  
  81. // don't use camelcase for automatically added attributes but underscore style
  82. // so updatedAt will be updated_at
  83. underscore: true
  84.  
  85. });
  86.  
  87. Client.sync().on('success', function() {
  88. // ok ... everything is nice!
  89. }).on('failure', function(error) {
  90. // oooh, did you entered wrong database credentials?
  91. })
  92.  
  93. } // createClientsTable
  94.  
  95. DB.createProjectsTable = function(){
  96. Project = sequelize.define('Project', {
  97.  
  98. id: { type: Sequelize.String, primaryKey: true },
  99. project_name: { type: Sequelize.STRING, allowNull: false },
  100. description: { type: Sequelize.TEXT },
  101. notes: { type: Sequelize.TEXT },
  102. due_date: { type: Sequelize.DATETIME },
  103. ongoing: { type: Sequelize.BOOLEAN, defaultValue: false },
  104. autobill: { type: Sequelize.BOOLEAN, defaultValue: false },
  105. currency: { type: Sequelize.STRING, allowNull: false },
  106. billable_hours: { type: Sequelize.STRING }, // @TODO convert column to float
  107. hourly_rate: { type: Sequelize.STRING }, // @TODO convert column to float
  108. daily_rate: { type: Sequelize.STRING }, // @TODO convert column to float
  109. estimated_cost: { type: Sequelize.STRING }, // @TODO convert column to float
  110. project_order: { type: Sequelize.INTEGER },
  111. public: { type: Sequelize.BOOLEAN, defaultValue: false },
  112. active: { type: Sequelize.TINYINT, defaultValue: 0 },
  113. important: { type: Sequelize.BOOLEAN, defaultValue: false },
  114. closed: { type: Sequelize.BOOLEAN, defaultValue: false },
  115. archived: { type: Sequelize.BOOLEAN, defaultValue: false }
  116. },
  117. {
  118. // don't delete database entries but set the newly added attribute deletedAt
  119. // to the current date (when deletion was done). paranoid will only work if
  120. // timestamps are not disabled
  121. paranoid: true,
  122.  
  123. // don't use camelcase for automatically added attributes but underscore style
  124. // so updatedAt will be updated_at
  125. underscore: true
  126.  
  127. });
  128.  
  129. Project.sync().on('success', function() {
  130. // ok ... everything is nice!
  131. }).on('failure', function(error) {
  132. // oooh, did you entered wrong database credentials?
  133. })
  134.  
  135. } // createProjectsTable
  136.  
  137. DB.createRelations = function(){ // ;D
  138.  
  139. // clients
  140. User.hasMany( Project );
  141. Client.hasOne( User, {as: 'Superuser', foreignKey: 'superuser_id'} );
  142.  
  143. // projects
  144. User.hasMany( Project );
  145. Project.belongsTo( Client );
  146. Project.belongsTo( User );
  147.  
  148. sequelize.sync();
  149.  
  150. }
  151.  
  152. module.exports.DB = DB;
Add Comment
Please, Sign In to add comment