Razvan.net

The place for apps & thoughts

Swift on server – Part 2: Basic Vapor / PostgreSQL app

In this Part 2, I’ll create a basic project for saving and retrieving an object (that has just basic types) from the database.

In Part 1 I detailed how to install all the necessary tools in order to create a Swift server side application using the framework Vapor with a PostgreSQL database.

In Part 3 I’ll show how to change the basic model made in this part, and make it use a Date type.


Create a new project

Run in the terminal:

Go to the new created directory:

If you’re on a Mac and want to use Xcode for development (why wouldn’t you?) generate a new Xcode project using:

and open it.

Otherwise just go ahead and use your favorite editor to edit the source files (I highly recommend Visual Code).

Build the app

Using:

and run it:

(In Xcode just make sure you selected the App scheme and run it).

Server starts running on port 8080 (make sure nothing else use it, otherwise you can change it in Config/servers.json)

Open a browser and go to: http://localhost:8080
You should get a “It works.” message.

Now let’s take a look at Sources/App/main.swift which is the entry point of the app.

The instance of Droplet, drop, is the one that is running the server, and that registers the routes.

Let’s add a new route accessible by a GET request that returns some users as JSON

Stop the running application. If you run it from command line: Ctrl-C in the terminal window.
From Xcode just press the stop button.

Rebuild and run the application:

In the navigator go to url: http://localhost:8080/users

You should get the JSON in return:

Make a User Model

Add a new file User.swift in Sources/App/Models/.
If you create it in command line:

if you use Xcode you must regenerate the project file

For start, let’s keep lastconnection date as String.

As part of Model protocol we must provide two extra properties:
id of type Node? that’s nil as long as the model is non saved in database or retrieved from the database.
exists that marks if the instance was retrieved from database or not.
And some extra functions:
– to initialize the object from a Node (init(node..),
– represent the object as a Node (makeNode),
– prepare the database by creating the table where the object will be saved (prepare),
– and removing the table associated with the model (revert).

Let’s modify the route in order to use the User model.

Rebuild, run and reload http://localhost:8080/users and you should get:

Adding PostgreSQL

Getting the Postgresql Provider

Add the Vapor’s postgresql-provider as a dependencies in Package.swift

Build the project

to get the new dependencies and compile them. If you get compilation errors like:

make sure you followed the Part 1 instructions and installed PostgreSQL or at least the libpq-fe.h. It’s a requirement in order to build the Postgres driver.

If you’re using Xcode you must regenerate the project file in order include the new dependencies

then open the new project.

Using the provider in the app

In main.swift prepare the database for use:

We start by importing VaporPostgreSQL then set in the Droplet creation, the preparations to run and the provider of database to use.

Configuration of Postgres connection

Just look at README.md of the provider’s project to get more info on the configuration file.
Short version, create the file: Config/secrets/postgresql.json
Config folder should exists but secrets folder must be created.
In the config file add the following configuration using your database information:

Create some users in database

Add a new route that creates some users in the database:

Rebuild, run and browse to: http://localhost:8080/createusers
You should get the list with the saved users (the ids are no longer null)

Let’s also modify the users route to get all the database users.

While we’re here let’s create a POST route to create an User

Build & Run and let’s try to add a new user.

Use a REST tool that lets you send post requests like Rested (from the Mac App Store) or Postman (multi-platform) and add a body like:

You should get a response:


So, that’s a way to use Vapor with PostgreSQL and using REST for adding end getting an object.

We are now ready to pass to Part 3 of this series of articles to see how to handle Date type instead of String for lastconnection.