populate.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { INestApplication } from '@nestjs/common';
  2. import { VendureBootstrapFunction } from '../src/bootstrap';
  3. import { setConfig, VendureConfig } from '../src/config/vendure-config';
  4. import { clearAllTables } from './clear-all-tables';
  5. import { getDefaultChannelToken } from './get-default-channel-token';
  6. import { MockDataService } from './mock-data.service';
  7. import { SimpleGraphQLClient } from './simple-graphql-client';
  8. export interface PopulateOptions {
  9. logging?: boolean;
  10. productCount: number;
  11. customerCount: number;
  12. }
  13. // tslint:disable:no-floating-promises
  14. /**
  15. * Clears all tables from the database and populates with (deterministic) random data.
  16. */
  17. export async function populate(
  18. config: VendureConfig,
  19. bootstrapFn: VendureBootstrapFunction,
  20. options: PopulateOptions,
  21. ): Promise<INestApplication> {
  22. (config.dbConnectionOptions as any).logging = false;
  23. const logging = options.logging === undefined ? true : options.logging;
  24. setConfig(config);
  25. await clearAllTables(config.dbConnectionOptions, logging);
  26. const app = await bootstrapFn(config);
  27. const defaultChannelToken = await getDefaultChannelToken(config.dbConnectionOptions, logging);
  28. const client = new SimpleGraphQLClient(
  29. `http://localhost:${config.port}/${config.apiPath}?token=${defaultChannelToken}`,
  30. );
  31. const mockDataClientService = new MockDataService(client, logging);
  32. await mockDataClientService.populateOptions();
  33. await mockDataClientService.populateProducts(options.productCount);
  34. await mockDataClientService.populateCustomers(options.customerCount);
  35. await mockDataClientService.populateFacets();
  36. await mockDataClientService.populateAdmins();
  37. return app;
  38. }