![]() |
VOOZH | about |
dotnet add package TG.Blazor.IndexedDB --version 1.5.0-preview
NuGet\Install-Package TG.Blazor.IndexedDB -Version 1.5.0-preview
<PackageReference Include="TG.Blazor.IndexedDB" Version="1.5.0-preview" />
<PackageVersion Include="TG.Blazor.IndexedDB" Version="1.5.0-preview" />Directory.Packages.props
<PackageReference Include="TG.Blazor.IndexedDB" />Project file
paket add TG.Blazor.IndexedDB --version 1.5.0-preview
#r "nuget: TG.Blazor.IndexedDB, 1.5.0-preview"
#:package TG.Blazor.IndexedDB@1.5.0-preview
#addin nuget:?package=TG.Blazor.IndexedDB&version=1.5.0-preview&prereleaseInstall as a Cake Addin
#tool nuget:?package=TG.Blazor.IndexedDB&version=1.5.0-preview&prereleaseInstall as a Cake Tool
This is a Blazor library for accessing IndexedDB and uses Jake Archibald's idb library for handling access to IndexedDB on the JavaScript side.
This version currently provides the following functionality:
It does not, at the moment, support aggregate keys, searches using a range and some of the more obscure features of IndexedDB.
Install-Package TG.Blazor.IndexedDB -Version 1.5.0-preview)<script src="_content/TG.Blazor.IndexedDB/indexedDb.Blazor.js"></script>The library provides a service extension to create a singleton instance of the DbStore.
Within the client application's startup.cs file, add the following to the ConfigureServices function.
services.AddIndexedDB(dbStore =>
{
dbStore.DbName = "TheFactory"; //example name
dbStore.Version = 1;
dbStore.Stores.Add(new StoreSchema
{
Name = "Employees",
PrimaryKey = new IndexSpec { Name = "id", KeyPath = "id", Auto = true },
Indexes = new List<IndexSpec>
{
new IndexSpec{Name="firstName", KeyPath = "firstName", Auto=false},
new IndexSpec{Name="lastName", KeyPath = "lastName", Auto=false}
}
});
dbStore.Stores.Add(new StoreSchema
{
Name = "Outbox",
PrimaryKey = new IndexSpec { Auto = true }
}
);
});
To define the database we need to first give it a name and set its version. IndexedDB uses the version to determine whether it needs to update the database. For example if you decide to add a new store then increment the version to ensure that the store is added to the database.
In IndexedDB a store is equivalent to table. To create a store we create a new StoreSchema and add it to the collection of stores.
Within the StoreSchema we define the name of the store, the primary index key and optionally a set of foreign key indexes if required.
The IndexSpec is used to define the primary key and any foreign keys that are required. It has the following properties:
In the example above for the "Employees" store the primary key is explicitly set to the keypath "id" and we want it automatically generated by IndexedDB. In the "Outbox" store the primary key just has Auto = true set. IndexedDB is left to handle the rest.
For the following examples we are going to assume that we have Person class which is defined as follows:
public class Person
{
public long? Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
And the data store name is "Employees"
To use IndexedDB in a component or page first inject the IndexedDbManager instance.
@inject IndexedDBManager DbManager
IndexedDBManager exposes ActionCompleted event that is raised when an action is completed.
If you want to receive notifications in the ```OnInit()`` function subscribe to the event.
The function that handles the event should have the following signature:
private void OnIndexedDbNotification(object sender, IndexedDBNotificationArgs args)
{
Message = args.Message;
}
It is recommended that your page or component should also implement IDisposable to unsubscribe from the event.
Assuming we have a new instance of our sample Person class, to add to the "Employees" store doing the following:
var newRecord = new StoreRecord<Person>
{
Storename ="Employees",
Data = NewPerson
};
await DbManager.AddRecord(newRecord);
var results = await DbManager.GetRecords<Person>("Employees");
To get a record using the id can be done as follows:
CurrentPerson = await DbManager.GetRecordById<long, Person>(DbManager.Stores[0].Name, id);
To search for a value on an index first create an instance of StoreIndexQuery<TInput> providing the name of the store to search in, the index to search on and the value to search for.
var indexSearch = new StoreIndexQuery<string>
{
Storename = DbManager.Stores[0].Name,
IndexName = SelectedIndex,
QueryValue = SearchString,
};
The StoreIndexQuery is then passed to the function IndexedDbManager.GetRecordByIndex<TInput, TResult>() where TInput is the type of the value to search for and TRresult is the type of the return value.
var result = await DbManager.GetRecordByIndex<string, Person>(indexSearch);
if (result is null)
{
return;
}
People.Add(result);
By default IndexedDB only returns the first record found that matches the query. If you want to get all of the records that match the query value use the following:
var result = await DbManager.GetAllRecordsByIndex<string, Person>(indexSearch);
if (result is null)
{
return;
}
People.AddRange(result);
To update a record call IndexedDbManager.UpdateRecord<T>(StoreRecord<T> recordToUpdate).
To delete a record call IndexedDbManager.DeleteRecord<TInput>(string storeName, TInput id).
To clear all the records in a store call the following function IndexedDbManager.ClearStore(string storeName).
If you are so inclined you can delete an entire database with the following function IndexedDbManager.DeleteDb(string dbName).
If you have occasion to what to add a store when the program is up and running. The following
var newStoreSchema = new StoreSchema
{
Name = NewStoreName,
PrimaryKey = new IndexSpec { Name = "id", KeyPath = "id", Auto = true },
};
await DbManager.AddNewStore(newStoreSchema);
What this will do is, if the store doesn't already exist, is increment the database version number and add the store to the database.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 5 NuGet packages that depend on TG.Blazor.IndexedDB:
| Package | Downloads |
|---|---|
|
Reshiru.Blazor.IndexedDB.Framework
A Blazor framework for interacting with IndexedDB |
|
|
IndexedDB.Blazor
A Blazor library for accessing IndexedDB |
|
|
Johnjalani.Blazor.IndexedDB.WebAssembly
A Blazor web assembly for interacting with IndexedDB |
|
|
IndexedDB.Blazor.Net8
A Blazor library for accessing IndexedDB |
|
|
IndexedDB.Blazor.sisn
A Blazor library for accessing IndexedDB |
Showing the top 4 popular GitHub repositories that depend on TG.Blazor.IndexedDB:
| Repository | Stars |
|---|---|
|
lofcz/LLMTornado
The .NET library to build AI agents with 30+ built-in connectors.
|
|
|
Tewr/BlazorWorker
Library for creating DotNet Web Worker threads/multithreading in Client side Blazor
|
|
|
KristofferStrube/Blazor.FileSystemAccess
A Blazor wrapper for the File System Access browser API.
|
|
|
arleypadua/PKHeX.Everywhere
Cross platform tools for interacting with Pokemon save files. The web version runs everywhere and the CLI works with Mac OS, Linux and Windows
|
| Version | Downloads | Last Updated |
|---|---|---|
| 1.5.0-preview | 341,870 | 1/8/2020 |
| 1.2.1-preview | 1,410 | 10/7/2019 |
| 1.2.0-preview | 532 | 9/17/2019 |
| 1.1.0-preview | 629 | 8/21/2019 |
| 1.0.0-preview | 516 | 8/15/2019 |
| 0.9.0-beta | 35,911 | 6/29/2019 |
| 0.6.0-beta | 744 | 11/17/2018 |
Updated to work with both server and client-side Blazor