get-product-count.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { bootstrapWorker, Logger, ProductService, RequestContextService } from '@vendure/core';
  2. import { devConfig } from './dev-config';
  3. if (require.main === module) {
  4. getProductCount()
  5. .then(() => process.exit(0))
  6. .catch(err => {
  7. Logger.error(err);
  8. process.exit(1);
  9. });
  10. }
  11. async function getProductCount() {
  12. // This will bootstrap an instance of the Vendure Worker, providing
  13. // us access to all of the services defined in the Vendure core.
  14. const { app } = await bootstrapWorker(devConfig);
  15. // Using `app.get()` we can grab an instance of _any_ provider defined in the
  16. // Vendure core as well as by our plugins.
  17. const productService = app.get(ProductService);
  18. // For most service methods, we'll need to pass a RequestContext object.
  19. // We can use the RequestContextService to create one.
  20. const ctx = await app.get(RequestContextService).create({
  21. apiType: 'admin',
  22. });
  23. // We use the `findAll()` method to get the total count. Since we aren't
  24. // interested in the actual product objects, we can set the `take` option to 0.
  25. const { totalItems } = await productService.findAll(ctx, { take: 0 });
  26. Logger.info(
  27. [
  28. '\n-----------------------------------------',
  29. `There are ${totalItems} products in the database`,
  30. '-----------------------------------------',
  31. ].join('\n'),
  32. );
  33. }