Server Side Swift with Vapor: Persisting Models | Ray Wenderlich

Learn how to create model objects that you can persist in a database with Vapor, a popular server side Swift framework.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4960-server-side-swift-with-vapor-persisting-models

Update: I should have called the var passed to the prepare closure acronyms rather than users to be more clear. Calling it users was a holdover from an old version of the project.

I am loving these Vapor videos. Thanks.

1 Like

Thank you Ray!!!, I come from Laravel, and I wish learn Swift on Server Side, it’s cool. Thanks. From Chimbote - Peru.

Thanks you so much Ray for all the Vapor tutorials.
They work fine on my laptop but all related to database doesn’t work on Heroku.
Is there a specific configuration or 'hack" to know ?

– Pourpre

@kamcma @keyner Thanks so much! :]

@pourpre There is indeed. To get this to work on Heroku, you need to install a PostgreSQL DB on the cloud and configure your Heroku app to point to it. Good news - that’s the subject of my next Screencast :] Hope you enjoy it and thanks for reading our site! :]

is there a reason why you used touch to add a file to the project versus just doing it via the XCode IDE? maybe a stupid question but I am just curious.

@dniswhite Yes - my understanding is that it’s best to use the file system for all file & directory creation, and treat the Xcode project as a “black box” generated by vapor xcode. I think of the xcode project as a “convenient alternative” way to build the project, with the primary method being vapor build (which runs swift build under the hood). If anyone understands this differently, let me know.

to avoid the warnings from the deprecation I changed the constructor for the Droplet to look like the following:

let drop = Droplet()

// add provider
drop.addProvider(VaporPostgreSQL.Provider.self)

// add models (preparations)
drop.preparations = Array([
ModelName.self
])

maybe there is a better option so figured I would share here in case others have another approach.

Thx @dniswhite! I noticed the deprecation warning as well and fixed it in a future screencast in this series with the following:

let drop = Droplet()
try drop.addProvider(VaporPostgreSQL.Provider)
drop.preparations += Acronym.self
1 Like

Is it possible to set up a model with a data type of Data? For example, say I were to save an image that was uploaded via a multipart form post?

Hello, Ray!
Thank you for Vapor tutorials! That’s cool!!!
But I can’t fix a bug.
When I launch the application, i see in console:
Could not initialize provider Provider: noPostgreSQLConfig
No command supplied, defaulting to serve…
Can not run preparations, droplet has no database
Program ended with exit code: 1

How to fix it?
Best regards, Vlad.

I’m really enjoying these videos, but I’ve come across a strange behavior that I’m trying to figure out.

This drop results in a the same data being inserted twice each time it’s called:

drop.get(“test”) { request in
var acronym = Acronym(short: “OMW”, long: “On My Way”)
try acronym.save()
return try JSON(node: Acronym.all().makeNode())
}

Actually any save() does that. All the code in Acronym.swift is a straight copy from the video. I should add this is Xcode 8 as well. Is this something I’m doing wrong, based on this snippet?

I wrote these two

drop.get("add-data") { req in
    var acronym = Acronym(short: "YOLO", long: "You Only Live Once")
    try acronym.save()
    return "made acronym"
}

drop.get("get-data") { req in
    return try JSON(node: Acronym.all().makeNode())
}

I am new to server-side programming so I don’t know how web browsers work. In the url field, I typed in …/add-data and pressed return key. the DB works fine. then typed …/get-data it reads well. however, when i change the letters ‘get’ to ‘add’ in the url field of a web browser. the drop.get(“add-data”) already executes before pressing Return key. and when I press Return key it invokes one more time. So the data got written twice to the DB. Is this normal?

Hi, Rey. I try this

get("user") { req in
      var user = User(email: "mymail@mail.com", name: "My Name")
      try user.save()
      return try JSON(User.all().makeNode())
    }

It is error where makeNode must have context (Missing argument for parameter in:). How you make able to just call makeNode() func?