These docs are for v0.12. Click to read the latest docs for v0.16.

Preparation

SQL databases need to be prepared for working with Models. This usually means creating a table with the appropriate columns.

final class User: Model {
    var id: Value?
    var name: String
    var age: Int
}

For the above User model, a table with three columns named users should be created.

id INTEGER PRIMARY KEY AUTO_INCREMENT, name VARCHAR, age INTEGER

This syntax changes between databases, and some don't need any preparation. Luckily, Fluent can handle this.

Application Preparations

If you add the User class to your application's preparations array, the database will be prepared for your user upon booting.

let app = Application(preparations: [
    User.self
])

It's recommended you add preparations even if your database is NoSQL incase you decide to use a different database technology in the future.

vapor run prepare

You can explicitly run the prepare command for Vapor, or the preparations will run the next time the application boots.

Custom Model Preparations

The automatic preparations use Swift's Mirror to attempt to determine what columns the model will need. This can fail if the model is too complex.

final class User: Model, RequestInitializable {
    var id: Value?
    var name: String
    var age: Int

    ...
    
    func serialize() -> [String : Value?] {
        return [
            "id": id,
            "name": name,
            "age": age
        ]
    }

    static func prepare(database: Database) throws {
        try database.create("users") { creator in
            creator.id()
            creator.string("name")
            creator.int("age")
        }
    }

    static func revert(database: Database) throws {
        try database.delete("users")
    }
}

The above example shows implementing custom serialize(), prepare(), and revert() methods for your model. Notice how the field names match between serialize() and prepare().

Custom Preparations

You can create custom preparations not tied to models.

final class CustomTablePreparation: Preparation {
    static func prepare(database: Database) throws {
        try database.create("my-table") { creator in
            creator.string("name")
            creator.int("test")
        }
    }

    static func revert(database: Database) throws {
        try database.delete("my-table")
    }
}

Just make sure to add them to the application's preparation list.

let app = Application(preparations: [
    CustomTablePreparation.self
])

Raw Queries

You can run raw database queries in the preparations.

static func prepare(database: Database) throws {
    if let mysql = database.driver as? MySQLDriver {
        let results = mysql.raw("SELECT @@version")
    }
}

All database drivers come with a raw method that exposes access to the native database engine. In this example, we cast the database driver to a MySQLDriver. This will be skipped if the database is not being powered by a MySQLDriver.