Structures
The following structures are available globally.
-
Configuration options for opening or creating a collection.
CollectionOptionslets you specify the document id field, an optional partition key for shard routing, and additional fields to index for query performance.See moreNote
TheidFieldandpartitionKeyare frozen at collection creation. Changing them after data exists would make existing records unreadable.indexedFieldscan be extended across opens.Declaration
Swift
public struct CollectionOptions : Sendable -
A typed, Sendable handle to one collection within a NyaruDB database.
NyaruCollectionis a thin facade that encodes and decodes the generic document typeTand delegates all storage operations to a sharedCollectionCoreactor. Handles are cheap value types — copy them freely.Writing documents. Use
insert,update,upsert,patch, anddelete:try await collection.insert(user) try await collection.patch(id: userId, changes: ["name": "New Name"])Reading documents. Use
get(id:)for point lookups,all()for everything,stream()for bounded-memory iteration, andfind()for fluent queries with predicates, sorting, and pagination.Maintenance. Call
See morecompact()opportunistically (e.g. on app backgrounding) to reclaim space from tombstoned records.Declaration
Swift
public struct NyaruCollection<T> : Sendable where T : Decodable, T : Encodable, T : Sendable
-
A pull-based
AsyncSequencethat yields documents one by one without materialising the entire collection in memory.The iterator fetches documents in bounded batches from the storage engine. Each batch is decoded lazily as the consumer calls
next(). When the batch is exhausted, the next batch is fetched from the collection actor.Memory usage is bounded by one batch (
batchSizedocuments).See moreNote
This replaces the earlierAsyncThrowingStream-based approach whose unbounded buffer allowed a fast producer to materialise the whole collection behind a slow consumer, defeating the purpose of streaming.Declaration
Swift
public struct NyaruDocumentStream<T> : AsyncSequence, Sendable where T : Decodable, T : Encodable, T : Sendable
-
A snapshot of collection statistics returned by
See moreNyaruCollection.stats().Declaration
Swift
public struct CollectionStats : Sendable -
Configuration shared by every collection opened from a single
NyaruDBinstance.DatabaseOptionscontrols compression, encryption, serialization format, file protection, and the fragmentation threshold that drives compaction hints. These options are applied to all collections within the database and are persisted in each collection’s manifest at creation time.Changing options after collections exist has no effect on existing collections — the manifest is frozen at creation. Only
See moreencryptionKeyis omitted from the manifest (it is provided at open time) so the key can be rotated or supplied from the Keychain without touching on-disk data.Declaration
Swift
public struct DatabaseOptions : Sendable -
The query execution plan produced by
QueryBuilder.explain().A query plan describes the chosen access path and how many predicates can be pushed down to the index versus evaluated in memory.
See moreDeclaration
Swift
public struct QueryPlan : Sendable -
A thread-safe wrapper around
See moreNSRegularExpressionthat makes regex instances compatible withSendablerequirements.Declaration
Swift
public struct SafeRegex : @unchecked Sendable
-
An immutable, fluent query builder for executing typed queries against a NyaruDB collection.
QueryBuilderis created byNyaruCollection.find()and provides a chainable API for adding predicates, sorting, pagination, and executing the query:let results = try await collection.find() .where("age", isGreaterThan: 18) .where("status", isEqualTo: "active") .sort(by: "name", ascending: true) .limit(20) .offset(0) .execute()Query planning. The builder includes a simple cost-based planner that selects the most efficient access path:
- Index equality lookup (highest priority).
- Index
INset lookup. - Index range/comparison lookup.
- Partition scan (if a partition key equality predicate exists).
- Full collection scan (fallback).
Call
See moreexplain()to inspect the plan without executing.Declaration
Swift
public struct QueryBuilder<T> : Sendable where T : Decodable, T : Encodable, T : Sendable -
A stable, hashable reference to a physical record stored in a shard file.
Every index entry in NyaruDB is a
RecordPointer. Storing bare offsets caused cross-shard lookup and delete corruption in the earlier engine because the same raw offset value is valid in every shard file — without the shard identifier, a pointer is ambiguous. By always coupling the shard ID with the offset, every index entry unambiguously identifies one record on disk, regardless of how many shards exist.Conforms to
See moreHashable(so it can be stored in posting-list arrays and sets),Codable(so index snapshots are serialisable), andSendable(for safe use across actor boundaries).Declaration
Swift
public struct RecordPointer : Hashable, Codable, Sendable
View on GitHub
Structures Reference