So, as per the title, I'm new to Vapor and Swift in general, so I've been battling for about a month now trying to get things to return from an existing database.
We're using Vapor 3.
I'm coming from PHP, and of course, we have a database already. For a range of reasons, Fluent's migration system isn't an option for us. We don't want it to be screwing with the database when we build.
With that said, I've set up a model, and have been attempting to run queries on it. It builds successfully, and then gives the error:
{"error":true,"reason":"
Model.defaultDatabase
is required to use request asDatabaseConnectable
."}
Our configure.swift looks like this.
import Vapor
import Leaf
import MySQL
public func configure(
_ config: inout Config,
_ env: inout Environment,
_ services: inout Services
) throws {
// Register routes to the router
let router = EngineRouter.default()
try routes(router)
services.register(router, as: Router.self)
let myService = NIOServerConfig.default(port: 8080)
services.register(myService)
try services.register(LeafProvider())
try services.register(MySQLProvider())
config.prefer(LeafRenderer.self, for: ViewRenderer.self)
/// Register custom MySQL Config
let mysqlConfig = MySQLDatabaseConfig(
hostname: "<ip>",
port: 3306,
username: "<user>",
password: "<password>",
database: "<database>"
)
services.register(mysqlConfig)
}
Our model is written to the standard expressed here: https://docs.vapor.codes/3.0/mysql/fluent/#model
And our controller has this:
import Vapor
import FluentMySQL
final class NewsController {
func index(_ req: Request) throws -> Future<View> {
let news = News.query(on: req).all()
return try req.view().render("newsIndex", news)
}
}
Any idea where I'm going wrong?