Blazor.IndexedDB 3.0.3

dotnet add package Blazor.IndexedDB --version 3.0.3                
NuGet\Install-Package Blazor.IndexedDB -Version 3.0.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Blazor.IndexedDB" Version="3.0.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Blazor.IndexedDB --version 3.0.3                
#r "nuget: Blazor.IndexedDB, 3.0.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Blazor.IndexedDB as a Cake Addin
#addin nuget:?package=Blazor.IndexedDB&version=3.0.3

// Install Blazor.IndexedDB as a Cake Tool
#tool nuget:?package=Blazor.IndexedDB&version=3.0.3                

NuGet installation

The NuGet package is at: https://www.nuget.org/packages/Blazor.IndexedDB

Either install it from command line:

PM> Install-Package Blazor.IndexedDB

Or include it in your project file:

<PackageReference Include="Blazor.IndexedDB" Version="3.0.3" />

Features

  • Connect and create database
  • Add record
  • Remove record
  • Edit record

How to use

  1. Add Blazor.IndexedDB/indexedDb.Blazor.js to your index.html
<script src="_content/Blazor.IndexedDB/indexedDb.Blazor.js"></script>
  1. Register IndexedDbFactory as a service.
services.AddSingleton<IIndexedDbFactory, IndexedDbFactory>();
  • IIndexedDbFactory is used to create the database connection and will create the database instance for you.

  • IndexedDbFactory requires an instance of IJSRuntime which should normally already be registered.

  1. Create any code first database model and inherit from IndexedDb. Only properties with the type IndexedSet<> will be used, any other properties will be ignored.
public class ExampleDb : IndexedDb
{
  public ExampleDb(IJSRuntime jSRuntime, string name, int version) : base(jSRuntime, name, version) { }
  public IndexedSet<Person> People { get; set; }
}
  • Your model (eg. Person) should contain an Id property or a property marked with the Key attribute.
public class Person
{
  [System.ComponentModel.DataAnnotations.Key]
  public long Id { get; set; }
  public string FirstName { get; set; }
  public string LastName { get; set; }
}
  1. Now you can start using your database.
  • Usage in Razor via inject: @inject IIndexedDbFactory DbFactory

Adding records

using (var db = await this.DbFactory.Create<ExampleDb>())
{
  db.People.Add(new Person()
  {
    FirstName = "First",
    LastName = "Last"
  });
  await db.SaveChanges();
}

Removing records

To remove an element it is faster to use an already created reference. You should also be able to remove an object only by it's Id but you have to use the .Remove(object) method (eg. .Remove(new object() { Id = 1 }))

using (var db = await this.DbFactory.Create<ExampleDb>())
{
  var firstPerson = db.People.First();
  db.People.Remove(firstPerson);
  await db.SaveChanges();
}

Modifying records

using (var db = await this.DbFactory.Create<ExampleDb>())
{
  var personWithId1 = db.People.Single(x => x.Id == 1);
  personWithId1.FirstName = "This is 100% a first name";
  await db.SaveChanges();
}

License

Original license.

Licensed under the MIT license.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Blazor.IndexedDB:

Package Downloads
chd.UI.Base.Client

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.0.3 1,427 12/6/2023
3.0.2 150 12/5/2023
3.0.1 156 12/4/2023
3.0.0 162 11/29/2023
2.0.4 150 12/6/2023
2.0.3 142 12/5/2023
2.0.2 146 12/4/2023
2.0.1 151 11/29/2023
1.2.1 152 11/29/2023 1.2.1 is deprecated because it is no longer maintained.

- 3.0.3:
- Added .ts files and webpack build for indexedDb.Blazor.js