Skip to content

Platform · Files

The database as a file

IBsqliteStorageAdmin treats a database as an artifact rather than a connection: what exists, read the bytes out, write bytes back, delete. Cross-provider migration and the diagnostics on Storage are built from this same surface, and none of it goes through EF.

Client database OPFS

Fastest persistent tier. Real files in the origin private file system. Concurrent connections, no COOP/COEP.

List

What each backend holds

Names come from the provider, so a backend that has never been used reports empty rather than failing, and one this browser cannot use says why. Related -journal files are not databases and do not appear.

Live
csharp Listing databases
// Databases as files, outside any SQL connection. No EF involved.
foreach (var provider in Database.Providers)
{
    // A backend this browser cannot use has nothing to list; probe.UnavailableReason says why.
    var probe = await provider.ProbeAsync();
    if (!probe.IsAvailable)
    {
        continue;
    }

    IReadOnlyList<string> names = await provider.Admin.ListAsync();
}
Export

Download a real .db

Snapshots app.db while it stays open, as of its last committed write - a transaction another context has open is waited for, not captured half done. On OPFS the image is the file byte for byte; on IndexedDB and Cache Storage it is a compacted copy made with VACUUM INTO. Either way it is an ordinary SQLite file, and a backup RestoreAsync takes back.

Try Download it and open it in any desktop SQLite tool - the rows you edited are there.
Live
csharp Exporting
@inject BsqliteDatabase Database

// A snapshot of the open database, as of its last committed write.
byte[] image = await Database.ExportAsync();

// A plain SQLite file: open it in any desktop tool, or restore it later.
await js.InvokeVoidAsync("bsqliteDemo.download", "app.db", image);
Restore

Restore from a file

Replaces app.db with the file, on the backend that holds it now. The file is checked before anything is touched - one that is not a SQLite database, or is cut short, is refused and the database stays as it was - and a file a server or desktop left in WAL mode is accepted. The database closes around the import, is read back to check every byte landed, and reopens; the context factory then applies the migrations an older backup is missing.

Try Export above, change some rows, then restore the file: the rows come back as they were. This is also how you ship a pre-built database with an app.
Live
csharp Restoring
// Checked first - not a SQLite database, or cut short, and nothing changes. Then the
// database closes (Closing handlers let go), the file replaces it where it lives, is
// read back to verify, and the database reopens (Opened handlers reload).
await Database.RestoreAsync(bytes);

// A context applies the migrations an older backup is missing.
await using var db = await Contexts.CreateDbContextAsync();
Delete

Start over

Removes app.db from every backend that holds a copy and forgets the binding, so the next open is free to pick the preferred provider again, run the migrations, and reseed. Every backend, not only the current one: a copy left elsewhere would be found again on the next open - what signing out of an offline app must not allow.

Live
csharp Deleting
// Closes the database and deletes it from every backend that holds a copy, then forgets
// the binding - so selection is free to pick the preferred backend again, and no copy
// left elsewhere comes back on the next open.
await Database.DeleteAsync();

// Migrates and reseeds the now-empty database.
await using var db = await Contexts.CreateDbContextAsync();
An unhandled error has occurred. Reload 🗙