Skip to content

soenneker/soenneker.blazor.utils.indexeddb

Soenneker.Blazor.Utils.IndexedDb

A Blazor utility library for managing IndexedDB

Installation

dotnet add package Soenneker.Blazor.Utils.IndexedDb

Setup

Register services in Program.cs:

builder.Services.AddIndexedDbUtilAsScoped();

Inject the higher-level utility where you need it:

@inject IIndexedDbUtil IndexedDb

Usage

Initialize the package once before first use, ensure the store exists, then read/write values by databaseName, storeName, and key:

await IndexedDb.Initialize();

const string databaseName = "app";
const string storeName = "settings";
const string key = "theme";

await IndexedDb.EnsureStore(databaseName, storeName);
await IndexedDb.Set(databaseName, storeName, key, "dark");

string? theme = await IndexedDb.Get(databaseName, storeName, key);
bool hasTheme = await IndexedDb.ContainsKey(databaseName, storeName, key);
int count = await IndexedDb.GetLength(databaseName, storeName);

You can also store typed objects; values are JSON-serialized by IIndexedDbUtil:

public sealed record UserPreference(string Theme, bool SidebarCollapsed);

await IndexedDb.Set("app", "preferences", "user:1", new UserPreference("dark", true));

UserPreference? preference = await IndexedDb.Get<UserPreference>("app", "preferences", "user:1");

About

A Blazor utility library for managing IndexedDB

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Contributors