init-load-test.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // tslint:disable-next-line:no-reference
  2. /// <reference path="../../core/typings.d.ts" />
  3. import { bootstrap, VendureConfig } from '@vendure/core';
  4. import { populate } from '@vendure/core/cli/populate';
  5. import { BaseProductRecord } from '@vendure/core/dist/data-import/providers/import-parser/import-parser';
  6. import stringify from 'csv-stringify';
  7. import fs from 'fs';
  8. import path from 'path';
  9. import { clearAllTables } from '../../core/mock-data/clear-all-tables';
  10. import { initialData } from '../../core/mock-data/data-sources/initial-data';
  11. import { populateCustomers } from '../../core/mock-data/populate-customers';
  12. import { devConfig } from '../dev-config';
  13. import { getLoadTestConfig, getMysqlConnectionOptions, getProductCount, getProductCsvFilePath } from './load-test-config';
  14. // tslint:disable:no-console
  15. /**
  16. * A script used to populate a database with test data for load testing.
  17. */
  18. if (require.main === module) {
  19. // Running from command line
  20. isDatabasePopulated()
  21. .then(isPopulated => {
  22. if (!isPopulated) {
  23. const count = getProductCount();
  24. const config = getLoadTestConfig();
  25. const csvFile = getProductCsvFilePath();
  26. return clearAllTables(config.dbConnectionOptions, true)
  27. .then(() => {
  28. if (!fs.existsSync(csvFile)) {
  29. return generateProductsCsv(count);
  30. }
  31. })
  32. .then(() => populate(() => bootstrap(config),
  33. path.join(__dirname, '../../create/assets/initial-data.json'),
  34. csvFile,
  35. path.join(__dirname, './data-sources'),
  36. ))
  37. .then(async app => {
  38. console.log('populating customers...');
  39. await populateCustomers(10, config as any, true);
  40. return app.close();
  41. });
  42. } else {
  43. console.log('Database is already populated!');
  44. }
  45. })
  46. .then(
  47. () => process.exit(0),
  48. err => {
  49. console.log(err);
  50. process.exit(1);
  51. },
  52. );
  53. }
  54. /**
  55. * Tests to see whether the load test database is already populated.
  56. */
  57. function isDatabasePopulated(): Promise<boolean> {
  58. const mysql = require('mysql');
  59. const count = getProductCount();
  60. const mysqlConnectionOptions = getMysqlConnectionOptions(count);
  61. const connection = mysql.createConnection({
  62. host: mysqlConnectionOptions.host,
  63. user: mysqlConnectionOptions.username,
  64. password: mysqlConnectionOptions.password,
  65. database: mysqlConnectionOptions.database,
  66. });
  67. return new Promise<boolean>((resolve, reject) => {
  68. connection.connect((error: any) => {
  69. if (error) {
  70. reject(error);
  71. return;
  72. }
  73. connection.query('SELECT COUNT(id) as prodCount FROM product', (err: any, results: any) => {
  74. if (err) {
  75. if (err.code === 'ER_NO_SUCH_TABLE') {
  76. resolve(false);
  77. return;
  78. }
  79. reject(err);
  80. return;
  81. }
  82. resolve(results[0].prodCount === count);
  83. });
  84. });
  85. });
  86. }
  87. /**
  88. * Generates a CSV file of test product data which can then be imported into Vendure.
  89. */
  90. function generateProductsCsv(productCount: number = 100): Promise<void> {
  91. const result: BaseProductRecord[] = [];
  92. const stringifier = stringify({
  93. delimiter: ',',
  94. });
  95. const data: string[] = [];
  96. console.log(`Generating ${productCount} rows of test product data...`);
  97. stringifier.on('readable', () => {
  98. let row;
  99. // tslint:disable-next-line:no-conditional-assignment
  100. while (row = stringifier.read()) {
  101. data.push(row);
  102. }
  103. });
  104. return new Promise((resolve, reject) => {
  105. const csvFile = getProductCsvFilePath();
  106. stringifier.on('error', (err: any) => {
  107. reject(err.message);
  108. });
  109. stringifier.on('finish', async () => {
  110. fs.writeFileSync(csvFile, data.join(''));
  111. console.log(`Done! Saved to ${csvFile}`);
  112. resolve();
  113. });
  114. generateMockData(productCount, row => stringifier.write(row));
  115. stringifier.end();
  116. });
  117. }
  118. function generateMockData(productCount: number, writeFn: (row: string[]) => void) {
  119. const headers: Array<keyof BaseProductRecord> = [
  120. 'name',
  121. 'slug',
  122. 'description',
  123. 'assets',
  124. 'facets',
  125. 'optionGroups',
  126. 'optionValues',
  127. 'sku',
  128. 'price',
  129. 'taxCategory',
  130. 'variantAssets',
  131. 'variantFacets',
  132. ];
  133. writeFn(headers);
  134. const LOREM = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna ' +
  135. 'aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. ' +
  136. 'Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. ' +
  137. 'Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
  138. .replace(/\r\n/g, '');
  139. const categories = getCategoryNames();
  140. for (let i = 1; i <= productCount; i++) {
  141. const outputRow: BaseProductRecord = {
  142. name: `Product ${i}`,
  143. slug: `product-${i}`,
  144. description: LOREM,
  145. assets: 'product-image.jpg',
  146. facets: `category:${categories[i % categories.length]}`,
  147. optionGroups: '',
  148. optionValues: '',
  149. sku: `PRODID${i}`,
  150. price: '12345',
  151. taxCategory: 'standard',
  152. variantAssets: '',
  153. variantFacets: '',
  154. };
  155. writeFn(Object.values(outputRow) as string[]);
  156. }
  157. }
  158. function getCategoryNames() {
  159. const allNames = initialData.collections.reduce((all, c) => [...all, ...c.facetNames], [] as string[]);
  160. return Array.from(new Set(allNames));
  161. }