Stand-alone CLI Scripts
It is possible to create stand-alone scripts that can be run from the command-line by using the bootstrapWorker function. This can be useful for a variety of use-cases such as running cron jobs or importing data.
Minimal example
Here's a minimal example of a script which will bootstrap the Vendure Worker and then log the number of products in the database:
This script can then be run from the command-line:
resulting in the following output:
The app object
The app object returned by the bootstrapWorker() function is an instance of the NestJS Application Context. It has full access to the NestJS dependency injection container, which means that you can use the app.get() method to retrieve any of the services defined in the Vendure core or by any plugins.
Creating a RequestContext
Almost all the methods exposed by Vendure's core services take a RequestContext object as the first argument. Usually, this object is created in the API Layer by the @Ctx() decorator, and contains information related to the current API request.
When running a stand-alone script, we aren't making any API requests, so we need to create a RequestContext object manually. This can be done using the RequestContextService: