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?