Part of the Roost family

Your data,
in plain Swift.

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

Users.swift
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()
ORDER BY name ASC3 rows
Ada, Grace, Linus.

Typed rows, in the order you asked for.

Part of Roost.
One Swift stack.

Roost The web framework 2.1 Nexus HTTP & routing 2.0 ESW HTML, compiled to Swift 1.6

WHAT YOU GET

Typed from table
to result.

Schemas from one macro.

@Schema generates initializers, row mapping, and Encodable conformance at compile time from plain property-wrapper structs.

Queries are values.

Every where, join, and orderBy returns a new query, so you can branch one and reuse it.

Changesets check the door.

Cast only the fields you permit, validate them, and insert or update inside a transaction with clear errors.

Preloading without N+1.

Batch-load HasMany, HasOne, BelongsTo, and ManyToMany with a fixed number of queries per relationship, not one per row.

A CLOSER LOOK

Model, query,
commit.

Three short files. The whole idea.

Columns you can see.

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
}

Queries are values.

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.swift
let 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()

All or nothing.

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.swift
let 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 it to
your package.

Two lines in Package.swift.

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.swift
.package(url: "https://github.com/roost-framework/Spectro.git", from: "2.1.0")

// in your target's dependencies
.product(name: "SpectroKit", package: "Spectro")
Terminal
mint install roost-framework/Spectro@2.1.0

Keep your data
close to your code.

Explore Spectro on GitHub