Skip to content
BunBase BunBase BunBase Docs Alpha v0.1.0

Collections

struct Post: Codable, Sendable {
var title: String
var body: String
}
let posts = client.collection("posts", type: Post.self)
let result = try await posts.list(query: ListQuery(
sort: "-_created_at",
limit: 20,
filter: ["status": .string("published")]
))
for post in result.items { print(post.title) }
print(result.nextCursor as Any) // nil = last page
var cursor: String? = nil
repeat {
let page = try await posts.list(query: ListQuery(limit: 50, after: cursor))
// process page.items
cursor = page.nextCursor
} while cursor != nil
let post = try await posts.get("post_id")
// Expand related records
let post = try await posts.get("post_id", expand: ["author"])
let newPost = Post(title: "Hello", body: "World")
let created = try await posts.create(newPost)
struct Patch: Encodable, Sendable { let title: String }
let updated = try await posts.update("post_id", patch: Patch(title: "Updated"))
try await posts.delete("post_id")
let restored = try await posts.restore("post_id")
let total = try await posts.count()
// With filter
let published = try await posts.count(filter: ["status": .string("published")])
// Include soft-deleted
let all = try await posts.count(includeDeleted: true)
let items = [Post(title: "A", body: "..."), Post(title: "B", body: "...")]
let created = try await posts.createMany(items)
// Equality
FilterValue.string("published")
FilterValue.int(42)
FilterValue.bool(true)
// Array (IN)
FilterValue.strings(["draft", "published"])
// Comparison operators
FilterValue.op(["gte": .int(18)])
FilterValue.op(["lt": .double(100.0)])
FilterValue.op(["neq": .string("deleted")])
posts.channel // "collection:posts"
posts.mineChannel // "collection:posts:mine" (current user's records)
posts.recordChannel(id: "id") // "record:posts:id"
posts.recordsChannel // "records:posts"