DocumentCollection
public class DocumentCollection
A class representing a collection of documents within the database.
The DocumentCollection class is responsible for managing the documents
in a specific collection, including their metadata, storage, and statistics.
- Properties:
metadata: Metadata associated with the collection, such as its name and configuration.storage: The storage engine responsible for persisting the documents in the collection.statsEngine: The statistics engine responsible for tracking and managing collection statistics.
-
Undocumented
Declaration
Swift
public let metadata: CollectionMetadata -
Initializes a new instance of
DocumentCollection.Declaration
Swift
public init(storage: StorageEngine, statsEngine: StatsEngine, name: String, indexes: [String] = [], partitionKey: String)Parameters
storageThe storage engine responsible for handling data persistence.
statsEngineThe statistics engine used for tracking collection metrics.
nameThe name of the collection.
indexesAn optional array of index names to be created for the collection. Defaults to an empty array.
partitionKeyThe key used to partition the data within the collection.
-
insert(_:Asynchronous) Inserts a document into the collection asynchronously.
Throws
An error if the insertion fails.Note
The document is stored in the collection specified by the metadata, and the first index field from the metadata’s indexes is used for indexing.Declaration
Swift
public func insert<T>(_ document: T) async throws where T : Decodable, T : EncodableParameters
documentThe document to be inserted. It must conform to the
Codableprotocol. -
bulkInsert(_:Asynchronous) Inserts multiple documents into the collection in bulk.
Throws
An error if the insertion fails.Note
This method uses the first index field from the collection’s metadata for indexing.Requires
Thedocumentsarray must not be empty.Declaration
Swift
public func bulkInsert<T>(_ documents: [T]) async throws where T : Decodable, T : EncodableParameters
documentsAn array of documents conforming to the
Codableprotocol to be inserted. -
findOne(query:AsynchronousshardKey: shardValue: ) Finds a single document in the collection that matches the specified query and shard key-value pair.
Throws
An error if the operation fails.Note
This method is asynchronous and must be called withawait.Declaration
Swift
public func findOne<T: Codable>( query: [String: Any]? = nil, shardKey: String, shardValue: String ) async throws -> T?Parameters
queryAn optional dictionary representing the query criteria. If
nil, no filtering is applied.shardKeyThe key used to identify the shard where the document is stored.
shardValueThe value of the shard key to locate the specific shard.
Return Value
An optional object of type
Tthat conforms toCodable, representing the found document, ornilif no document matches the criteria. -
update(_:Asynchronousmatching: ) Updates a document in the collection that matches the given predicate.
Throws
An error if the update operation fails.
Note
This method is asynchronous and must be called with
await.Declaration
Swift
public func update<T>(_ document: T, matching predicate: @escaping (T) -> Bool) async throws where T : Decodable, T : EncodableParameters
documentThe document of type
Tto update. Must conform toCodable.predicateA closure that takes a document of type
Tas its argument and returns a Boolean value indicating whether the document matches the condition. -
fetch(query:AsynchronousshardKey: shardValue: ) Fetches documents from the collection that match the specified query and optional shard parameters.
Throws
An error if the fetch operation fails.Note
The generic typeTmust conform to theCodableprotocol.Declaration
Swift
public func fetch<T: Codable>( query: [String: Any]? = nil, shardKey: String? = nil, shardValue: String? = nil ) async throws -> [T]Parameters
queryAn optional dictionary representing the query criteria. The keys are field names, and the values are the values to match. If
nil, all documents are fetched.shardKeyAn optional string representing the shard key to filter the documents. If
nil, no shard key filtering is applied.shardValueAn optional string representing the shard value to filter the documents. If
nil, no shard value filtering is applied.Return Value
An array of documents of type
Tthat match the query and shard parameters. -
delete(where:Asynchronous) Deletes documents from the collection that satisfy the given predicate.
This method asynchronously deletes documents of type
Tfrom the collection where the provided predicate evaluates totrue.Throws
An error if the deletion process fails.
Note
The
predicateclosure is executed asynchronously.Declaration
Swift
public func delete<T>(where predicate: @escaping (T) -> Bool) async throws where T : Decodable, T : EncodableParameters
predicateA closure that takes an instance of
Tas its argument and returns a Boolean value indicating whether the document should be deleted. -
query()AsynchronousAsynchronously creates a query for documents of the specified type.
Throws
An error if the query cannot be created.Note
The generic typeTmust conform to theCodableprotocol.Declaration
Swift
public func query<T>() async throws -> Query<T> where T : Decodable, T : EncodableReturn Value
A
Queryobject that can be used to perform operations on documents of typeT.
View on GitHub