Vapor — Fluent as database ORM

What’s Fluent and how to install it

Alexandre Cools
5 min readOct 31, 2020

What’s Fluent?

Fluent is a framework (created by Vapor team) you can use with Vapor to interact with databases. If you want to know more about it, feel free to have a quick look to his documentation:

Fluent is an ORM framework for Swift. It takes advantage of Swift’s strong type system to provide an easy-to-use interface for your database. Using Fluent centers around the creation of model types which represent data structures in your database. These models are then used to perform create, read, update, and delete operations instead of writing raw queries. — Vapor documentation

Wait, what? An ORM?

ORM is for Object Relational Mapping.

Basically, an ORM is a mechanism allowing you to query the data in the database using an object-oriented paradigm.

For example, if you have a MySQL database, you could do something like this:

SELECT * FROM users

And by using an ORM, you could have something like this:

User.query(on: database).all()

So in the first case, you have to know a new language (and what’s about the day you want to change your database system?), while with the ORM, you only have to know how to use it in your favorite programming language and it will work with any database system.

Obviously, you do not have to use an ORM to manipulate a database, but it’s much easier.

Install Fluent in our project

New project

If you don’t have created your project, you can choose to automatically include Fluent at the creation by using the following command:

vapor new [ProjectName]

Once the template generated, the command line will ask you if you want to include Fluent in your project and then you will be able to choose the database you want (Postgres, MySQL, SQLite or Mongo)

You only have to type the number associated to the database you want. Let’s say we choose Postgres, so we have to enter 1 and the press return.

Congratulation, you have create a new project with Fluent.

Note: When you create a new project with Fluent directly installed, it will generate some example files for you to get started. If you know how to use Fluent, you can remove them and if not, you can read my article to learn how to use Fluent 😇.

Existing project

If you already have a project, you can open your Package.swift project file, and then add the Fluent package to the dependencies array. Fluent is only one piece of the puzzle, you will have to add a new package depending on which database you want.

💡 Tips: If you don’t know which database you want with Fluent, you can find all the possibilities you have in the documentation right here.

You will also have to add the module as dependency in the target section.

When you have added Fluent to the Package.swift file, Xcode should install it immediately, so you should see new packages in your Project Navigator.

Congratulation, you have added Fluent to your project.

How to change the database later

Maybe before using your Vapor application in production you will want to change the database you use: we can do it without any code change, because Fluent is an ORM. 🎉

To do it so, you have to open your Package.swift project file, find the dependencies you use for your current database and then, the only thing you have to do here is to replace the driver package with the one who’s for your new database.

Once it’s done, Xcode should update automatically his packages (remove the old and install the new one).

💡 Tips: If Xcode doesn’t update his dependencies by himself, you can do it manually by opening the File menu, scrolling to Swift Packages and then click on Update to Latest Package Versions.

Once your Package.swift and your Xcode are up to date, go to the configure.swift file, replace the import of the previous driver by the new one and also, you have to configure the database in the configure(_ app: Application) function.

I will not do it in this article, but if you have any issue, feel free to contact me or to let a comment. I’ll be happy to help you!

🚀 What’s next?

Don’t forget to “clap” and share this article if you like it and want to see more content like this! 👏

If you have any question, I’ll be happy to read you in the comments section right bellow.

Thanks for reading,

--

--

Alexandre Cools

iOS Developer @LunabeeStudio 👨🏻‍💻 — Sport addict 🏊‍♂️🚴‍♂️🏃‍♂️ — Travel enthusiast ✈️