populate.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { Connection, createConnection } from 'typeorm';
  2. import * as faker from 'faker/locale/en_GB';
  3. import { UserEntity } from '../core/entity/user/user.entity';
  4. import { AddressEntity } from '../core/entity/address/address.entity';
  5. import { ProductEntity } from '../core/entity/product/product.entity';
  6. import { ProductTranslationEntity } from '../core/entity/product/product-translation.entity';
  7. import { ProductVariantTranslationEntity } from '../core/entity/product-variant/product-variant-translation.entity';
  8. import { ProductVariantEntity } from '../core/entity/product-variant/product-variant.entity';
  9. populate();
  10. export async function populate() {
  11. const connection = await createConnection({
  12. type: 'mysql',
  13. entities: ['./**/entity/**/*.entity.ts'],
  14. synchronize: true,
  15. logging: false,
  16. host: '192.168.99.100',
  17. port: 3306,
  18. username: 'root',
  19. password: '',
  20. database: 'test',
  21. });
  22. await populateUsersAndAddresses(connection);
  23. await populateProducts(connection);
  24. }
  25. async function populateProducts(connection: Connection) {
  26. for (let i = 0; i < 5; i++) {
  27. const product = new ProductEntity();
  28. product.image = faker.image.imageUrl();
  29. const name = faker.commerce.productName();
  30. const slug = name.toLowerCase().replace(/\s+/g, '-');
  31. const description = faker.lorem.sentence();
  32. const translation1 = makeProductTranslation('en', name, slug, description);
  33. const translation2 = makeProductTranslation('de', name, slug, description);
  34. await connection.manager.save(translation1);
  35. await connection.manager.save(translation2);
  36. // 1 - 4 variants
  37. const variantCount = Math.floor(Math.random() * 4) + 1;
  38. let variants = [];
  39. for (let j = 0; j < variantCount; j++) {
  40. const variant = new ProductVariantEntity();
  41. const variantName = `${name} variant ${j + 1}`;
  42. variant.image = faker.image.imageUrl();
  43. variant.price = faker.commerce.price(100, 12000, 0);
  44. const variantTranslation1 = makeProductVariantTranslation('en', variantName);
  45. const variantTranslation2 = makeProductVariantTranslation('de', variantName);
  46. await connection.manager.save(variantTranslation1);
  47. await connection.manager.save(variantTranslation2);
  48. variant.translations = [variantTranslation1, variantTranslation2];
  49. await connection.manager.save(variant);
  50. console.log(`${j + 1}. created product variant ${variantName}`);
  51. variants.push(variant);
  52. }
  53. product.variants = variants;
  54. product.translations = [translation1, translation2];
  55. await connection.manager.save(product);
  56. console.log(`${i + 1}. created product & translations for ${translation1.name}`);
  57. }
  58. }
  59. function makeProductTranslation(
  60. langCode: string,
  61. name: string,
  62. slug: string,
  63. description: string,
  64. ): ProductTranslationEntity {
  65. const productTranslation = new ProductTranslationEntity();
  66. productTranslation.languageCode = langCode;
  67. productTranslation.name = `${langCode} ${name}`;
  68. productTranslation.slug = `${langCode} ${slug}`;
  69. productTranslation.description = `${langCode} ${description}`;
  70. return productTranslation;
  71. }
  72. function makeProductVariantTranslation(langCode: string, name: string): ProductVariantTranslationEntity {
  73. const productVariantTranslation = new ProductVariantTranslationEntity();
  74. productVariantTranslation.languageCode = langCode;
  75. productVariantTranslation.name = `${langCode} ${name}`;;
  76. return productVariantTranslation;
  77. }
  78. async function populateUsersAndAddresses(connection: Connection) {
  79. for (let i = 0; i < 5; i++) {
  80. const user = new UserEntity();
  81. user.firstName = faker.name.firstName();
  82. user.lastName = faker.name.lastName();
  83. user.emailAddress = faker.internet.email(user.firstName, user.lastName);
  84. user.phoneNumber = faker.phone.phoneNumber();
  85. const address = new AddressEntity();
  86. address.fullName = `${user.firstName} ${user.lastName}`;
  87. address.streetLine1 = faker.address.streetAddress();
  88. address.city = faker.address.city();
  89. address.province = faker.address.county();
  90. address.postalCode = faker.address.zipCode();
  91. address.country = faker.address.countryCode();
  92. await connection.manager.save(address);
  93. user.addresses = [address];
  94. await connection.manager.save(user);
  95. console.log('created user and address for ' + user.firstName + ' ' + user.lastName);
  96. }
  97. }