Vapor Models
Before we move on to the topic of controllers, we will go back to our ShoppingList
Vapor application and create two Vapor Models that will be saved in the database using Fluent. These two Models are the ShoppingList
Model and the Item Model. Vapor's Model is an extension on top of Fluent's Entity protocol that conveniently implements certain methods in the protocol so we do not have to write the implementation of these methods for every Model we write. Let's see how we can create these two Models using Vapor's Model protocol.
The Shopping List Model
To create a Shopping List Model, all we need to do is inherit our class from the Model protocol, add a few methods that will be used to create an instance of that type using data from the database, and save the state of an instance into the database. Let's perform the following steps to create our first Shopping List Model in Vapor:
- Delete the
Post.swift
Model file included as part of our template project inside theModels
folder. Also...