I have built a MySQL database with multiple tables and complex relationships, but when I go through the vapor documentation, specifically, in the building the model phase, there is a method for creating the table (that my model class will interact with).
static func prepare(_ database: Database) throws {
try database.create("users") { users in
users.id()
users.string("name")
}
}
However, I don't want to use it because the table that I already have contain foreign keys and types like DATETIME (which I don't know how to declare within the swift context.) is there a way to link my already built tables with vapor?
This is somewhere Vapor (or more correctly Fluent, which is the database level of Vapor) is a bit limited.
Yes, you can use your existing tables. In your
prepare(_:)
method, you can simply leave the implementation empty without creating a table at all. You should also leaverevert(_:)
empty as well.In your
init(node:in:)
initialiser andmakeNode(context:)
method, you will need to map between the column names and types in your table and the property types in your Swift model.