Server Side Swift with Kitura by David Okun & Chris Bailey

Server Side Swift with Kitura by David Okun & Chris Bailey

Author:David Okun & Chris Bailey
Language: eng
Format: epub
Publisher: Ray Wenderlich


More complex database queries

As you’ve seen in this chapter, SwiftKueryORM makes it very easy to persist, fetch, update and delete objects from a database without having to work directly with the database or build SQL queries.

However, some apps will need to perform more complex tasks. Specifically, you’ll sometimes want to be able to craft custom SQL queries. Kitura has you covered here as well.

SwiftKueryORM itself is built on a set of lower-level APIs from the SwiftKuery module — a library for building SQL queries in a type-safe manner. This means rather than creating a raw SQL query — an error-prone and potentially dangerous proposition — you can instead use native Swift tools to do this safely, with the compiler’s help.

For example, instead of issuing this raw SQL:

SELECT id, emoji, date FROM journalentry WHERE emoji = "😁";

You could instead write this line of Swift:

let query = Select(entry.id, entry.emoji, entry.date, from: entries).where(entry.emoji = "😁")

Here’s the really nice thing about this implementation: The methods added to your types when you conform them to Model use SwiftKuery in their implementations. This means that you can choose to override these methods with custom queries any time you have need to do so.

You get the convenience of SwiftKueryORM’s convenience methods most of the time and the ability to easily drop down into SQL whenever the need arises!

Before continuing, return to Xcode and stop your server. You’re doing this because, while your project runs in Xcode, it listens on port 8080. You’ll need to have this port available for your Docker image to run in the next section (otherwise, attempts to run it would fail because of the port conflict).



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.