Swift: Documents
Read, write and query document collections, count and aggregate, and commit several writes at once with the Swift SDK.
Read, write and query document collections, count and aggregate, and commit several writes at once. Every call runs as the client's signed-in user, so your project's policies and row-level security apply. Types and members are listed in source order; each one links to its HTTP endpoint where it maps to one.
#Types and members
SovariaCollections
Document collections (Firestore-style): sovaria.collections.collection("chats/42/messages").add([...]).
public final class SovariaCollections: @unchecked SendableSovariaCollections.collection
public func collection(_ path: String) -> CollectionRefSovariaCollections.collectionGroup
Every collection with this id at any depth (comments, posts/p1/comments, …), like Firestore's collectionGroup: query, count, aggregate and findNearest read them all.
public func collectionGroup(_ collectionId: String) -> CollectionRefDocument cursors and __key__ are full document paths in a group.
SovariaCollections.commit
Write several documents atomically (["op": "set"|"update"|"delete", "path": "c/id", "data": [...]]).
public func commit(_ writes: [[String: Any]], reads: [[String: Any]] = []) async throws -> [String: Any]CollectionRef
public struct CollectionRef: @unchecked SendableCollectionRef.path
A property.
public let path: StringCollectionRef.doc
public func doc(_ id: String) -> DocumentRefCollectionRef.add
Add a document with a new random id; returns its reference.
public func add(_ data: [String: Any]) async throws -> DocumentRefCollectionRef.query
Field query: query(where: [["age", ">=", 18]], orderBy: [["age", "desc"]], limit: 20).
public func query(where filters: [Any] = [], orderBy: [[Any]] = [], limit: Int? = nil,
startAfter: [String: Any]? = nil) async throws -> QueryPageA where entry can be a group: QueryFilter.or([["a", "==", 1], ["b", "<", 2]]).
CollectionRef.findNearest
Vector search (Firestore findNearest): the limit documents whose vectorField (an array of numbers) is closest to queryVector, after the where pre-filter, closest first; each document has a distance.
public func findNearest(_ vectorField: String, queryVector: [Double], limit: Int, distanceMeasure: String = "",
where filters: [Any] = [], distanceThreshold: Double? = nil,
distanceResultField: String? = nil) async throws -> QueryPagedistanceMeasure: "cosine" | "euclidean" | "dot_product". distanceThreshold: at most this distance (cosine/euclidean) or at least this product (dot_product).
CollectionRef.count
public func count(where filters: [Any] = []) async throws -> IntCollectionRef.aggregate
count / sum / average on the server: aggregate(["n": Aggregate.count(), "revenue": Aggregate.sum("total")], where: [["paid", "==", true]]).
public func aggregate(_ aggregations: [String: [String: Any]], where filters: [Any] = [], orderBy: [[Any]] = [],
limit: Int? = nil) async throws -> [String: Double?]sum and average use numeric values only; average is nil when there are none. With limit they cover the first limit results (in orderBy order); without, every match.
DocumentRef
public struct DocumentRef: @unchecked SendableDocumentRef.collection
A property.
public let collection: StringDocumentRef.id
A property.
public let id: StringDocumentRef.get
The document's data, or nil when it doesn't exist.
public func get() async throws -> [String: Any]?DocumentRef.set
public func set(_ data: [String: Any]) async throwsDocumentRef.update
Merge fields into the document (others are kept).
public func update(_ fields: [String: Any]) async throwsDocumentRef.delete
public func delete() async throwsQueryPage
public struct QueryPage: @unchecked SendableQueryPage.documents
A property.
public let documents: [[String: Any]]QueryPage.hasMore
A property.
public let hasMore: BoolQueryPage.nextCursor
A property.
public let nextCursor: [String: Any]?QueryFilter
or / and groups for where (Firestore Filter.orFilter / Filter.andFilter; at most 30 OR terms after expansion).
public enum QueryFilterQueryFilter.or
public static func or(_ filters: [Any]) -> [String: Any]QueryFilter.and
public static func and(_ filters: [Any]) -> [String: Any]Aggregate
Aggregations for CollectionRef.aggregate.
public enum AggregateAggregate.count
public static func count() -> [String: Any]Aggregate.sum
public static func sum(_ field: String) -> [String: Any]Aggregate.average
public static func average(_ field: String) -> [String: Any]