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 Changes for the Ray Wenderlich Vapor Videos for Vapor 3 RC 2 Ā· GitHub

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: ā€œGitHub - vapor/vapor: šŸ’§ A server-side Swift HTTP web framework.ā€, from: ā€œ3.0.0-rc.2ā€),
.package(url: ā€œGitHub - vapor/fluent-mysql-driver: šŸ–‹šŸ¬ Swift ORM (queries, models, relations, etc) built on MySQL.ā€, 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: ā€œGitHub - vapor/vapor: šŸ’§ A server-side Swift HTTP web framework.ā€, 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! GitHub - raywenderlich/vapor-til: The TIL Application for the Vapor book

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