From the course: Using Vapor with SQL and NoSQL Databases

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Overview of the Model protocol

Overview of the Model protocol - Swift Tutorial

From the course: Using Vapor with SQL and NoSQL Databases

Start my 1-month free trial

Overview of the Model protocol

- [Instructor] In Vapor a model is not only a representation of your data, it's also the base protocol for any of your application's models. Especially the ones that you want to persist to a database. A model is only available in Vapor. Its Fluent equivalent is called an entity. When working with a model, you must import Vapor and Fluent. Model objects must conform to the model protocol. When doing so, you'll be informed of some missing items that are required. First, every model must have a property called id. This is your identifier. It's of optional type node, and it's declared like this. Next, you have to provide a means to create a model from the persisted data. To do so, it should be what's called node initializable. To do this, you need to implement a particular init method, and that signature is init, node, in context and it throws. What this function tries to do is when pulling data from a database, it's going to try to create your model object, and in doing so will try to…

Contents