populate.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. channels?: string[];
  15. }
  16. // tslint:disable:no-floating-promises
  17. /**
  18. * Clears all tables from the database and populates with (deterministic) random data.
  19. */
  20. export async function populate(
  21. config: VendureConfig,
  22. bootstrapFn: VendureBootstrapFunction,
  23. options: PopulateOptions,
  24. ): Promise<INestApplication> {
  25. (config.dbConnectionOptions as any).logging = false;
  26. const logging = options.logging === undefined ? true : options.logging;
  27. const originalRequireVerification = config.authOptions.requireVerification;
  28. config.authOptions.requireVerification = false;
  29. setConfig(config);
  30. await clearAllTables(config.dbConnectionOptions, logging);
  31. const app = await bootstrapFn(config);
  32. const defaultChannelToken = await getDefaultChannelToken(logging);
  33. const client = new SimpleGraphQLClient(`http://localhost:${config.port}/${config.apiPath}`);
  34. client.setChannelToken(defaultChannelToken);
  35. await client.asSuperAdmin();
  36. const mockDataService = new MockDataService(client, logging);
  37. if (options.channels) {
  38. await mockDataService.populateChannels(options.channels);
  39. }
  40. const zones = await mockDataService.populateCountries();
  41. await mockDataService.setChannelDefaultZones(zones);
  42. await mockDataService.populateShippingMethods();
  43. const assets = await mockDataService.populateAssets();
  44. const optionGroupId = await mockDataService.populateOptions();
  45. const taxCategories = await mockDataService.populateTaxCategories(zones);
  46. await mockDataService.populateProducts(options.productCount, optionGroupId, assets, taxCategories);
  47. await mockDataService.populateCustomers(options.customerCount);
  48. await mockDataService.populateFacets();
  49. config.authOptions.requireVerification = originalRequireVerification;
  50. return app;
  51. }