populate.ts 2.4 KB

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