Fluent Database in Swift

Mitesh Dewda
Globant
Published in
6 min readAug 18, 2022
image source: https://github.com/vapor/fluent

What is Fluent?

Fluent is an open-source ORM (Object Relational Mapper) framework, which is a part of the Vapor project. It provides an abstraction layer between the Vapor application and the database. It saves us from writing queries to interact with databases directly and rather provide an easy-to-use interface for our database.

Although Fluent is a part of the Vapor project, it is independent and capable enough to manage database connections on its own.

What is ORM ?

When we have a relational database we can always use SQL and write our SQL query to get the data. But what if we don’t want to write SQL by ourselves and want to use the power of Swift or any other programming language to just use some helper functions and get all the data in the way we want. Such as just writing Movie.all() to get all the Movies data. That’s where ORMs come into the picture!

ORM works on relational databases and allows us to communicate relational database tables and records in the form of objects. So in other words it’s a mapping between Objects and Relational databases.

Fluent Driver can be used to connect these two above things, meaning we can create these models and these classes in our swift code that can actually talk to the Database and get the stuff from the database like getting all the movies from the database.

So the real benefit we get when we use ORM is that we don’t deal with writing SQL queries, we only deal with objects and classes whether we are using JavaScript/Java etc. each one has its ORMs and we use the classes to access the database tables. Each table is represented by the name of the class and each column is represented by the property in the class model.

Adding Fluent to the Vapor project

To use Fluent in our Vapor project, we need to either include Fluent while creating the project or it can be added later in the existing project.

Steps for both are mentioned below -

Step 1: Adding Fluent while creating a Vapor project.

  • Go to the terminal > run vapor new <project-name>
  • After pressing enter, we will get a prompt asking “Would you like to use Fluent?” select y
  • Then we will get a prompt asking “Which database would you like to use?” Select the number corresponding to the database names mentioned.
  • After pressing enter, we will get a prompt asking “Would you like to use Leaf?” Select the option as per requirement and press enter.
  • After pressing enter, our project having Fluent configurations will be created.
  • Navigate to the project folder, type the command “open package.swift” and press enter. This will open our project in Xcode Editor.
  • Fluent configurations can be verified in the Package.swift file at our project’s root level and the fluent dependencies can be verified under Project Dependencies as seen below.

Step 2: Adding Fluent to the existing Vapor project

To add Fluent to the existing project, we need to add two dependencies to our package:

The <db> in the second URL is the database driver which we use to connect Fluent with our database. Eg. — fluent-postgres-driver.

This driver URL is also available in the official Vapor documentation for Fluent, mentioned above.

  • Add the Fluent dependency to the target dependencies in Package.swift. Like below -
  • Once the packages are added as dependencies, we can configure our databases using app.databases in the file configure.swift.

Models in Fluent

In a Database, we have Models, which are the primary artifacts in a Database Management system. Models show the logical structure of a database, including the relationships and the constraints that determine how data can be stored and accessed. A Database Model can include entities, their relationships, data flow, tables, etc.

Similarly, we have Models in Fluent also. Models are the heart of Fluent. They are the swift representation of our data and are used throughout Fluent. As per Vapor’s official documentation — “Models represent fixed data structures in our database, like tables or collections.”. Below is a Model from Vapor docs, which represents a galaxy.

If we look at the above Model, it is just a Swift class that conforms to a Model protocol. A Protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. The protocol can be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements. When we extend a Protocol, apart from the actual implementation of the protocol, we can also implement some additional functionality per our requirement. Multiple protocols can be extended by separating them with a comma. Like below -

So overall we can say that Models are the objects that we save and access in our database. Models in fluent are type-safe, so when we interact with databases in Fluent, a Model gives us compile-time safety.

In a swift project, we create Models directory under Sources > Apps and store our Models. It is not necessary to create a separate Models directory, it’s just to maintain the separation of concern.

The below snippet shows a Todo Model that conforms to two Protocols.

Conclusion

This article just talks about Fluent ORM, how to add it to our Vapor project and Models in Fluent. There are further subtopics in Fluent that one needs to know to use Fluent properly in a project, such as Fluent Schema, migration in Fluent, etc. Those can be referred to at Vapor’s official documentation on Fluent.

Reference

--

--