{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","params":[],"results":{"codes":[]},"settings":""},"next":{"description":"","pages":[]},"title":"Query","type":"basic","slug":"query","excerpt":"","body":"The Query object allows querying the underlying database using a model.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"let users = try Query<User>().filter(\\\"age\\\", .greaterThan, 21).all()\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Model Types\"\n}\n[/block]\nYou can use the Query methods directly on a model's type.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"let users = User.filter(\\\"age\\\", .greaterThan, 21).all()\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Model Instances\"\n}\n[/block]\nInstances that conform to model have the methods `save()`, `delete()`.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"let user = User(name: \\\"John\\\", age: 25)\\ntry user.save()\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\nThe above user is created, then saved.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"guard let user = User.find(1) else {\\n    throw Abort.badRequest\\n}\\nuser.name = \\\"Jane\\\"\\ntry user.save()\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\nThe above user has the name changed and updated.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"guard let user = User.find(1) else {\\n    throw Abort.badRequest\\n}\\ntry user.delete()\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\nThe above user is fetched then deleted.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Comparisons\"\n}\n[/block]\nThe following comparisons can be made using the `filter()` method.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"public enum Comparison {\\n    case equals\\n    case greaterThan\\n    case lessThan\\n    case greaterThanOrEquals\\n    case lessThanOrEquals\\n    case notEquals\\n}\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Limit\"\n}\n[/block]\nLimit the amount of results received to less than or equal to the number supplied.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"let users = User.filter(\\\"name\\\", \\\"Vapor\\\").limit(5).all()\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Scope\"\n}\n[/block]\n`.in` and `.notIn` operations are supported.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"let users = User.filter(\\\"id\\\", .in, [1, 2, 3]).all()\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Raw Queries\"\n}\n[/block]\nAll database drivers come with a `raw` method. You can cast the database driver to its underlying driver and run raw queries.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"if let mysql = app.database.driver as? MySQLDriver {\\n    let results = mysql.raw(\\\"SELECT :::at:::@version\\\")\\n}\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\nRaw queries will not work if you change the underlying database technology.","updates":[],"order":1,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"57687299d98cfd0e006761ad","createdAt":"2016-06-20T22:47:53.223Z","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"},"__v":24,"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":"","parentDoc":null}
The Query object allows querying the underlying database using a model. [block:code] { "codes": [ { "code": "let users = try Query<User>().filter(\"age\", .greaterThan, 21).all()", "language": "swift" } ] } [/block] [block:api-header] { "type": "basic", "title": "Model Types" } [/block] You can use the Query methods directly on a model's type. [block:code] { "codes": [ { "code": "let users = User.filter(\"age\", .greaterThan, 21).all()", "language": "swift" } ] } [/block] [block:api-header] { "type": "basic", "title": "Model Instances" } [/block] Instances that conform to model have the methods `save()`, `delete()`. [block:code] { "codes": [ { "code": "let user = User(name: \"John\", age: 25)\ntry user.save()", "language": "swift" } ] } [/block] The above user is created, then saved. [block:code] { "codes": [ { "code": "guard let user = User.find(1) else {\n throw Abort.badRequest\n}\nuser.name = \"Jane\"\ntry user.save()", "language": "swift" } ] } [/block] The above user has the name changed and updated. [block:code] { "codes": [ { "code": "guard let user = User.find(1) else {\n throw Abort.badRequest\n}\ntry user.delete()", "language": "swift" } ] } [/block] The above user is fetched then deleted. [block:api-header] { "type": "basic", "title": "Comparisons" } [/block] The following comparisons can be made using the `filter()` method. [block:code] { "codes": [ { "code": "public enum Comparison {\n case equals\n case greaterThan\n case lessThan\n case greaterThanOrEquals\n case lessThanOrEquals\n case notEquals\n}", "language": "swift" } ] } [/block] [block:api-header] { "type": "basic", "title": "Limit" } [/block] Limit the amount of results received to less than or equal to the number supplied. [block:code] { "codes": [ { "code": "let users = User.filter(\"name\", \"Vapor\").limit(5).all()", "language": "swift" } ] } [/block] [block:api-header] { "type": "basic", "title": "Scope" } [/block] `.in` and `.notIn` operations are supported. [block:code] { "codes": [ { "code": "let users = User.filter(\"id\", .in, [1, 2, 3]).all()", "language": "swift" } ] } [/block] [block:api-header] { "type": "basic", "title": "Raw Queries" } [/block] All database drivers come with a `raw` method. You can cast the database driver to its underlying driver and run raw queries. [block:code] { "codes": [ { "code": "if let mysql = app.database.driver as? MySQLDriver {\n let results = mysql.raw(\"SELECT @@version\")\n}", "language": "swift" } ] } [/block] Raw queries will not work if you change the underlying database technology.