Type Safe SQL in Swift
Squeal is the sound you make when you realize there's a typo in your SQL statement.
Raw SQL
let query = """
SELECT email FROM users
WHERE id = 1
AND name = 'jack'
LIMIT 1
"""
Using Squeal
let query = SQL
.SELECT(\.email, FROM: users)
.WHERE(\.id == 1)
.AND(\.name == "jack")
.LIMIT(1)
hint: users
is a Table
object that represents the users
table in the database schema.
This is early
Use at your own risk
Only supports Postgres syntax at the moment
-
ORMS have a lot of issues (see below), mainly:
- Need to learn an other pseudo language
- Complex
- Hides what's really going on under the hood
- Performace issues
-
The alternative, writing Pure SQL comes with major drawbacks:
- No more type safety
- No IDE support
What if we could have the best of both worlds?
- Should Read like SQL
- Be Type-safe
- Should Enforce correct SQL Syntax with IDE autocompletion
- Usable in place of raw SQL Strings
- By designing a Swift API that mimics SQL statements almost one to one
- By leveraging the incredible Swift's type system to enforce correctness at compile time
- By using Swift Type system to enforce SQL syntax
- By using Swift's
CustomStringConvertible
protocol to generate raw SQL strings
Don't use an ORM - Prime reacts
The Vietnam of Computer Science
Object-Relational Mapping is the Vietnam of Computer Science
ORM is an anti-pattern
In defence of SQL