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
storage
The storage engine responsible for handling data persistence.
statsEngine
The statistics engine used for tracking collection metrics.
name
The name of the collection.
indexes
An optional array of index names to be created for the collection. Defaults to an empty array.
partitionKey
The 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 : Encodable
Parameters
document
The document to be inserted. It must conform to the
Codable
protocol. -
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
Thedocuments
array must not be empty.Declaration
Swift
public func bulkInsert<T>(_ documents: [T]) async throws where T : Decodable, T : Encodable
Parameters
documents
An array of documents conforming to the
Codable
protocol 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
query
An optional dictionary representing the query criteria. If
nil
, no filtering is applied.shardKey
The key used to identify the shard where the document is stored.
shardValue
The value of the shard key to locate the specific shard.
Return Value
An optional object of type
T
that conforms toCodable
, representing the found document, ornil
if 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 : Encodable
Parameters
document
The document of type
T
to update. Must conform toCodable
.predicate
A closure that takes a document of type
T
as 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 typeT
must conform to theCodable
protocol.Declaration
Swift
public func fetch<T: Codable>( query: [String: Any]? = nil, shardKey: String? = nil, shardValue: String? = nil ) async throws -> [T]
Parameters
query
An 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.shardKey
An optional string representing the shard key to filter the documents. If
nil
, no shard key filtering is applied.shardValue
An 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
T
that 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
T
from the collection where the provided predicate evaluates totrue
.Throws
An error if the deletion process fails.
Note
The
predicate
closure is executed asynchronously.Declaration
Swift
public func delete<T>(where predicate: @escaping (T) -> Bool) async throws where T : Decodable, T : Encodable
Parameters
predicate
A closure that takes an instance of
T
as 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 typeT
must conform to theCodable
protocol.Declaration
Swift
public func query<T>() async throws -> Query<T> where T : Decodable, T : Encodable
Return Value
A
Query
object that can be used to perform operations on documents of typeT
.