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.
Fastest persistent tier. Real files in the origin private file system. Concurrent connections, no COOP/COEP.
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.
// 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();
}Download a real .db
Exports app.db from whichever backend currently
owns it. On OPFS that is a file read; on IndexedDB and Cache Storage the image is
assembled from pages. Either way the result is an ordinary SQLite file.
var session = await Database.EnsureSessionAsync();
var provider = session.Resolution.Provider;
byte[] image = await provider.Admin.ExportAsync("app.db");
// A plain SQLite file: open it in any desktop tool.
await js.InvokeVoidAsync("bsqliteDemo.download", "app.db", image);Seed a tab from a file
Replaces the named database on the selected backend and records the sticky binding, so the next open finds the imported data instead of creating an empty file elsewhere. The worker is closed first: an open connection would keep writing the image being replaced.
// The worker is closed for the whole of the work, so it cannot keep writing the image
// being replaced - and nothing can reopen the database halfway through.
await Database.WhileClosedAsync(async ct =>
{
await provider.Admin.ImportAsync("app.db", bytes, ct);
// Record the sticky binding, or the next open would create an empty file on the
// preferred backend and the import would look like it vanished.
await Database.Bindings.SetProviderNameAsync("app.db", provider.Name, ct);
});
// The reopen raises Opened, which is how every page hears it should reload; a context
// runs the migrations before they come back for the database.
await using (await Database.OpenContextAsync())
{
}Start over
Removes app.db from the current backend and
forgets the binding, so the next open is free to pick the preferred provider again,
run the migrations, and reseed.
await Database.WhileClosedAsync(async ct =>
{
await provider.Admin.DeleteAsync("app.db", ct);
// Forget the binding so selection is free to pick the preferred backend again.
await Database.Bindings.RemoveAsync("app.db", ct);
});
// Migrates and reseeds the now-empty database.
await using (await Database.OpenContextAsync())
{
}