{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","params":[],"results":{"codes":[]},"settings":""},"next":{"description":"","pages":[]},"title":"Preparation","type":"basic","slug":"preparation","excerpt":"","body":"SQL databases need to be prepared for working with Models. This usually means creating a table with the appropriate columns.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"final class User: Model {\\n    var id: Value?\\n    var name: String\\n    var age: Int\\n}\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\nFor the above User model, a table with three columns named `users` should be created.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"id INTEGER PRIMARY KEY AUTO_INCREMENT, name VARCHAR, age INTEGER\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\nThis syntax changes between databases, and some don't need any preparation. Luckily, Fluent can handle this.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Application Preparations\"\n}\n[/block]\nIf you add the `User` class to your application's preparations array, the database will be prepared for your user upon booting.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"let app = Application(preparations: [\\n    User.self\\n])\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\nIt's recommended you add preparations even if your database is NoSQL incase you decide to use a different database technology in the future.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"vapor run prepare\",\n      \"language\": \"shell\"\n    }\n  ]\n}\n[/block]\nYou can explicitly run the prepare command for Vapor, or the preparations will run the next time the application boots.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Custom Model Preparations\"\n}\n[/block]\nThe 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.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"final class User: Model, RequestInitializable {\\n    var id: Value?\\n    var name: String\\n    var age: Int\\n\\n    ...\\n    \\n    func serialize() -> [String : Value?] {\\n        return [\\n            \\\"id\\\": id,\\n            \\\"name\\\": name,\\n            \\\"age\\\": age\\n        ]\\n    }\\n\\n    static func prepare(database: Database) throws {\\n        try database.create(\\\"users\\\") { creator in\\n            creator.id()\\n            creator.string(\\\"name\\\")\\n            creator.int(\\\"age\\\")\\n        }\\n    }\\n\\n    static func revert(database: Database) throws {\\n        try database.delete(\\\"users\\\")\\n    }\\n}\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\nThe above example shows implementing custom `serialize()`, `prepare()`, and `revert()` methods for your model. Notice how the field names match between `serialize()` and `prepare()`.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Custom Preparations\"\n}\n[/block]\nYou can create custom preparations not tied to models.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"final class CustomTablePreparation: Preparation {\\n    static func prepare(database: Database) throws {\\n        try database.create(\\\"my-table\\\") { creator in\\n            creator.string(\\\"name\\\")\\n            creator.int(\\\"test\\\")\\n        }\\n    }\\n\\n    static func revert(database: Database) throws {\\n        try database.delete(\\\"my-table\\\")\\n    }\\n}\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\nJust make sure to add them to the application's preparation list.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"let app = Application(preparations: [\\n    CustomTablePreparation.self\\n])\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Raw Queries\"\n}\n[/block]\nYou can run raw database queries in the preparations.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"static func prepare(database: Database) throws {\\n    if let mysql = database.driver as? MySQLDriver {\\n        let results = mysql.raw(\\\"SELECT :::at:::@version\\\")\\n    }\\n}\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\nAll 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`.","updates":["5773caf28029ad0e00c38e38"],"order":2,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"5768728407b1f30e0039c72f","project":"57086fe855a4690e005de407","createdAt":"2016-06-20T22:47:32.478Z","__v":17,"githubsync":"","parentDoc":null,"version":{"version":"0.12","version_clean":"0.12.0","codename":"Client Clyde","is_stable":false,"is_beta":true,"is_hidden":false,"is_deprecated":true,"categories":["5768701b72cd4c0e000fad19","5768701b72cd4c0e000fad1a","5768701b72cd4c0e000fad1b","5768701b72cd4c0e000fad1c"],"_id":"5768701b72cd4c0e000fad18","releaseDate":"2016-06-20T22:37:15.641Z","__v":1,"createdAt":"2016-06-20T22:37:15.641Z","project":"57086fe855a4690e005de407"},"user":"57086f3d447c410e00221bd1","category":{"sync":{"isSync":false,"url":""},"pages":[],"title":"Fluent","slug":"fluent","order":2,"from_sync":false,"reference":false,"_id":"5768701b72cd4c0e000fad1b","project":"57086fe855a4690e005de407","__v":0,"version":"5768701b72cd4c0e000fad18","createdAt":"2016-06-20T22:35:07.648Z"}}
SQL databases need to be prepared for working with Models. This usually means creating a table with the appropriate columns. [block:code] { "codes": [ { "code": "final class User: Model {\n var id: Value?\n var name: String\n var age: Int\n}", "language": "swift" } ] } [/block] For the above User model, a table with three columns named `users` should be created. [block:code] { "codes": [ { "code": "id INTEGER PRIMARY KEY AUTO_INCREMENT, name VARCHAR, age INTEGER", "language": "text" } ] } [/block] This syntax changes between databases, and some don't need any preparation. Luckily, Fluent can handle this. [block:api-header] { "type": "basic", "title": "Application Preparations" } [/block] If you add the `User` class to your application's preparations array, the database will be prepared for your user upon booting. [block:code] { "codes": [ { "code": "let app = Application(preparations: [\n User.self\n])", "language": "swift" } ] } [/block] It's recommended you add preparations even if your database is NoSQL incase you decide to use a different database technology in the future. [block:code] { "codes": [ { "code": "vapor run prepare", "language": "shell" } ] } [/block] You can explicitly run the prepare command for Vapor, or the preparations will run the next time the application boots. [block:api-header] { "type": "basic", "title": "Custom Model Preparations" } [/block] 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. [block:code] { "codes": [ { "code": "final class User: Model, RequestInitializable {\n var id: Value?\n var name: String\n var age: Int\n\n ...\n \n func serialize() -> [String : Value?] {\n return [\n \"id\": id,\n \"name\": name,\n \"age\": age\n ]\n }\n\n static func prepare(database: Database) throws {\n try database.create(\"users\") { creator in\n creator.id()\n creator.string(\"name\")\n creator.int(\"age\")\n }\n }\n\n static func revert(database: Database) throws {\n try database.delete(\"users\")\n }\n}", "language": "swift" } ] } [/block] The above example shows implementing custom `serialize()`, `prepare()`, and `revert()` methods for your model. Notice how the field names match between `serialize()` and `prepare()`. [block:api-header] { "type": "basic", "title": "Custom Preparations" } [/block] You can create custom preparations not tied to models. [block:code] { "codes": [ { "code": "final class CustomTablePreparation: Preparation {\n static func prepare(database: Database) throws {\n try database.create(\"my-table\") { creator in\n creator.string(\"name\")\n creator.int(\"test\")\n }\n }\n\n static func revert(database: Database) throws {\n try database.delete(\"my-table\")\n }\n}", "language": "swift" } ] } [/block] Just make sure to add them to the application's preparation list. [block:code] { "codes": [ { "code": "let app = Application(preparations: [\n CustomTablePreparation.self\n])", "language": "swift" } ] } [/block] [block:api-header] { "type": "basic", "title": "Raw Queries" } [/block] You can run raw database queries in the preparations. [block:code] { "codes": [ { "code": "static func prepare(database: Database) throws {\n if let mysql = database.driver as? MySQLDriver {\n let results = mysql.raw(\"SELECT @@version\")\n }\n}", "language": "swift" } ] } [/block] 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`.