I’m new to Swift development and excited to share my first project with the Swift on Server community. I’ve always wanted to learn Swift, and working on a server-side project felt like the perfect way to get started.
I’d like to introduce RedisOM Swift. I’ve used the Redis OM libraries extensively in other projects written in Python, and thought it would be a unique and fun challenge to work on a port for the Swift community.
What is RedisOM Swift
RedisOM Swift is a high-level Redis object mapper inspired by redis-om-python.
It provides a declarative way to model, persist, and query JSON documents in Redis using Swift key paths, macros, and async/await.
It integrates seamlessly with both Vapor and Swift ServiceLifecycle, making it suitable for API servers, workers, or microservices that need structured access to RedisJSON and RediSearch.
Key Features
- Declarative Models with
@Modelmacro that automatically generates RediJSON schema and RediSearch indexes. - Rich, Fluent Queries with a type-safe builder supporting
.where(),.and(),.or().not(),.limit(), etc. - Lifecycle Integration that works with both Vapor’s Lifecycle and Swift ServiceLifecycle for automatic startup and shutdown.
- Embedded / Nested Model support to query into nested documents using key paths.
- Flexible Configuration
- Connection Pooling and Retry Policies
Quick Example
Define your Models
@Model
struct User: JsonModel {
@Id var id: String?
@Index(type: .text) var name: String
@Index var email: String
@Index var aliases: [String]?
@Index(type: .numeric) var age: Int?
@Index var address: [Address]?
@Index(type: .numeric) var createdAt: Date?
static let keyPrefix: String = "user"
}
@Model
struct Address: JsonModel {
@Id var id: String?
@Index(type: .text)
var addressLine1: String
@Index(type: .text)
var addressLine2: String? = nil
@Index var city: String
@Index var state: String
@Index var country: String
@Index var postalCode: String
static let keyPrefix: String = "address"
}
Save and Fetch
var user: User = User(
name: "Alice",
email: "alice@example.com",
aliases: ["Alicia", "alice"],
age: 45,
address: [
Address(
addressLine1: "123 South Main St", city: "Pittsburg", state: "PA",
country: "US", postalCode: "15120"
)
],
createdAt: Date()
)
try await user.save()
let fetched = try await User.get(id: user.id!)
Query with RediSearch
let users: [User] = try await User.find().where(\.$name == "Alice").and(
\.$address[\.$city] == "Pittsburg"
).all()
Integrate with Vapor
public func configure(_ app: Application) throws {
let redis = try RedisOM(url: "redis://localhost:6379")
redis.register(User.self)
app.lifecycle.use(redis)
}
Check it out
The GitHub repository can be found here: GitHub - eric-musliner/redis-om-swift: Object mapping, and more, for Redis and Swift
I’d love feedback, ideas, or contributions from the community as I continue to grow this library. Thanks!