Blazor.IndexedDB
3.0.3
dotnet add package Blazor.IndexedDB --version 3.0.3
NuGet\Install-Package Blazor.IndexedDB -Version 3.0.3
<PackageReference Include="Blazor.IndexedDB" Version="3.0.3" />
paket add Blazor.IndexedDB --version 3.0.3
#r "nuget: Blazor.IndexedDB, 3.0.3"
// 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
- Add
Blazor.IndexedDB/indexedDb.Blazor.js
to yourindex.html
<script src="_content/Blazor.IndexedDB/indexedDb.Blazor.js"></script>
- 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 ofIJSRuntime
which should normally already be registered.
- Create any code first database model and inherit from
IndexedDb
. Only properties with the typeIndexedSet<>
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 anId
property or a property marked with theKey
attribute.
public class Person
{
[System.ComponentModel.DataAnnotations.Key]
public long Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
- 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 | Versions 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. |
-
net8.0
- Microsoft.AspNetCore.Components.Web (>= 8.0.0)
- Microsoft.JSInterop (>= 8.0.0)
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.
- 3.0.3:
- Added .ts files and webpack build for indexedDb.Blazor.js