NyaruDB
public actor NyaruDB
The root entry point for the NyaruDB embedded document database.
NyaruDB is an actor that serialises all file I/O through Swift’s
concurrency model. It manages the lifecycle of collections, handles
manifest I/O, and coordinates durability across all open collections.
Opening a database. Provide a directory URL or filesystem path:
let db = try await NyaruDB(path: "path/to/database")
Opening collections. Use collection(_:of:options:) to open or create
a typed collection:
let users = try await db.collection("users", of: User.self)
Lifecycle. Call sync() to flush pending index and shard I/O, and
close() to shut down cleanly. After close(), the instance cannot be
used again.
Note
A singleNyaruDB instance corresponds to one directory on disk.
Opening multiple instances pointing at the same directory is not supported.
-
Opens or creates a database at the given directory URL.
If the directory does not exist, it is created (with intermediate directories). All collection data is stored as subdirectories within this directory.
Throws
NyaruError.ioErrorif the directory cannot be created.Declaration
Swift
public init(path: URL, options: DatabaseOptions = DatabaseOptions()) throwsParameters
pathThe directory URL for the database.
optionsDatabase-wide configuration applied to all collections.
-
Opens or creates a database at the given filesystem path string.
Convenience initialiser that converts the string to a directory URL and delegates to
init(path:options:).Throws
NyaruError.ioErrorif the directory cannot be created.Declaration
Swift
public convenience init(path: String, options: DatabaseOptions = DatabaseOptions()) throwsParameters
pathThe filesystem path to the database directory.
optionsDatabase-wide configuration.
-
collection(_:Asynchronousof: options: ) Opens an existing collection or creates a new one with the given name and document type.
When opening an existing collection, the provided options are validated against the persisted manifest. If the base configuration (id field, partition key, compression, encryption, format) does not match,
collectionTypeMismatchis thrown.Indexed fields can be added across opens — new fields are populated by scanning all shards on first open. The id field is always indexed automatically.
Throws
NyaruError.collectionTypeMismatchif options conflict with the persisted manifest,NyaruError.databaseClosedif closed.Declaration
Swift
public func collection<T: Codable & Sendable>( _ name: String, of type: T.Type, options collectionOptions: CollectionOptions = CollectionOptions() ) async throws -> NyaruCollection<T>Parameters
nameThe collection name.
typeThe document type, which must conform to
Codable & Sendable.collectionOptionsPer-collection options (id field, partition key, indexed fields).
Return Value
A typed
NyaruCollection<T>handle. -
Returns the names of all collections present on disk.
Scans the database directory for subdirectories containing a
manifest.jsonfile and reads each manifest to extract the collection name. Handles encrypted manifests transparently.Throws
NyaruError.databaseClosedif closed, orNyaruError.decryptionFailedif a manifest cannot be decrypted.Declaration
Swift
public func listCollections() throws -> [String]Return Value
A sorted list of collection names.
-
drop(_:Asynchronous) Permanently deletes a collection and all of its files from disk.
If the collection is currently open, it is destroyed through the core (which shuts down shards cleanly). Otherwise the directory is removed directly.
Throws
NyaruError.collectionNotFoundif the collection directory does not exist,NyaruError.databaseClosedif closed.Declaration
Swift
public func drop(_ name: String) async throwsParameters
nameThe name of the collection to drop.
-
sync()AsynchronousFlushes all index snapshots and shard headers to disk.
After
sync()returns successfully, a crash on any clean shard requires no recovery work — the index snapshots are current and the dirty flag is cleared. This is a no-op if no mutations have occurred since the last sync.Throws
NyaruError.databaseClosedif closed, or the first I/O error encountered across all collections.Declaration
Swift
public func sync() async throws -
close()AsynchronousSyncs and then closes every open collection.
After
close()returns, the instance is permanently closed. Calling any method other thanclose()(which is idempotent) will throwNyaruError.databaseClosed.Throws
The first I/O error encountered from any collection’s sync or close operations.Declaration
Swift
public func close() async throws
View on GitHub