populate.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  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 { MockDataService } from './mock-data.service';
  6. import { SimpleGraphQLClient } from './simple-graphql-client';
  7. export interface PopulateOptions {
  8. logging?: boolean;
  9. productCount: number;
  10. customerCount: number;
  11. }
  12. // tslint:disable:no-floating-promises
  13. /**
  14. * Clears all tables from the database and populates with (deterministic) random data.
  15. */
  16. export async function populate(
  17. config: VendureConfig,
  18. bootstrapFn: VendureBootstrapFunction,
  19. options: PopulateOptions,
  20. ): Promise<INestApplication> {
  21. (config.dbConnectionOptions as any).logging = false;
  22. const logging = options.logging === undefined ? true : options.logging;
  23. setConfig(config);
  24. await clearAllTables(config.dbConnectionOptions, logging);
  25. const app = await bootstrapFn(config);
  26. const client = new SimpleGraphQLClient(`http://localhost:${config.port}/${config.apiPath}`);
  27. const mockDataClientService = new MockDataService(client, logging);
  28. await mockDataClientService.populateOptions();
  29. await mockDataClientService.populateProducts(options.productCount);
  30. await mockDataClientService.populateCustomers(options.customerCount);
  31. await mockDataClientService.populateAdmins();
  32. return app;
  33. }