Server Side Swift with Vapor: CRUD Database Options | Ray Wenderlich

Learn how to create, read, update, and delete objects in a database using Vapor, a popular server side Swift framework.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4996-server-side-swift-with-vapor-crud-database-options

very practical guide! thanks for sharing :slight_smile:

My ears was in pain after you have suddenly shout in excitement.

Can edit the video volume in the last few seconds?

@lisadziuba Thanks! :]

@actually Sorry about that! I’ll try and keep an eye on that in the future.

Thank you very much! The guide is really useful and your explanations are very clear. Looking forward for the screencast, that will explain how to build MVC architecture properly in the project.

@booorisss Thanks so much! Yep - I have 2 screencasts on controllers coming up soon :]

Hi Ray,
thanks for these wonderful tutorials! I’ve learned a lot in general from the tutorials and books from your site, and now trying something new (server stuff is unfamiliar territory for me) is very interesting!
I could follow everything in this tutorial, all the functions work, except one.
making a post request and accessing the drop.post(“new”) doesn’t work for me.
I’ve included a screenshot.
Am I missing something (obvious)?
Thanks!
Filip

I have the same exact issue as filipd. I’m not sure what’s wrong

@filipd @mightysasuke It looks like you’re using Postman instead of the app I was using to test (Rested). That’s fine, except you need to make sure to send your data as JSON, rather than as form data (that’s how I set up the app).

To do this, you need to set a header like this:

Then you need to set the body like this (note I selected raw):

I just tested the code from this screencast with the above and it seems to work OK. I hope this helps!

Thanks! that solved the issue!

@rwenderlich I am having similar issue as @filipd. Getting the following error.

Uncaught Error: DatabaseError.invalidSQL("ERROR: relation \"posts\" does not exist\nLINE 1: INSERT INTO posts (content) VALUES ($1)\n ^\n"). Use middleware to catch this error and provide a better response. Otherwise, a 500 error page will be returned in the production environment.

@rishabhtayal That error can’t come from my screencast, as my screencast does not contain a model/table for posts. Please compare your project to the downloadable materials; maybe you have some extra code in there that shouldn’t be.

@rwenderlich In my case I am using the default model and controller class, and adding the post method to the droplet.

drop.post("new") { request in
    var post = try Post(node: request.json)
    try post.save()
    return post
}

@rwenderlich do I need to create the table in the database in terminal first?

The default Post model does have a prepare method, but it has an empty body. So you would need to add something like:
try database.create(“posts”) { posts in
posts.id()
posts(“content”) //etc
}
I’m assume that the default posts stuff is set up so that the CRUD stuff happens in the PostsController (and is routed via drop.resource(“posts”, PostController()).

If you try in the browser doing localhost:8080/posts/show etc it will route to the controller - however the create route (usually not GET) also routes there via a GET route which I don’t quite understand (and it doesn’t via a POST from Rested as Ray did in an earlier screencast). Nor does that create route actually create anything, its currently a mystery to me.

Since Ray is not (yet) using controllers it might be easier following exactly along his screencasts, and creating a new model class rather than use the one created in the default.
HTH

1 Like

Thanks for helping out @rishabhtaya, @johnmacshea!

No thank you, thank you for these great videos, and I see three more have just dropped! Funnily enough I was just going through the controller documentation, et voila - you’ve done all the hard work already. Thanks!

Greetings!

First off, thank you for these amazing screencasts!
I just now began learning Vapor and they have been really helpful to get around its concepts.

I’m getting an exception on PostgreSQLSerializer for the “new” router.
I tried providing an id in the JSON request body, to no avail.
Also, I reverted the database and made sure all preparations were in place.
I’m using Postman as the rest client, and have made sure the parameters are all correct.

I can’t really figure out what’s wrong :frowning:

Hi Ray, first i would like to say thank you because this ScreenCast is very informative easy to understand. This(server side) is very new to me and you make it seem so easy to understand.

I have a question regarding your CRUD tutorial. I’ve experimented with it and this is the result:

The code above is working properly.

What I would like to happen in the other hand is replace the query to search for the field “short”, with the conditions that it is caseInsensitive, and it contains the string. for example, i searched for “af”, it would return the entry “AFK”.

i have managed to almost do it by using database query

let filtered = try postgresql.raw(“select * from acronyms where lower(short) like ‘%(short.lowercased())%’”)

but I cant seem to update the value, because postgresql.raw returns a Node. i cant seem to convert it to an array of Acronym.

Sorry for the terrible english.

Thank you!