v0.3.0
Commands
This release adds commands to make it easy to run custom maintenance, cleanup or productivity tasks with your app.
Commands are built on top of Swift Argument Parser which means there is a ton of baked in functionality for custom flags, options, arguments and automatically generated help details. Commands also have access to your application's services meaning your can easily use your configured Databases, Queues, etc inside commands.
To create a command, first conform to the Command
interface and implement func start()
. You may add options, flags and configuration provided by Swift Argument Parser.
final class SyncUserData: Command {
static var configuration = CommandConfiguration(commandName: "sync", discussion: "Sync all data for all users.")
@Option(help: "Sync data for a specific user only.")
var id: Int?
@Flag(help: "Should data be loaded but not saved.")
var dry: Bool = false
func start() -> EventLoopFuture<Void> {
if let userId = id {
// sync only a specific user's data
} else {
// sync all users' data
}
}
}
Next, register your command type.
app.registerCommand(SyncUserData.self)
Now you may run your Alchemy app with your new command.
$ swift run MyApp sync --id 2 --dry
Make Commands
Also new are a suite of commands to increase productivity by generate typical interfaces you might use such as models, controllers, migrations, middleware, jobs, etc. Run $ swift run MyApp help
for a list of all make commands.
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 Server make:model Todo id:increments:primary name:string is_done:bool user_id:bigint:references.users.id --migration --controller
🧪 create Sources/App/Models/Todo.swift
🧪 create Sources/App/Migrations/2021_09_24_11_07_02CreateTodos.swift
└─ remember to add migration to a Database.migrations!
🧪 create Sources/App/Controllers/TodoController.swift
Like all commands, you may view the details & arguments of each make command with swift run MyApp help <command>
.
Additional Model CRUD functions
Also new are a few functions to help quickly look up, update, or delete models. Check out Model.find
, Model.delete
, and Model.update
for what's new.
For a more in depth summary, check out the Commands guide.