Posts Tagged With vapor-tutorial

6 posts

Leaf Part 1 - Getting Started

Sunday, November 12, 2017 by Tim

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

Leaf

Up until now, everything we have been doing has been focused on an API and all the requests we have been sending have been sent through a REST client. This is great for iOS apps (and other mobile clients) but at some point we are going to want to have a presence on the web and a way of using the application through a website. We can serve HTML files via Vapor relatively easily but we will want a way to display content from our application. The way to achieve this is to use a templating language where we can use a template to display different things of the same type - for instance, a template to display a reminder. Vapor has it's own templating language, Leaf and in this tutorial we will learn how to use it!

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:

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 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 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: