Guest User

Untitled

a guest
Jul 20th, 2016
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. var express = require('express');
  2. var router = express.Router();
  3. var Sequelize = require('sequelize');
  4.  
  5. module.exports = function(connection) {
  6.  
  7. var User = connection.define('User', {
  8. name: Sequelize.STRING,
  9. email: {
  10. type: Sequelize.STRING,
  11. primaryKey: true
  12. },
  13. password: Sequelize.STRING,
  14. address: Sequelize.STRING,
  15. phone: Sequelize.STRING
  16. });
  17.  
  18. // TODO: nao precisa criar um id. o sequelize coloca um ID automatico auto incremental se nao tiver PK
  19.  
  20. var Kid = connection.define('Kid', {
  21. name: Sequelize.STRING,
  22. id: {
  23. type: Sequelize.STRING,
  24. primaryKey: true
  25. },
  26. gender: Sequelize.STRING,
  27. birth: Sequelize.STRING
  28. });
  29.  
  30. var Product = connection.define('Product', {
  31. name: Sequelize.STRING,
  32. id: {
  33. type: Sequelize.STRING,
  34. primaryKey: true
  35. },
  36. price: Sequelize.STRING,
  37. photo: Sequelize.STRING, //photo adress on net
  38. availability: Sequelize.BOOLEAN,
  39. description: Sequelize.STRING,
  40. category: Sequelize.STRING
  41. });
  42.  
  43. var ShopCart = connection.define('ShopCart', {
  44. id: {
  45. type: Sequelize.STRING,
  46. primaryKey: true
  47. },
  48. date: Sequelize.STRING,
  49. address: Sequelize.STRING,
  50. status: Sequelize.STRING
  51. });
  52.  
  53. /* var Order = connection.define('Order', {
  54. id: {
  55. type: Sequelize.STRING,
  56. primaryKey: true
  57. },
  58. qtd: Sequelize.ARRAY(Sequelize.INTEGER),
  59. date: Sequelize.STRING,
  60. address: Sequelize.STRING,
  61. status: Sequelize.STRING
  62. });*/
  63.  
  64. var Supplier = connection.define('Supplier', {
  65. name: Sequelize.STRING,
  66. id: {
  67. type: Sequelize.STRING,
  68. primaryKey: true
  69. },
  70. password: Sequelize.STRING,
  71. address: Sequelize.STRING,
  72. phone: Sequelize.STRING,
  73. banner: Sequelize.STRING, //photo adress on net
  74. description: Sequelize.STRING,
  75. categories: Sequelize.ARRAY(Sequelize.STRING)
  76. });
  77.  
  78. var ShopCartProduct = connection.define('ShopCartProduct', {
  79. qtd: Sequelize.INTEGER
  80. });
  81.  
  82. //cardinalidades
  83. User.hasMany(Kid, {as: 'Kids'})
  84. User.hasMany(ShopCart, {as: 'History'})
  85. ShopCart.belongsToMany(Product, {through: 'ShopCartProduct', foreignKey: 'ShopCartId'});
  86. Product.belongsToMany(ShopCart, {through: 'ShopCartProduct', foreignKey: 'ProductId'});
  87. // ShopCart.hasMany(Order, {as: 'Orders'})
  88. //Order.belongsTo(User, {as: 'User'})
  89. //Order.belongsTo(Supplier, {as: 'Supplier'})
  90. //Order.hasMany(Product, {as: 'Products'})
  91. Supplier.hasMany(Product, {as: 'Products'})
  92.  
  93.  
  94. connection.sync(); // create tables if dont exist
  95.  
  96. router.get("/resetdb",function(req,res){ // bugado
  97.  
  98. connection.sync({force: true}); // reset db
  99.  
  100. // connection.query('SET FOREIGN_KEY_CHECKS = 0') // necessario por causa da tabela ElderUser
  101. // .then(function(){
  102. // return connection.sync({ force: true });
  103. // })
  104. // .then(function(){
  105. // return connection.query('SET FOREIGN_KEY_CHECKS = 1')
  106. // })
  107. // .then(function(){
  108. // console.log('Database synchronised.');
  109. // }, function(err){
  110. // console.log(err);
  111. // });
  112.  
  113. res.send("tables reset!");
  114. console.log('tables reset by user');
  115. });
  116.  
  117. // return router;
  118.  
  119. //var tables = {User,Kid,Product,ShopCart,Order,Supplier};
  120. var tables = {User,Kid,Product,ShopCart,Supplier};
  121. return tables;
  122. }
Add Comment
Please, Sign In to add comment