Guest User

Untitled

a guest
Jul 11th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. import Sequelize from 'sequelize';
  2. import _ from 'lodash';
  3. import Faker from 'faker';
  4.  
  5. const Conn = new Sequelize(
  6. 'elzzleDB',
  7. 'root',
  8. 'password',
  9. {
  10. dialect: 'mysql',
  11. host: 'localhost',
  12. port: 3306
  13. }
  14. );
  15.  
  16. //start creating objects here
  17. const Person = Conn.define('person', {
  18. firstName: {
  19. type: Sequelize.STRING,
  20. allowNull: false
  21. },
  22. lastName: {
  23. type: Sequelize.STRING,
  24. allowNull: false
  25. },
  26. email: {
  27. type: Sequelize.STRING,
  28. allowNull: false,
  29. validate: {
  30. isEmail: true
  31. }
  32. }
  33. });
  34.  
  35. const Listing = Conn.define('listing', {
  36. thumbnailUrl: {
  37. type: Sequelize.STRING,
  38. allowNull: false
  39. },
  40. title: {
  41. type: Sequelize.STRING,
  42. allowNull: false
  43. },
  44. description: {
  45. type: Sequelize.TEXT("medium"),
  46. allowNull: false
  47. },
  48. allowRent: {
  49. type: Sequelize.BOOLEAN,
  50. allowNull: false
  51. },
  52. allowBuy: {
  53. type: Sequelize.BOOLEAN,
  54. allowNull: false
  55. },
  56. rentPrice: {
  57. type: Sequelize.INTEGER,
  58. allowNull: true,
  59. },
  60. buyPrice: {
  61. type: Sequelize.INTEGER,
  62. allowNull: true
  63. }
  64. });
  65.  
  66. //Relationships
  67. Person.hasMany(Listing);
  68. Listing.belongsTo(Person);
  69.  
  70. //WARNING: Will forcefully override tables in db
  71. Conn.sync({force: true}).then(() => {
  72. _.times(10, () => {
  73. var row = Person.create({
  74. firstName: Faker.name.firstName(),
  75. lastName: Faker.name.lastName(),
  76. email: Faker.internet.email()
  77. }).then(person => {
  78. let price = Faker.random.number(100);
  79. return _.times(10, () => {
  80. person.createListing({
  81. thumbnailUrl: Faker.image.imageUrl(),
  82. title: Faker.commerce.productName(),
  83. description: Faker.lorem.sentences(),
  84. allowRent: Faker.random.boolean(),
  85. allowBuy: Faker.random.boolean(),
  86. rentPrice: price,
  87. buyPrice: price * 10 + Faker.random.number(100)
  88. })
  89. });
  90. });
  91. });
  92. });
  93.  
  94. export default Conn;
Add Comment
Please, Sign In to add comment