{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","params":[],"results":{"codes":[]},"settings":""},"next":{"description":"","pages":[]},"title":"Model","type":"basic","slug":"model","excerpt":"","body":"Models make interacting with your database objects easy. They support finding, saving, deleting, and even preparing the database.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Simple Example\"\n}\n[/block]\nThe following example shows a `User` model.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"import Vapor\\n\\nfinal class User: Model {\\n    var id: Value?\\n    var name: String\\n    var age: Int\\n\\n    init(name: String, age: Int) {\\n        self.name = name\\n        self.age = age\\n    }\\n\\n    init(serialized: [String: Value]) {\\n        name = serialized[\\\"name\\\"].string ?? \\\"\\\"\\n        age = serialized[\\\"age\\\"].int ?? 0\\n    }\\n}\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\n`init(name: String, age: Int)`\nThis method is not required to be a `Model`, but it allows a convenient way to initialize a new model.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Properties\"\n}\n[/block]\n`id`\nThis model has three properties. The first is the `id: Value?` which is required to be a `Model`. This will contain the identifier for item in the database. It is of type `Value` since different types of databases store identifiers differently.\n\n`name`, `age`\nThe next two properties are just for this user. Their names will match the data stored in the database.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Serialized Initialization\"\n}\n[/block]\n`init(serialized: [String: Value])`\nThis method is required to be a model. It is called to inflate the Swift version of your model from the database version. If you use defaults, all of the field names should match the property names.\n\nIf your properties are non-optional, you must provide a sensible default here in case the database returns `NULL` or does not include the field when queried. \n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Serialize\"\n}\n[/block]\nYou can customize the way the model serializes to the database by overriding the `serialize()` method.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"func serialize() -> [String : Value?] {\\n    return [\\n        \\\"id\\\": id,\\n        \\\"name\\\": name,\\n        \\\"age\\\": age\\n    ]\\n}\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\nThe keys in this method will become the columns in a SQL database or outline the dictionary structure in a NoSQL database.\n\nIf you define your own serialization method, you will most likely need to create custom preparations. See the Preparation section for more information.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Querying\"\n}\n[/block]\nAll of the methods defined in the Query section can be used on a Model instance or type.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"let user = try User.find(1)\\ntry user.delete()\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\nSee the query section to learn more.","updates":[],"order":0,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"5768701b72cd4c0e000fad30","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"},"githubsync":"","__v":9,"createdAt":"2016-06-20T22:35:47.700Z","parentDoc":null,"project":"57086fe855a4690e005de407","user":"57086f3d447c410e00221bd1","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"}}
Models make interacting with your database objects easy. They support finding, saving, deleting, and even preparing the database. [block:api-header] { "type": "basic", "title": "Simple Example" } [/block] The following example shows a `User` model. [block:code] { "codes": [ { "code": "import Vapor\n\nfinal class User: Model {\n var id: Value?\n var name: String\n var age: Int\n\n init(name: String, age: Int) {\n self.name = name\n self.age = age\n }\n\n init(serialized: [String: Value]) {\n name = serialized[\"name\"].string ?? \"\"\n age = serialized[\"age\"].int ?? 0\n }\n}", "language": "swift" } ] } [/block] `init(name: String, age: Int)` This method is not required to be a `Model`, but it allows a convenient way to initialize a new model. [block:api-header] { "type": "basic", "title": "Properties" } [/block] `id` This model has three properties. The first is the `id: Value?` which is required to be a `Model`. This will contain the identifier for item in the database. It is of type `Value` since different types of databases store identifiers differently. `name`, `age` The next two properties are just for this user. Their names will match the data stored in the database. [block:api-header] { "type": "basic", "title": "Serialized Initialization" } [/block] `init(serialized: [String: Value])` This method is required to be a model. It is called to inflate the Swift version of your model from the database version. If you use defaults, all of the field names should match the property names. If your properties are non-optional, you must provide a sensible default here in case the database returns `NULL` or does not include the field when queried. [block:api-header] { "type": "basic", "title": "Serialize" } [/block] You can customize the way the model serializes to the database by overriding the `serialize()` method. [block:code] { "codes": [ { "code": "func serialize() -> [String : Value?] {\n return [\n \"id\": id,\n \"name\": name,\n \"age\": age\n ]\n}", "language": "swift" } ] } [/block] The keys in this method will become the columns in a SQL database or outline the dictionary structure in a NoSQL database. If you define your own serialization method, you will most likely need to create custom preparations. See the Preparation section for more information. [block:api-header] { "type": "basic", "title": "Querying" } [/block] All of the methods defined in the Query section can be used on a Model instance or type. [block:code] { "codes": [ { "code": "let user = try User.find(1)\ntry user.delete()", "language": "swift" } ] } [/block] See the query section to learn more.