Prism supports three relationship types between database models: hasMany, belongsTo, and hasOne. Define them on your models, then load related data with simple queries.
Build an endpoint that returns users with their posts:
Users with Posts API
await server.get("/users/:id") { request in guard let id = Int(request.parameters["id"] ?? "") else { return .json(["error": "Invalid ID"], status: .badRequest) } guard let user = try await db.find(User.self, id: id) else { return .json(["error": "Not found"], status: .notFound) } let posts = try await db.loadHasMany("posts", foreignKey: "user_id", localValue: "\(user.id)") let profile = try await db.loadHasOne("profiles", foreignKey: "user_id", localValue: "\(user.id)") return .json([ "id": user.id, "name": user.name, "email": user.email, "bio": profile?["bio"]?.textValue ?? "", "posts": posts.map { row in [ "id": row["id"]?.intValue ?? 0, "title": row["title"]?.textValue ?? "" ] as [String: Any] } ] as [String: Any])}
For performance-sensitive endpoints, consider using a single JOIN query instead of multiple relationship loads. Relationships are convenient for simple cases; raw SQL with JOINs is better when you need to minimize round trips.