A Swift ORM for PostgreSQL, inspired by Elixir's Ecto. Property-wrapper schemas, a composable query builder, actor-based concurrency, and a CLI for migrations.
- Features
- Requirements
- Installation
- Quick Start
- Schema Definition
- CRUD Operations
- Query Builder
- Relationships
- Transactions
- Aggregates and GROUP BY
- Field Selection
- CLI Reference
- Architecture
- Configuration
- Development
- Testing
- Troubleshooting
- License
- Property-wrapper schema definitions --
@ID,@Column,@Timestamp,@ForeignKey,@HasMany,@HasOne,@BelongsTo,@ManyToMany @Schemamacro -- generatesSchemaBuilderconformance at compile time (zero boilerplate)- Generic primary keys --
@ID<UUID>,@ID<Int>,@ID<String>via thePrimaryKeyTypeprotocol - Immutable query builder --
Query<T>is a value type; every.where(),.join(),.orderBy()returns a new query - Type-safe aggregates --
.sum(),.avg(),.min(),.max(),.count()withGROUP BYandHAVINGsupport - Upsert and bulk insert --
ON CONFLICTupserts and multi-row inserts with automatic batching - Relationship preloading -- batch-loads
HasMany,HasOne,BelongsTo, andManyToManyrelationships to prevent N+1 queries - Transaction support --
READ COMMITTEDisolation with automatic rollback; full CRUD and query builder available inside transactions viaQueryExecutor - Actor-based connection pooling -- built on SwiftNIO and PostgresKit
- Plain SQL migrations -- timestamped
.sqlfiles with-- migrate:up/-- migrate:downmarkers - CLI tool --
spectrobinary for database creation, migrations, and status - Swift 6 strict concurrency -- full
Sendablecompliance across all types
- Swift 6.0+ (managed via
mise.toml) - macOS 13+
- PostgreSQL
Add to your Package.swift:
.package(url: "https://github.com/Maartz/Spectro.git", from: "1.0.0")Then add "SpectroKit" to your target's dependencies:
.target(
name: "MyApp",
dependencies: [
.product(name: "SpectroKit", package: "Spectro"),
]
)The spectro CLI is distributed via Mint:
mint install Maartz/SpectroPin a version in your Mintfile:
Maartz/Spectro@1.0.0
import Spectro
@Schema("users")
struct User {
@ID var id: UUID
@Column var name: String
@Column var email: String
@Timestamp var createdAt: Date
}The @Schema macro generates Schema and SchemaBuilder conformance at compile time -- no manual init() or build(from:) required.
let spectro = try Spectro(
hostname: "localhost",
username: "postgres",
password: "postgres",
database: "myapp_dev"
)
// Insert
let user = try await spectro.insert(User())
// Query
let repo = spectro.repository()
let activeUsers = try await repo.query(User.self)
.where { $0.name == "John" }
.orderBy({ $0.createdAt }, .desc)
.limit(10)
.all()
// Get by ID
let found = try await spectro.get(User.self, id: someUUID)
// Update
let updated = try await spectro.update(User.self, id: someUUID, changes: ["name": "Jane"])
// Delete
try await spectro.delete(User.self, id: someUUID)
// Shutdown (always call before releasing)
await spectro.shutdown()The @Schema("table_name") macro generates everything needed to map a struct to a database table:
static let tableNamefrom the string argument- A default
init()with type-appropriate defaults - A convenience
init(column params...)for@Columnand@ForeignKeyproperties SchemaBuilder.build(from:)for row mapping
@Schema("users")
struct User {
@ID var id: UUID
@Column var name: String
@Column var email: String
@Column var bio: String?
@Timestamp var createdAt: Date
}| Wrapper | Purpose | Example |
|---|---|---|
@ID<T> |
Primary key (UUID, Int, or String) | @ID var id: UUID |
@Column<T> |
Regular column, optional name override | @Column("display_name") var name: String |
@Timestamp |
Date column | @Timestamp var createdAt: Date |
@ForeignKey<T> |
Foreign key reference, optional name override | @ForeignKey var userId: UUID |
@HasMany<T> |
One-to-many relationship, optional FK binding | @HasMany(foreignKey: "authorId") var posts: [Post] |
@HasOne<T> |
One-to-one relationship | @HasOne var profile: Profile? |
@BelongsTo<T> |
Inverse of HasMany/HasOne | @BelongsTo var user: User? |
@ManyToMany<T> |
Many-to-many via junction table | @ManyToMany(junctionTable: "user_tags") var tags: [Tag] |
Primary keys are not limited to UUID. Any type conforming to PrimaryKeyType can be used. Built-in conformances: UUID, Int, String.
@Schema("articles")
struct Article {
@ID var id: Int // SERIAL primary key
@Column var title: String
}
@Schema("slugs")
struct Slug {
@ID var id: String // TEXT primary key
@Column var target: String
}Foreign keys match the primary key type of the referenced table:
@Schema("comments")
struct Comment {
@ID var id: UUID
@Column var body: String
@ForeignKey var articleId: Int // references Article's Int PK
}By default, Swift property names are converted to snake_case for column names. Override with a string argument:
@Schema("users")
struct User {
@ID var id: UUID
@Column("display_name") var name: String // maps to "display_name" column
@ForeignKey("team_ref_id") var teamId: UUID // maps to "team_ref_id" column
}If you prefer not to use the macro, implement Schema and SchemaBuilder manually:
struct User: Schema, SchemaBuilder {
static let tableName = "users"
@ID var id: UUID
@Column var name: String
@Column var email: String
@Timestamp var createdAt: Date
init() {
self.id = UUID()
self.name = ""
self.email = ""
self.createdAt = Date()
}
static func build(from values: [String: Any]) -> User {
var user = User()
if let v = values["id"] as? UUID { user.id = v }
if let v = values["name"] as? String { user.name = v }
if let v = values["email"] as? String { user.email = v }
if let v = values["createdAt"] as? Date { user.createdAt = v }
return user
}
}All CRUD is available on both the Spectro facade and the GenericDatabaseRepo actor (via spectro.repository()).
let repo = spectro.repository()
// Insert a single record
let user = try await repo.insert(User(name: "Alice", email: "alice@example.com"))
// Insert with explicit primary key (e.g. for seeding)
let admin = try await repo.insert(
User(id: knownUUID, name: "Admin", email: "admin@example.com"),
includePrimaryKey: true
)
// Get by primary key
let found = try await repo.get(User.self, id: someUUID)
// Get or throw SpectroError.notFound
let mustExist = try await repo.getOrFail(User.self, id: someUUID)
// Fetch all
let everyone = try await repo.all(User.self)
// Update by ID
let updated = try await repo.update(User.self, id: someUUID, changes: [
"name": "Bob",
"email": "bob@example.com"
])
// Delete by ID
try await repo.delete(User.self, id: someUUID)Insert or update on conflict using ConflictTarget:
// Upsert on column conflict -- updates all non-PK columns
let user = try await repo.upsert(
User(name: "Alice", email: "alice@example.com"),
conflictTarget: .columns(["email"])
)
// Upsert with specific columns to update
let user = try await repo.upsert(
User(name: "Alice", email: "alice@example.com"),
conflictTarget: .columns(["email"]),
set: ["name"] // only update name on conflict
)
// Upsert on named constraint
let user = try await repo.upsert(
User(name: "Alice", email: "alice@example.com"),
conflictTarget: .constraint("users_email_unique")
)Insert multiple records in a single query. Automatically batches at 1000 rows to stay under PostgreSQL's parameter limit:
let users = [
User(name: "Alice", email: "alice@example.com"),
User(name: "Bob", email: "bob@example.com"),
User(name: "Carol", email: "carol@example.com"),
]
let inserted = try await repo.insertAll(users)Query<T> is an immutable value type. Every method returns a new query, so you can safely branch and reuse intermediate queries.
let repo = spectro.repository()
let base = repo.query(User.self)
.where { $0.isActive == true }
// Branch 1: recent users
let recent = try await base
.orderBy({ $0.createdAt }, .desc)
.limit(10)
.all()
// Branch 2: count
let total = try await base.count()// Equality
.where { $0.name == "John" }
.where { $0.status != "banned" }
// Comparison
.where { $0.age >= 18 }
.where { $0.score < 100 }
// String patterns (case-sensitive)
.where { $0.name.like("J%") }
.where { $0.name.contains("ohn") } // LIKE '%ohn%'
.where { $0.email.endsWith("@gmail.com") } // LIKE '%@gmail.com'
.where { $0.name.startsWith("J") } // LIKE 'J%'
// String patterns (case-insensitive)
.where { $0.name.ilike("%john%") }
.where { $0.name.iContains("john") } // ILIKE '%john%'
.where { $0.name.iStartsWith("j") } // ILIKE 'j%'
.where { $0.name.iEndsWith("son") } // ILIKE '%son'
// Collection
.where { $0.status.in(["active", "pending"]) }
.where { $0.role.notIn(["banned", "suspended"]) }
.where { $0.age.between(18, and: 65) }
// Null checks
.where { $0.deletedAt.isNull() }
.where { $0.email.isNotNull() }
// Date
.where { $0.createdAt.isToday() }
.where { $0.createdAt.isThisWeek() }
.where { $0.createdAt.isThisMonth() }
.where { $0.createdAt.isThisYear() }
.where { $0.createdAt.before(cutoffDate) }
.where { $0.createdAt.after(startDate) }
// Logical operators
.where { $0.role == "admin" || $0.role == "moderator" }
.where { ($0.age >= 18) && ($0.isActive == true) }
.where { !($0.status == "banned") }// Single field (ascending by default)
.orderBy { $0.createdAt }
// Explicit direction
.orderBy({ $0.createdAt }, .desc)
// Multiple fields
.orderBy({ $0.name }, .asc, then: { $0.createdAt }, .desc).limit(20)
.offset(40)// Inner join
let results = try await repo.query(User.self)
.join(Post.self, on: { $0.left.id == $0.right.userId })
.where { $0.name == "John" }
.all()
// Left join
let results = try await repo.query(User.self)
.leftJoin(Post.self, on: { $0.left.id == $0.right.userId })
.all()
// Right join
let results = try await repo.query(User.self)
.rightJoin(Post.self, on: { $0.left.id == $0.right.userId })
.all()
// Through join (many-to-many via junction table)
let results = try await repo.query(User.self)
.joinThrough(Tag.self, through: UserTag.self, on: { builder in
(builder.main.id == builder.junction.userId,
builder.junction.tagId == builder.target.id)
})
.all()| Method | Returns | Description |
|---|---|---|
.all() |
[T] |
Execute query, return all matching rows |
.first() |
T? |
Execute query, return first row or nil |
.firstOrFail() |
T |
Execute query, throw SpectroError.notFound if empty |
.count() |
Int |
Return count of matching rows |
@Schema("users")
struct User {
@ID var id: UUID
@Column var name: String
@HasMany var posts: [Post]
@HasOne var profile: Profile?
@ManyToMany(junctionTable: "user_tags", parentFK: "userId", relatedFK: "tagId")
var tags: [Tag]
}
@Schema("posts")
struct Post {
@ID var id: UUID
@Column var title: String
@ForeignKey var userId: UUID
@BelongsTo var user: User?
}
@Schema("profiles")
struct Profile {
@ID var id: UUID
@Column var bio: String
@ForeignKey var userId: UUID
}
@Schema("tags")
struct Tag {
@ID var id: UUID
@Column var name: String
}
@Schema("user_tags")
struct UserTag {
@ID var id: UUID
@ForeignKey var userId: UUID
@ForeignKey var tagId: UUID
}Preloading executes one additional query per relationship (not one per row):
// Load users with their posts (2 queries total)
let users = try await repo.query(User.self)
.preload(\.$posts)
.all()
// Chain multiple preloads
let users = try await repo.query(User.self)
.preload(\.$posts)
.preload(\.$profile)
.preload(\.$tags) // many-to-many preload
.all()
// Override foreign key when it doesn't follow convention
let posts = try await repo.query(Post.self)
.preload(\.$author, foreignKey: "authorId")
.all()Preload queries support chaining with .where(), .orderBy(), and .limit():
let users = try await repo.query(User.self)
.where { $0.isActive == true }
.preload(\.$posts)
.orderBy({ $0.name }, .asc)
.limit(50)
.all()Transactions use READ COMMITTED isolation with automatic rollback on error. The closure receives a Repo-conforming object with full CRUD and query builder support:
let (user, profile) = try await spectro.transaction { repo in
let user = try await repo.insert(User(name: "Alice", email: "alice@example.com"