In this tutorial we will walk through how to add a database layer to a Vapor project. We will be using MySQL and will assume that you have set it up on your local machine.
Follow these 3 steps if you don’t already have MySQL on your machine.
Install Homebrew:/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Install MySQL:brew install mysql
Start the MySQL server:mysql.server start
More info about MySQL setup here
We are continuing on our ‘Car’ project. You don’t necessary have to have read that tutorial if you already are a Vapor rock star. If not then please find it here.
Open your Vapor project and open the
Package.swift file and add the extra dependency under the main Vapor dependency:
Now open your terminal (make sure you are cd into the project) and run
vapor xcode --mysql, this will make sure everything is compiled correctly with Vapor and linked with MySQL on your local machine.
You should now be able to see some of the new dependencies in the
Dependencies folder in your project. (MySQL, VaporMySQL, FluentMySQL etc).
Now open your
main.swift file and add the following lines of code:
Now let’s jump into our
Car.swift file (and down in the extension of the model) add some actual preparation code. Preparation is also know as migrations in other server side coding frameworks. (More info on how Laravel uses it here)
If you now Run the project then it will compile correctly but crash right after it has started. This is because we are missing a MySQL config file.
So now open terminal again and run:
Open the newly added file and add the following: (Of course depending on your local MySQL settings)
If you try to Run your Xcode project now and get the following:
mysql.server start in your terminal, this makes sure that you have MySQL running on your local machine. You will see a success message when it’s up running. (You can’t continue the tutorial before you have this setup)
When you have a MySQL database setup on your local machine, then try to Run the Xcode project. You should then see the following:
The console is telling you that the model Car is getting setup in the database as a table. You will only get this message(s) the first time you Run the project where you don’t have the tables setup in the database.
And boom, we are up and running. Now check your database interface if you are using any (I’m using SequelPro), you should now be able to see our Car model as a database table (cars). You will also see a table called ‘fluent’, this is Vapor’s way of keeping track of what tables should be created and which already have been created.
Ok, the fun part starts now. Open your controller
CarController.swift and update our
create function to this:
Now try to run the ‘post’ cURL (or use Postman, still an awesome tool).
You should now be able to see that your first entry in a database with Vapor as been stored! Congratulations, huge day.
Now let’s return our stored data, by updating the index function to the following:
(You can also remove the
let cars:[Cars] =  session property if you have that in from earlier tutorial)
Now try to run the ‘get’ endpoint in your browser (or Postman, or cURL, etc).
And voila!, you should now see a JSON response of objects from the database.
Well, you now know how to create routes for controllers and work with models, you know how to store and retrieve from the database. Now go explore and maybe get a job at Nodes if you think you have the skills and what it takes.