populate.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { INestApplication } from '@nestjs/common';
  2. import { Channel } from '../../shared/generated-types';
  3. import { VendureBootstrapFunction } from '../src/bootstrap';
  4. import { setConfig } from '../src/config/config-helpers';
  5. import { VendureConfig } from '../src/config/vendure-config';
  6. import { clearAllTables } from './clear-all-tables';
  7. import { getDefaultChannelToken } from './get-default-channel-token';
  8. import { MockDataService } from './mock-data.service';
  9. import { SimpleGraphQLClient } from './simple-graphql-client';
  10. export interface PopulateOptions {
  11. logging?: boolean;
  12. productCount: number;
  13. customerCount: number;
  14. allCountries?: boolean;
  15. channels?: string[];
  16. }
  17. // tslint:disable:no-floating-promises
  18. /**
  19. * Clears all tables from the database and populates with (deterministic) random data.
  20. */
  21. export async function populate(
  22. config: VendureConfig,
  23. bootstrapFn: VendureBootstrapFunction,
  24. options: PopulateOptions,
  25. ): Promise<INestApplication> {
  26. (config.dbConnectionOptions as any).logging = false;
  27. const logging = options.logging === undefined ? true : options.logging;
  28. const originalRequireVerification = config.authOptions.requireVerification;
  29. config.authOptions.requireVerification = false;
  30. setConfig(config);
  31. await clearAllTables(config.dbConnectionOptions, logging);
  32. const app = await bootstrapFn(config);
  33. const defaultChannelToken = await getDefaultChannelToken(logging);
  34. const client = new SimpleGraphQLClient(`http://localhost:${config.port}/${config.apiPath}`);
  35. client.setChannelToken(defaultChannelToken);
  36. await client.asSuperAdmin();
  37. const mockDataService = new MockDataService(client, logging);
  38. if (options.channels) {
  39. await mockDataService.populateChannels(options.channels);
  40. }
  41. const zones = await mockDataService.populateCountries(options.allCountries);
  42. await mockDataService.setChannelDefaultZones(zones);
  43. await mockDataService.populateShippingMethods();
  44. const assets = await mockDataService.populateAssets();
  45. const optionGroupId = await mockDataService.populateOptions();
  46. const taxCategories = await mockDataService.populateTaxCategories(zones);
  47. await mockDataService.populateProducts(options.productCount, optionGroupId, assets, taxCategories);
  48. await mockDataService.populateCustomers(options.customerCount);
  49. await mockDataService.populateFacets();
  50. config.authOptions.requireVerification = originalRequireVerification;
  51. return app;
  52. }