Vapor Tutorials

We have created a number of tutorials to help you get going with Vapor. These start from the very beginning with getting going with Vapor and will get more and more advanced. If you would like a tutorial about something, send us a tweet!

Getting Started With Vapor

Sunday, August 27, 2017 by Tim

This is the start of a series of tutorials on Vapor. For more, click here

Vapor

Swift is Apple's programming language that was introduced at the 2014 Worldwide Developers Conference. A year later, in December 2015, it was open sourced and ever since that has been able to run on Linux. Since it was open sourced, a number of frameworks have been created to take advantage of its power and speed. One of those is Vapor and in this tutorial, you will learn how to get up and running with Vapor.

What is Vapor?

Vapor is a web framework that sits on top of Swift to allow you to easily write server-side applications. These could be APIs that you call from an iOS or Android app, or it could be a fully-fledged website, like Twitter. It is comparable to Flask or Django in Python, NodeJS in Javascript, or Ruby on Rails for Ruby.

Fluent And Controllers - Part 1

Monday, September 11, 2017 by Tim

This is the second tutorial of a series of tutorials on Vapor. For more, click here

Fluent

In the last tutorial we learnt how to get started with Vapor and create a new project from a template. However, now we want to actually be able to do something with it! As I mentioned before, this series of tutorials will be based on creating a Reminders app, so today we are going to learn how to save and retrieve reminders from a database.

Before we get started, we need to remove all of the stuff from the template that we aren't going to use. So delete the tests directory (don't worry we'll come back to tests in a future tutorial!) and the files from the Controllers and Models directories in the Sources/App directory:

Fluent Part 2 - Parent-Child Relationships

Thursday, September 28, 2017 by Tim

This is the third tutorial of a series of tutorials on Vapor. For more, click here

Fluent

In the last tutorial we started to put together some parts of our Reminders application and we can now create and view reminders. In this tutorial we are going to add some more complexity to our application by introducing relationships between different models. By the end of this tutorial you will set up a parent-child relationship between a new User model and our existing Reminder model.

If you have been following along with the tutorial from the start, or are using an old template, you will need to upgrade your project for Swift 4. I have already written a short post on the basic steps, but you can also see this commit for the changes required to your Package.swift. Thankfully, at this stage, as we don't have much code, that is all that needs to be done!

Fluent Part 3 - Sibling Relationships

Thursday, October 5, 2017 by Tim

This is the third tutorial of a series of tutorials on Vapor. For more, click here

Fluent

In the last tutorial we looked at adding a parent-child relationship between our reminders and users. In this tutorial we are going to look at sibling relationships and allow reminders to be categorised. By the end of this tutorial you will set up a sibling relationships between a new Category model and our existing Reminder model.

Sibling Relationships

First, let's recap on what sibling relationships are and how they work in Vapor. A sibling relationship is a many-to-many relationship; in our app this means that a reminder can have many categories and a category can contain many reminders. This means that in our models, you can't really link IDs to each other since there will probably be many links.

Fluent Part 4 - Persistence With MySQL And Fluent Patterns

Tuesday, October 24, 2017 by Tim

This is the fifth tutorial of a series of tutorials on Vapor. For more, click here

Fluent

In the last tutorial we rounded off the relationships by adding a new Category model and a sibling relationship between that and our Reminder model. In this tutorial we are going to finish talking about Fluent for now, talk about some of the conventions that are emerging and importantly talk about persistence.

Model Keys

Currently in our model code, we need to define the initialisers for Row and JSON. We also need to tell Fluent how to create the database tables for our models in our Preparation. Until Codable is supported in Fluent 3 we need to manually specify all of they keys for our models. One of the emerging patterns of Fluent is to use a struct to define our keys. This makes sense as generally having hardcoded strings lying around in our code is never really a good idea, especially when they are duplicated! If we create an inner struct in our Model to hold the responsibility for naming our keys, that becomes a lot safer. So in our Reminder model, create an inner struct inside our Model that looks something like: