DrUalcman-BlazorIndexedDb
1.6.42
See the version list below for details.
dotnet add package DrUalcman-BlazorIndexedDb --version 1.6.42
NuGet\Install-Package DrUalcman-BlazorIndexedDb -Version 1.6.42
<PackageReference Include="DrUalcman-BlazorIndexedDb" Version="1.6.42" />
paket add DrUalcman-BlazorIndexedDb --version 1.6.42
#r "nuget: DrUalcman-BlazorIndexedDb, 1.6.42"
// Install DrUalcman-BlazorIndexedDb as a Cake Addin #addin nuget:?package=DrUalcman-BlazorIndexedDb&version=1.6.42 // Install DrUalcman-BlazorIndexedDb as a Cake Tool #tool nuget:?package=DrUalcman-BlazorIndexedDb&version=1.6.42
BlazorIndexedDb
Manage indexedDb from c# with Blazor. Simple way to interact with IndexedDB similar how you can do with EntityFramework
NuGet installation
PM> Install-Package DrUalcman-BlazorIndexedDb
New version changes coming
Working on change version control to avoid need to delete the previous database if add, delete or change StoreSet
Current features
Create StoreContext<TStore> from abstract class. Allow multiple StoreContext but dabase name should be different names. StoreSet per each model you need into a database. Set PrimaKey in the model. Using convention if have property Id or TableNameId or IdTableName then this is used like PrimaryKey AutoIncremental (only if it's a number is autoincremental) CRUD from StoreSet Select all or one by PrimaryKey or property from StoreSet Clean all data in a storeSet Drop Database
How to use
BlazorIndexedDb requires an instance IJSRuntime, should normally already be registered.
Create any code first database model you'd like to create and inherit from IndexedDb. You must be use the attribute FieldAttribute to setup the properties.
Your model (eg. PlayList) should contain an Id property or a property marked with the key attribute.
public class PlayList
{
[FieldAttribute(IsKeyPath = true, IsAutoIncemental = false, IsUnique = true)] //not required from version 1.5.18
public string Id { get; set; }
public string Url { get; set; }
public string Title { get; set; }
public string Ownner { get; set; }
}
namespace
BlazorIndexedDb
BlazorIndexedDb.Attributes
BlazorIndexedDb.Commands
BlazorIndexedDb.Models
BlazorIndexedDb.Store
Then can create a DBContext class inherits from StoreContext<TSore> to manage the database like in EF using a constructor with IJSRuntime and properties with the server about how to manage your tables.
public class DBContext : StoreContext<DBContext>
{
#region properties
public StoreSet<PlayList> PlaysList { get; set; }
#endregion
#region constructor
public DBContext(IJSRuntime js) : base(js, new Settings { DBName = "MyDBName", Version = 1 }) { }
#endregion
}
In Program.cs add the service for the DBContext
using BlazorIndexedDb;
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
//INJECT THE DBContext or use your implementation
builder.AddBlazorIndexedDbContext<DBContext>();
var app = builder.Build();
await app.RunAsync();
}
}
In the Index.html no need add any javascript reference. This file in previous versions is now dynamic add when is needed.
In the component need to use a IndexDb inject DBContext
[Inject]
public DBContext DB { get; set; }
void Select()
{
var PlaysList = await DB.PlaysList.SelectAsync();
}
void Add()
{
var NewItems = new List<PlayList>();
CommandResponse response = await DB.PlaysList.AddAsync(NewItems);
Console.WriteLine(response.Message);
Console.WriteLine(response.Result);
//Get result per each element
foreach (var item in response.Response)
{
Console.WriteLine(item.Message);
Console.WriteLine(item.Result);
}
}
void Update()
{
var NewItem = new PlayList();
CommandResponse response = await DB.PlaysList.UpdateAsync(NewItem);
Console.WriteLine(response.Message);
Console.WriteLine(response.Result);
//Get result per each element
foreach (var item in response.Response)
{
Console.WriteLine(item.Message);
Console.WriteLine(item.Result);
}
}
void Delete()
{
int id = 1;
CommandResponse response = await DB.PlaysList.DeleteAsync(id);
Console.WriteLine(response.Message);
Console.WriteLine(response.Result);
//Get result per each element
foreach (var item in response.Response)
{
Console.WriteLine(item.Message);
Console.WriteLine(item.Result);
}
}
void Drop()
{
CommandResponse response = await DB.DropDatabaseAsync();
Console.WriteLine(response.Message);
Console.WriteLine(response.Result);
}
void Init()
{
//if you delete a db and want to initialize again
await DB.Init();
}
You can modify the model classes any time, but if the model you will pass don't match with the model created when create the IndexDb this will return a exception.
Working with records
In all the select action you will receive the List<TModel> except if you are looking for one Key Id send, then you will receive the Model object. In all actions you will receive a ResponseJsDb model or a List<ResponseJsDb> with all the responses, if you are sending a lot of rows.
public class ResponseJsDb
{
public bool Result { get; set; }
public string Message { get; set; }
}
Working with records from StoreSet
The store set always return the model or list of the model for all select actions and CommandResponse record for the comands actions
public record CommandResponse(bool Result, string Message, List<ResponseJsDb> Response);
Exceptions
When exception will return a ResponseException
public class ResponseException : Exception
{
public string Command { get; set; }
public string StoreName { get; set; }
public string TransactionData { get; set; }
}
More info
Check website for more info.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. 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. |
-
net6.0
- Microsoft.AspNetCore.Components.Web (>= 6.0.32)
- Microsoft.AspNetCore.Components.WebAssembly (>= 6.0.32)
- System.Text.Json (>= 6.0.9)
-
net7.0
- Microsoft.AspNetCore.Components.Web (>= 7.0.20)
- Microsoft.AspNetCore.Components.WebAssembly (>= 7.0.20)
-
net8.0
- Microsoft.AspNetCore.Components.Web (>= 8.0.7)
- Microsoft.AspNetCore.Components.WebAssembly (>= 8.0.7)
- System.Text.Json (>= 8.0.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated | |
---|---|---|---|
1.8.47 | 113 | 10/10/2024 | |
1.7.46 | 180 | 9/5/2024 | |
1.6.45 | 108 | 9/4/2024 | |
1.6.44 | 111 | 8/30/2024 | |
1.6.43 | 137 | 8/17/2024 | |
1.6.42 | 93 | 8/7/2024 | |
1.6.41 | 264 | 6/22/2024 | |
1.6.40 | 281 | 4/16/2024 | |
1.6.39 | 216 | 3/26/2024 | |
1.6.38 | 238 | 2/28/2024 | |
1.6.37 | 179 | 2/27/2024 | |
1.6.36 | 222 | 2/27/2024 | |
1.6.35 | 237 | 2/18/2024 | |
1.6.34 | 646 | 10/10/2023 | |
1.6.33 | 387 | 8/18/2023 | |
1.6.32 | 785 | 3/23/2023 | |
1.6.31 | 809 | 1/28/2023 | |
1.6.30 | 889 | 8/10/2022 | |
1.6.28 | 845 | 5/25/2022 | |
1.6.27 | 855 | 4/3/2022 | |
1.6.26 | 798 | 4/3/2022 | |
1.6.25 | 769 | 4/3/2022 | |
1.5.24 | 621 | 12/28/2021 | |
1.5.23 | 555 | 12/27/2021 | |
1.5.22 | 598 | 12/19/2021 | |
1.5.21 | 665 | 11/15/2021 | |
1.5.20 | 737 | 11/14/2021 | |
1.5.19 | 774 | 10/25/2021 | |
1.5.18 | 680 | 10/24/2021 | |
1.5.17 | 721 | 8/29/2021 | |
1.4.16 | 671 | 8/9/2021 | |
1.4.15 | 693 | 8/8/2021 | |
1.4.14 | 657 | 8/3/2021 | |
1.4.13 | 795 | 7/12/2021 | |
1.3.11 | 743 | 5/8/2021 | |
1.3.10 | 696 | 5/2/2021 |
Fixed bug #3: Update Model from William Chen contribution. Update nugets dependencies.