Introduction
Often, you’ll want to run specific tasks around maintenance, cleanup or productivity for your Alchemy app. TheCommand interface makes this a cinche, allowing you to create custom commands to run your application with. It’s built on the powerful Swift Argument Parser making it easy to add arguments, options, flags and help functionality to your custom commands. All commands have access to services registered in Application.boot so it’s easy to interact with whatever database, queues, & other functionality that your app already has.
Included Commands
Run Commands
When Alchemy is run, it takes an argument that determines how it behaves on launch. When no argument is passed, the default command isserve which boots the app and serves it on the machine.
There are also migrate and queue commands which help run migrations and queue workers/schedulers respectively.
You can run these like so.
Run -> Arguments.
If you’re looking to extend your Alchemy app with your own custom commands, check out Commands.
Serve
swift runorswift run Server serve
| Option | Default | Description |
|---|---|---|
| —host | 127.0.0.1 | The host to listen on |
| —port | 3000 | The port to listen on |
| —unixSocket | nil | The unix socket to listen on. Mutually exclusive with host & port |
| —workers | 0 | The number of workers to run |
| —schedule | false | Whether scheduled tasks should be scheduled |
| —migrate | false | Whether any outstanding migrations should be run before serving |
| —env | env | The environment to load |
Migrate
swift run Server migrate
| Option | Default | Description |
|---|---|---|
| —rollback | false | Should migrations be rolled back instead of applied |
| —env | env | The environment to load |
Queue
swift run Server queue
| Option | Default | Description |
|---|---|---|
| —name | nil | The queue to monitor. Leave empty to monitor Queue.default |
| —channels | default | The channels to monitor, separated by comma |
| —workers | 1 | The number of workers to run |
| —schedule | false | Whether scheduled tasks should be scheduled |
| —env | env | The environment to load |
make Commands
Out of the box, Alchemy includes a variety of commands to boost your productivity and generate commonly used interfaces. These commands are prefaced with make:, and you can see all available ones with swift run MyApp help.
For example, the make:model command makes it easy to generate a model with the given fields. You can event generate a full populated Migration and Controller with CRUD routes by passing the --migration and --controller flags.
swift run MyApp help <command>.
Writing A Custom Command
To create a command, conform to theCommand protocol, implement func start(), and register it with app.registerCommand(...). Now, when you run your Alchemy app you may pass your custom command name as an argument to execute it.
For example, let’s say you wanted a command that prints all user emails in your default database.
Application.boot
print argument to run your command.
Adding Options, Flags, and help info
BecauseCommand inherits from Swift Argument Parser’s ParsableCommand you can easily add flags, options, and configurations to your commands. There’s also support for adding help & discussion strings that will show if your app is run with the help argument.
swift run MyApp sync --id 2 --dry and it run with the given arguments.
Printing help info
Out of the box, your server can be run with thehelp argument to show all commands available to it, including any custom ones your may have registered.
configuration, options, flags, etc.
-e, --env <env-file> to any command to have it load your environment from a custom env file before running.
