Server Side Swift with Vapor - Part 15: Persisting | Ray Wenderlich

Learn how to set up your Vapor application to use MySQL as a database, using Docker, so your data is persisted.


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

This might be an obvious point, but just mentioning it for the sake of those who might not have thought of this :slight_smile:

For those who already have MySQL running on their machine either via something like MAMP or XAMPP or some other means, you don’t really have to install Docker - unless you really want to, of course :slight_smile: You can simply use your existing MySQL server to work with Vapor. While I do have Docker set up already, I also have an existing MySQL installation via MAMP and I simply used that for the tutorial.

I do realize why Docker was used for the tutorial since that is easier than running the viewer/student through a full MySQL installation, but just mentioning this in case this does prove to be useful to someone out there …

I’m getting 2 MySQL Compiling errors (for the build end of the video):
type ‘SHA1’ has no member 'digest’
value of type ‘Data’ has no member ‘hexEncodedString’

https://drive.google.com/open?id=1kYcLmKI7TbYUyuvFSF1kn54tdYo4SEU9
https://drive.google.com/open?id=1e03yQyqnn3xELxEBw-lRUfEfpJ-WF8mR
https://drive.google.com/open?id=1SEvio4I-aNdhy_tMDGVcjm4G1NIkUQox
https://drive.google.com/open?id=1BsbidHQtsjYRldiAR9Vw4tMNzjsjKssR

@amyerson I’m not seeing those errors, can you make sure your Package.swift looks like:

// swift-tools-version:4.0
import PackageDescription

let package = Package(
    name: "TILApp",
    dependencies: [
        // 💧 A server-side Swift web framework.
        .package(url: "https://github.com/vapor/vapor.git", from: "3.0.0-rc"),
        .package(url: "https://github.com/vapor/fluent-mysql.git", from: "3.0.0-rc"),
    ],
    targets: [
        .target(name: "App", dependencies: ["FluentMySQL", "Vapor"]),
        .target(name: "Run", dependencies: ["App"]),
        .testTarget(name: "AppTests", dependencies: ["App"]),
    ]
)

And run vapor update in Terminal to make sure you have the latest RC

I’m getting an error, after I changed my pivot to a MySQLUUIDPivot, the searchHandler method broke.

return try Acronym.query(on: req).group(or) { or in
        or.filter(\.short == searchTerm).all()
        or.filter(\.long == searchTerm).all()
    }.all()

I get the “Use of unresolved identifier or” message. The “OR” giving the error is the one inside the .group(or). I’ve been messing around with it, but couldn’t find a way to fix.

@andrekilik :thinking: change the Pivot shouldn’t affect that file! Are you on RC2?

On RC2, I had to re-write the search handler as follows in order to get it to compile:

return try Acronym.query(on: req).group(.or, closure: {(or) in
	try or.filter(\.short == term)
	try or.filter(\.long == term)
}).all()

Note the two try statements within the closure :slight_smile:

@fahim in case you haven’t seen it, all the changes required for RC2 are here https://gist.github.com/0xTim/b372f91c8bd71ea757d1da526058f2d9

Thanks, Tim :slight_smile: I did see that and have it bookmarked. I was just responding to @andrekilik since I believe the error he was talking about was the one I mentioned.

1 Like

Hello,

This is my Package.swift

$ cat Package.swift
// swift-tools-version:4.0
import PackageDescription

let package = Package(
name: “TILApp”,
dependencies: [
// :droplet: A server-side Swift web framework.
.package(url: “https://github.com/vapor/vapor.git”, from: “3.0.0-rc.2”),
.package(url: “https://github.com/vapor/fluent-mysql.git”, from: “3.0.0-rc.2”)
],
targets: [
.target(name: “App”, dependencies: [“FluentMySQL”, “Vapor”]),
.target(name: “Run”, dependencies: [“App”]),
.testTarget(name: “AppTests”, dependencies: [“App”])
]
)

I was also getting the same error as @amyerson regarding the “digest” and “encoding”

Then after doing “vapor update” seems everything is updated and no longer have those errors.

Is it all working again then?

Thank you very much Tim. It is working again, yes. I do not have those 2 MySQL compiling errors anymore.

1 Like

I keep getting the compile error 'Missing required module ‘CSQLite’.

This is Package.swift

// swift-tools-version:4.0
import PackageDescription

let package = Package(
name: “TILApp”,
dependencies: [
// :droplet: A server-side Swift web framework.
.package(url: “https://github.com/vapor/vapor.git”, from: “3.0.0-rc”),

    // 🔵 Swift ORM (queries, models, relations, etc) built on SQLite 3.
    .package(url: "https://github.com/vapor/fluent-mysql.git", from: "3.0.0-rc")
],
targets: [
    .target(name: "App", dependencies: ["FluentMySQL", "Vapor"]),
    .target(name: "Run", dependencies: ["App"]),
    .testTarget(name: "AppTests", dependencies: ["App"])
]

)

Does anyone know a way to get this to compile?

@andy4202 that usually means you have leftover references to SQLite somewhere in your code, something like an import FluentSQLite or SQLiteModel lying around somewhere. If you do rm -rf .build to remove any old build artefacts and regenerate the project it should point of the actual error if you can’t find it

1 Like

It seems like PostgreSQL setup is quite different than what you’ve shown. Any chance you’ll be doing a video on using that database? For example, I don’t see anything like the automated model setup interfaces that you used with MySQL

@gargoyle they should be almost identical. See this project which is similar but uses Postgres instead of MySQL

@0xtim Please include the link :slight_smile:

:man_facepalming:

Here you go! https://github.com/raywenderlich/vapor-til

Hi Tim,

cool tutorial - very good - up to now I was successful in building it as described and shown in the video clips.

I hope this is the right place to post my question, when running the project from Xcode (already changed to FluentMySQL DB connection) I get the following error message

Thread 1: Fatal error: Error raised at top level: :warning:️ MySQL Error: Unsupported auth plugin: caching_sha2_password

  • id: MySQLError.authPlugin

File: /App-Programmierung/DB_Access/VAPOR/TILAPP/.build/checkouts/mysql.git-1890032512239690518/Sources/MySQL/Connection/MySQLConnection+Authenticate.swift

  • func: authenticate(username:database:password:)
  • line: 46
  • column: 143

Username/Password/Database is set correct as stated within docker MySQL command, and MySQL within docker is running.

Package.swift is correct as stated in your post.

It would be fine if you can give me a hint what I have done wrong.

Many thanks!!

Alex

@a_stefka you haven’t done anything wrong! Unfortunately MySQL 10.0.2(?) introduced a new default authentication module that Vapor doesn’t support yet. There’s an issue open but for now you can do:

docker run --name mysql -e MYSQL_USER=vapor \
  -e MYSQL_PASSWORD=password -e MYSQL_DATABASE=vapor \
  -p 3306:3306 -d mysql/mysql-server:5.7

This specifies the 5.7 MySQL Server which still works