Vapor – Swift Choice For the Back End
Back in February, I did an episode of my podcast where I gave an overview of different backend options when developing a mobile application. In short, when looking for the right option, there is a plethora of choices. For instance, for Swift developers, Vapor has become an increasingly apt choice. Therefore, I decided to deep dive into Vapor and whether it is really a viable choice.
A Plethora Of Choices
However for single developers and developer teams (such as me) which focus on Apple products, there isn’t a dedicated framework for server-side applications. Therefore, the closest option is CloudKit, which is good for data storage but lacks options such as:
- No support outside of web and Apple devices
- No support for complete backups
- Locked into the CloudKit data model for relationships
- No Cron Jobs
However there are other options to remain developing in Swift on server.
Vapor - Swift for the Server
Since Swift was open sourced in late 2015, compatibility for other operating system has been added. One of the first teams to move Swift to the server was the Vapor team. Since the release of version 3.0 in May 2018, interest has grown.
In this article, I will review Vapor and cover:
Therefore, let’s begin by talking about how easy it is to get started.
Getting Started with Vapor
In this article I recently published, I showed how to get started by installing Vapor on your Mac and Ubuntu as well as services like Heroku. In short, I found the process to be pretty simple and effective. That is to say, it is no more complex then any other web framework.
For instance, on the Mac, developers can use Homebrew, which is fairly common, to tap the Vapor repo and install. After you have access to the Vapor CLI tool which includes a subcommand for setting up your Heroku application with the Vapor buildpack.
Likewise on Ubuntu, you add the apt repo for Vapor and install via the apt command. Having done that, you can easily use a combination of a process monitor and an http server to proxy the server.
If you are interested in more details on this process, check out this article which deep dives into Vapor and setting up for macOS, Heroku, and Ubuntu.
Developing with Vapor
Having installed Vapor, the next part of the procedure is developing the app. As a result, the IDE is very important.
Developer Tools with Vapor
In spite of its issues, we can still develop in Swift with Xcode. However, since there are command line tools to build a Swift application and a developer could choose their own tool. At the present time, the real limitation is that Swift in general is only supported on macOS and Ubuntu. That is to say there is no support for other Linux or Unix operating systems as well as no support for Windows. However, the community is certainly moving forward with extending support in those areas. Similarly, the community has provided plenty of packages.
Third Party Support for Vapor
Node.js has npm, Ruby has gems, C# has Nuget, Python has pip and Swift has the Swift Package Manager. Generally speaking, the Swift Package Manager or SPM has been slowly adapted as the premier dependency manager. In the long run while Cocoapods and Carthage still reign in the area of iOS development, it is inevitable that SPM will dominate in the future. Having discussed outside support for Vapor, let’s discuss what Vapor (and its package support currently).
Database Support and Fluent ORM
My experience working with various ORMs are varied. Being that I’ve worked with Entity Framework in C# and Sequelize for Node.Js, I have some experience. Having said that, what makes these work so well is the ability to use lambdas and implement many functional programming paradigms. As a result, since Swift has that ability, using the Fluent ORM is fairly easy to learn.
Equally important is its support of various database software. As of now, there is support for:
As can be seen, there is sufficient database support. Not to mention, but its support for those databases is fairly deep.
Deep Database Support
For instance, I ran into an issue where I was searching for objects by title - a fairly simple keyword search for a REST API list operation. However, what I didn’t realize was that many titles have diacritcs, specifically accents. In other words if a user searches for something with an e if a title has an é, that result won’t show up. In spite of this, there is code in PostgreSQL to unaccent a string, such as there is a way to do lower or upper case transformation. Having adding the necessary functions, I am able to call the function with Fluent’s API:
let columnId = PostgreSQLColumnIdentifier.column("Game", "title") let column = PostgreSQLExpression.column(columnId) let arg = GenericSQLFunctionArgument