Schemas from one macro.
@Schema generates initializers, row mapping, and Encodable conformance at compile time from plain property-wrapper structs.
A PostgreSQL ORM that feels like Swift.
Inspired by Ecto. Property-wrapper schemas, queries you can reuse, and changesets that check input before it reaches your database.
Spectro 2.1 · Swift 6 · PostgreSQL
import Spectro
@Schema("users")
struct User {
@ID var id: UUID
@Column var name: String
@Column var email: String
}
let users = try await db.repository()
.query(User.self)
.where { $0.email.endsWith("@acme.io") }
.orderBy({ $0.name }, .asc)
.all()
Typed rows, in the order you asked for.
Part of Roost.
One Swift stack.
WHAT YOU GET
@Schema generates initializers, row mapping, and Encodable conformance at compile time from plain property-wrapper structs.
Every where, join, and orderBy returns a new query, so you can branch one and reuse it.
Cast only the fields you permit, validate them, and insert or update inside a transaction with clear errors.
Batch-load HasMany, HasOne, BelongsTo, and ManyToMany with a fixed number of queries per relationship, not one per row.
A CLOSER LOOK
Three short files. The whole idea.
Property wrappers name every column and relationship. The struct is the schema; there is no second file to keep in step.
Models.swift@Schema("users")
struct User {
@ID var id: UUID
@Column var name: String
@Column var email: String
@Timestamp var createdAt: Date
@HasMany var posts: [Post]
}
@Schema("posts")
struct Post {
@ID var id: UUID
@Column var title: String
@Column var views: Int
@ForeignKey var userId: UUID
@BelongsTo var user: User?
@Timestamp var createdAt: Date
}
Build a query once, then page it, sum it, or preload its relationships. Preloading the posts costs one extra query, not one per row.
Queries.swiftlet db = try Spectro(username: "postgres", password: "postgres", database: "app")
let repo = db.repository()
let popular = repo.query(Post.self)
.where { $0.views >= 1_000 && $0.title.iContains("swift") }
let page = try await popular
.orderBy({ $0.createdAt }, .desc)
.page(size: 20, page: 1)
let totalViews = try await popular.sum { $0.views }
let writers = try await repo.query(User.self)
.orderBy({ $0.name }, .asc)
.preload(\.$posts)
.limit(10)
.all()
The changeset keeps only permitted fields, so "admin" is dropped. When the blank rename fails, the transaction rolls back and Ada is never committed.
Signup.swiftlet signup = Changeset<User>.cast(nil,
params: ["name": "Ada", "email": "ada@acme.io", "admin": true],
permitted: ["name", "email"])
.validateRequired(["name", "email"])
.validateFormat("email", pattern: #"^.+@.+\..+$"#)
try await db.transaction { tx in
let ada = try await tx.insert(signup)
let rename = Changeset.cast(ada, params: ["name": ""], permitted: ["name"])
.validateRequired(["name"])
_ = try await tx.update(rename)
}
INSTALL
Add the package, depend on SpectroKit, then import Spectro in your code. The spectro command runs migrations; install it with Mint.
Swift 6.0+ · macOS 13+ or Linux · PostgreSQL
Read the guide.package(url: "https://github.com/roost-framework/Spectro.git", from: "2.1.0")
// in your target's dependencies
.product(name: "SpectroKit", package: "Spectro")
mint install roost-framework/Spectro@2.1.0
Keep your data
close to your code.