# BsqliteDatabase (Class) `Bit.Bsqlite.BsqliteDatabase` - package `Bit.Bsqlite` - `using Bit.Bsqlite;` - implements `IAsyncDisposable`, `IDisposable` One named database for the lifetime of the application: opened on first use, shared by every caller, and closable so that admin operations can replace the file underneath. A Blazor WebAssembly application is one process with one user, so the natural shape is a singleton session per database rather than a pool. This type is that singleton. It is what the EF Core integration's context factory opens through, and what an application should inject when it wants the connection directly. BsqliteDatabase.CloseAsync tears the worker down. Subscribers to BsqliteDatabase.Closing drop the connections and live queries they hold; subscribers to BsqliteDatabase.Opened reopen. That is the protocol for moving a database between backends or importing a file over it. ## Constructor - **BsqliteDatabase**(IBsqliteSessionFactory sessions, string databaseName) ## Property - **Connection** : `BsqliteConnection` - The open connection, or null when the database is not open. - **IsOpen** : `bool` - Whether a session is currently open. - **Name** : `string` - The database this instance opens. - **Provider** : `IBsqliteStorageProvider` - The provider holding the data, when the database is open. - **Resolution** : `BsqliteStorageResolution` - Why the current backend was chosen, when the database is open. - **Session** : `BsqliteSession` - The open session, or null before the first open or after a close. ## Method - **CloseAsync**(CancellationToken cancellationToken = default) : `Task` - Closes the session and terminates its worker, after giving BsqliteDatabase.Closing subscribers the chance to let go. A no-op when the database is not open. - **DeleteAsync**(CancellationToken cancellationToken = default) : `Task` - Closes the database and deletes it - from every backend that holds a copy - together with the record of where it lived. The next BsqliteDatabase.OpenAsync starts an empty database, chosen afresh. What signing a user out of an offline app calls for. Every backend is cleared, not only the one in use: a database whose binding is gone is looked for on every backend before it is created anew, so a copy left elsewhere - by an interrupted migration, say - would otherwise come back on the next open. A backend that cannot be reached is logged and skipped - unless the database lives there - and the record is then kept, marked deleted, so a copy it may still hold is never looked for. BsqliteDatabase.Closing handlers let go first; the database is not reopened. Close other tabs that have it open first: a backend that cannot delete a file another tab holds - OPFS, a Blazor Hybrid file - throws, and IndexedDB and Cache Storage would leave that tab working on a database that no longer exists. - **Dispose**() : `void` - Synchronous disposal, for containers that dispose synchronously. The worker is torn down asynchronously in the background; prefer BsqliteDatabase.DisposeAsync where possible. - **DisposeAsync**() : `ValueTask` - **ExportAsync**(CancellationToken cancellationToken = default) : `Task` - Reads the open database out as a SQLite file image, consistent as of the last committed write, without closing it. - **OpenAsync**(CancellationToken cancellationToken = default) : `ValueTask` - Opens the database if it is not open and returns the session. - **ReopenAsync**(CancellationToken cancellationToken = default) : `Task` - Closes and reopens, which is how a changed binding or an imported file takes effect. - **RestoreAsync**(ReadOnlyMemory image, CancellationToken cancellationToken = default) : `Task` - Replaces the database with image - a backup taken with BsqliteDatabase.ExportAsync, or any SQLite database file - and reopens it. The image is checked before anything is touched: a file that is not a SQLite database, or is cut short, is refused with BsqliteCorruptDatabaseException and the database is left as it was. A file last written in write-ahead-log mode - every database EF Core creates on a server or a desktop - is marked for a rollback journal on the way in, which is the only mode a web VFS can open. The database is closed around the import, as BsqliteDatabase.WhileClosedAsync closes it: BsqliteDatabase.Closing handlers let go first, and BsqliteDatabase.Opened handlers see the restored database. It is imported onto the backend that holds the database now, and read back to check that every byte landed. When the import or that check fails, the database as it was is put back where it can be read out first, and a database that was open is reopened either way. An EF Core context factory applies the migrations the restored database is missing the next time it creates a context, so a backup from an earlier release of the app opens on the current model. Other tabs that have the database open are not closed: close them first, or restore before they open it. On OPFS, IndexedDB and a Blazor Hybrid file the import replaces the database in one step; on Cache Storage an import cut short - the tab closed part way - leaves neither the old database nor the new one. - **WhileClosedAsync**(Func work, CancellationToken cancellationToken = default) : `Task` - Runs work with the database closed and reopens afterwards, so an admin operation never races the worker that holds the file open. The database stays closed for the whole of work: an BsqliteDatabase.OpenAsync from anywhere else - a page loading, an BsqliteDatabase.Opened handler still running from the last open - waits until the work is done and the database has reopened, rather than opening it on the binding the work is about to change. The work itself must not open this database; it gets an InvalidOperationException if it tries. ## Event - **Closing** : `Func` - Raised before the session is closed. Handlers must release every connection, context, and live query bound to it; the worker is terminated once they return. - **Opened** : `Func` - Raised after a session opens, including the first time.