populate.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. setConfig(config);
  27. await clearAllTables(config.dbConnectionOptions, logging);
  28. const app = await bootstrapFn(config);
  29. const defaultChannelToken = await getDefaultChannelToken(logging);
  30. const client = new SimpleGraphQLClient(`http://localhost:${config.port}/${config.apiPath}`);
  31. client.setChannelToken(defaultChannelToken);
  32. await client.asSuperAdmin();
  33. const mockDataService = new MockDataService(client, logging);
  34. if (options.channels) {
  35. await mockDataService.populateChannels(options.channels);
  36. }
  37. const zones = await mockDataService.populateCountries();
  38. await mockDataService.setChannelDefaultZones(zones);
  39. await mockDataService.populateShippingMethods();
  40. const assets = await mockDataService.populateAssets();
  41. const optionGroupId = await mockDataService.populateOptions();
  42. const taxCategories = await mockDataService.populateTaxCategories(zones);
  43. await mockDataService.populateProducts(options.productCount, optionGroupId, assets, taxCategories);
  44. await mockDataService.populateCustomers(options.customerCount);
  45. await mockDataService.populateFacets();
  46. return app;
  47. }