populate.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* tslint:disable:no-console */
  2. import { INestApplication } from '@nestjs/common';
  3. import fs from 'fs-extra';
  4. import path from 'path';
  5. import { LanguageCode } from '../../shared/generated-types';
  6. import { VendureBootstrapFunction } from '../src/bootstrap';
  7. import { setConfig } from '../src/config/config-helpers';
  8. import { VendureConfig } from '../src/config/vendure-config';
  9. import { clearAllTables } from './clear-all-tables';
  10. import { getDefaultChannelToken } from './get-default-channel-token';
  11. import { MockDataService } from './mock-data.service';
  12. import { SimpleGraphQLClient } from './simple-graphql-client';
  13. export interface PopulateOptions {
  14. logging?: boolean;
  15. customerCount: number;
  16. productsCsvPath: string;
  17. initialDataPath: string;
  18. }
  19. // tslint:disable:no-floating-promises
  20. /**
  21. * Clears all tables from the database and populates with (deterministic) random data.
  22. */
  23. export async function populate(
  24. config: VendureConfig,
  25. bootstrapFn: VendureBootstrapFunction,
  26. options: PopulateOptions,
  27. ): Promise<INestApplication> {
  28. (config.dbConnectionOptions as any).logging = false;
  29. const logging = options.logging === undefined ? true : options.logging;
  30. const originalRequireVerification = config.authOptions.requireVerification;
  31. config.authOptions.requireVerification = false;
  32. setConfig(config);
  33. await clearAllTables(config.dbConnectionOptions, logging);
  34. const app = await bootstrapFn(config);
  35. await populateInitialData(app, options.initialDataPath, logging);
  36. await populateProducts(app, options.productsCsvPath, logging);
  37. await populateCollections(app, options.initialDataPath, logging);
  38. const defaultChannelToken = await getDefaultChannelToken(logging);
  39. const client = new SimpleGraphQLClient(`http://localhost:${config.port}/${config.adminApiPath}`);
  40. client.setChannelToken(defaultChannelToken);
  41. await client.asSuperAdmin();
  42. const mockDataService = new MockDataService(client, logging);
  43. await mockDataService.populateCustomers(options.customerCount);
  44. config.authOptions.requireVerification = originalRequireVerification;
  45. return app;
  46. }
  47. async function populateInitialData(app: INestApplication, initialDataPath: string, logging: boolean) {
  48. const { Populator } = await import('../src/data-import/providers/populator/populator');
  49. const { initialData } = await import(initialDataPath);
  50. const populator = app.get(Populator);
  51. try {
  52. await populator.populateInitialData(initialData);
  53. if (logging) {
  54. console.log(`\nPopulated initial data`);
  55. }
  56. } catch (err) {
  57. console.error(err.message);
  58. }
  59. }
  60. async function populateCollections(app: INestApplication, initialDataPath: string, logging: boolean) {
  61. const { Populator } = await import('../src/data-import/providers/populator/populator');
  62. const { initialData } = await import(initialDataPath);
  63. if (!initialData.collections.length) {
  64. return;
  65. }
  66. const populator = app.get(Populator);
  67. try {
  68. await populator.populateCollections(initialData);
  69. if (logging) {
  70. console.log(`\nCreated ${initialData.collections.length} Collections`);
  71. }
  72. } catch (err) {
  73. console.error(err.message);
  74. }
  75. }
  76. async function populateProducts(app: INestApplication, productsCsvPath: string, logging: boolean) {
  77. const { Importer } = await import('../src/data-import/providers/importer/importer');
  78. const importer = app.get(Importer);
  79. const productData = await fs.readFile(productsCsvPath, 'utf-8');
  80. const importResult = await importer.parseAndImport(productData, LanguageCode.en, false).toPromise();
  81. if (importResult.errors && importResult.errors.length) {
  82. console.log(`${importResult.errors.length} errors encountered when importing product data:`);
  83. await console.log(importResult.errors.join('\n'));
  84. }
  85. if (logging) {
  86. console.log(`\nImported ${importResult.imported} products`);
  87. }
  88. }