Exploring our new Vapor Project
Open your newly created project either by opening the newly created folder named SwiftlyGround and double-clicking the Package.swift file. Or if you’re still in the terminal you can navigate to the folder SwiftlyGround and type;
open Package.swift
Either method should launch Xcode and the project will begin loading quite a list of dependencies. I won’t go into what each of these does, in large part because I don’t claim to know what they all do. But these packages are all part of what makes Vapor run and it really isn’t necessary to know what they all do.
Important: Make sure you select your Mac as the run destination for this project. Xcode might select your iPhone by default if you have been building iOS apps. You will get a build error if you try building to an iOS device.
Looking around our new project, SwiftlyGround you should see a file structure that looks like this;
Vapor Starting File structure
Starting at the top with the Package file this is where you’ll see a list of dependencies and their targets. Later on you may add a package here if you wanted to incorporate Sign in with Apple for example. Among many other things.
The Public folder is empty and will likely stay that way for the duration of this project. It could be used to serve static files—such as images, CSS, JavaScript, or HTML directly to the client app without routing through your Swift code.
The Sources folder is where things get interesting and we will be working heavily in the folders within Sources.
Controllers – Handle requests from your client and return responses.
DTO (Data Transfer Objects) – Define the shape of data sent to or from your API. For example, you might have a User model with properties for userName, city, and birthday. While your server stores all three, you may not want to include birthday in every user response. A DTO lets you define a version of the data containing only userName and city, which is safe to send to clients.
Migrations – Manage changes to your database schema over time, such as creating new fields defined on existing tables.
Models – Represent data structures stored in your database. Models map Swift classes to database tables and are used for querying, creating, and updating records.
You can think of your database as a collection of tables, where each table represents a specific type of data. In Vapor, each table corresponds to a Model, and the columns (or fields) in that table represent the model’s properties. For example, our SwiftlyGround coffee shop could be represented by a SwiftlyGround model. Its properties like address, phoneNumber, and hoursOfOperation become the fields of the database table that store the details of the coffee shop.
The configure file is where we will add our Migrations to the API.
The entrypoint file in a Vapor project is where the app starts running. It initializes the application, sets up configurations (like routes, middleware, and database connections), and then launches the server.
Finally our routes file is where all of our controllers get initialized and added to the application.
That’s a good overview of the project. In the next section we will start defining our first model for SwiftlyGround.
I hope you have enjoyed the reading so far. You can reach out to me on various social platforms.
Dan O’Leary