Swift Structured Queries for PostgreSQL v0.1.0
Initial release providing type-safe PostgreSQL query building for Swift.
Features
- Type-safe query builder DSL for PostgreSQL
- @table macro for automatic query generation from Swift structs
- Comprehensive PostgreSQL function support (85% of Chapter 9 coverage)
- Advanced SQL features: Window functions, CTEs, JOINs, subqueries
- PostgreSQL-specific operators: JSONB (
@>
,<@
,->
,->>
,#>
), array operations, full-text search (@@
) - Enum table support via CasePaths integration
- Snapshot testing with optional SQL validation against PostgreSQL
Database-Agnostic Core
StructuredQueriesCore
module provides universal SQL types (QueryFragment, Statement, Table protocols)StructuredQueriesPostgres
module adds PostgreSQL-specific functions and operators- Clean separation enables potential multi-database support
CI and Testing
- ✅ Tests pass on macOS (Xcode 26.0) and Linux (Swift 6.1, 6.2)
- ✅ CI completes in ~5 minutes
- ✅ SQL validation gracefully handles missing PostgreSQL (no log spam)
- ✅ No heavy dependencies by default (PostgresNIO optional via trait)
- ✅ 573 tests covering all major SQL features
Installation
Add to your Package.swift:
```swift
dependencies: [
.package(url: "https://github.com/coenttb/swift-structured-queries-postgres", from: "0.1.0")
]
```
Example Usage
```swift
import StructuredQueriesPostgres
@table
struct User {
var id: Int
var name: String
var age: Int
var isActive: Bool
}
// Type-safe queries
let activeUsers = User
.where { $0.isActive && $0.age >= 18 }
.order(by: .name)
.limit(10)
// Execute with swift-records
let users = try await activeUsers.fetchAll(db)
```
Integration with swift-records
This package is designed to work seamlessly with swift-records for database operations:
- swift-structured-queries-postgres builds queries
- swift-records executes queries and manages connections
Credits
This package is a PostgreSQL-focused fork of Point-Free's swift-structured-queries, adapted for PostgreSQL with extensive function coverage and production-ready CI.
Requirements
- Swift 6.0+
- macOS 13+ / iOS 16+ (for build-time macro support)
- PostgreSQL (optional, for SQL validation testing only)
License
This library is released under the MIT license. See LICENSE for details.