November 2018
Intermediate to advanced
404 pages
10h 16m
English
To read all of your records in a database, use query(on:) to query the database and all() to list all of them:
func getAll(_ req: Request) throws -> Future<View> { return JournalEntry.query(on: req).all().flatMap(to: View.self) { entries in let context = JournalContext(title: self.title, author: self.author, count: String(entries.count), entries: entries) let leaf = try req.make(LeafRenderer.self) return leaf.render("main", context) }}
If you know the ID of an item, you can use find(_:on:) instead of query(on:). The following code shows you how to retrieve an item by its id:
func getEntry(_ req: Request) throws -> Future<View> { let id = try req.parameters.next(Int.self) return JournalEntry.find(id, on: req).flatMap(to: ...Read now
Unlock full access