DrUalcman-BlazorIndexedDb 1.9.49

dotnet add package DrUalcman-BlazorIndexedDb --version 1.9.49
                    
NuGet\Install-Package DrUalcman-BlazorIndexedDb -Version 1.9.49
                    
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="DrUalcman-BlazorIndexedDb" Version="1.9.49" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DrUalcman-BlazorIndexedDb" Version="1.9.49" />
                    
Directory.Packages.props
<PackageReference Include="DrUalcman-BlazorIndexedDb" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DrUalcman-BlazorIndexedDb --version 1.9.49
                    
#r "nuget: DrUalcman-BlazorIndexedDb, 1.9.49"
                    
#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.
#:package DrUalcman-BlazorIndexedDb@1.9.49
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DrUalcman-BlazorIndexedDb&version=1.9.49
                    
Install as a Cake Addin
#tool nuget:?package=DrUalcman-BlazorIndexedDb&version=1.9.49
                    
Install as a Cake Tool

Nuget Nuget

BlazorIndexedDb

Manage IndexedDB from C# with Blazor. A simple way to interact with IndexedDB, similar to how you can do with Entity Framework.

NuGet installation

PM> Install-Package DrUalcman-BlazorIndexedDb

New version changes coming

Working on changing version control to avoid the need to delete the existing database when adding, removing, or modifying a StoreSet.

Current features

Create StoreContext<TStore> from abstract class. Allow multiple StoreContext but database name should be different names. StoreSet per each model you need into a database. Set PrimaryKey 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 of IJSRuntime, which should normally already be registered.

Create a code-first database model and inherit from IndexedDb. You must use the FieldAttribute to configure the properties.

Your model (e.g., PlayList) should contain an Id property or a property marked with the key attribute.

    public class PlayList
    {
        [FieldAttribute(IsKeyPath = true, IsAutoIncremental = 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 create a DBContext class inheriting from StoreContext<TStore> to manage the database, similar to Entity Framework. Define properties that represent your tables.

public class DBContext : StoreContext<DBContext>
    {
        #region properties
        public StoreSet<PlayList> PlayList { 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 index.html, there is no need to add any JavaScript reference. In previous versions this was required, but now it is added dynamically when needed.

In the component, inject DBContext to access IndexedDB.

        [Inject]
        public DBContext DB { get; set; }

        void Select()
        {
            var PlayList = await DB.PlayList.SelectAsync();
        }

        void Add()
        {
            var NewItems = new List<PlayList>();
            CommandResponse response = await DB.PlayList.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.PlayList.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.PlayList.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 select actions, you will receive a List<TModel>, except when querying by a single key, in which case you will receive a single model instance.

All actions return either a ResponseJsDb or a List<ResponseJsDb> when multiple rows are processed.

    public class ResponseJsDb
    {
        public bool Result { get; set; }
        public string Message { get; set; }
    }

Working with records from StoreSet

The store set always returns the model or list of the model for all select actions and CommandResponse record for the commands actions

 public record CommandResponse(bool Result, string Message, List<ResponseJsDb> Response);

Exceptions

When an exception occurs, a ResponseException will be returned.

    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 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 was computed.  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.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.9.49 233 4/28/2026
1.8.48 483 6/1/2025
1.8.47 660 10/10/2024
1.7.46 340 9/5/2024
1.6.45 280 9/4/2024
1.6.44 265 8/30/2024
1.6.43 280 8/17/2024
1.6.42 269 8/7/2024
1.6.41 434 6/22/2024
1.6.40 436 4/16/2024
1.6.39 363 3/26/2024
1.6.38 388 2/28/2024
1.6.37 326 2/27/2024
1.6.36 393 2/27/2024 1.6.36 is deprecated because it has critical bugs.
1.6.35 396 2/18/2024
1.6.34 749 10/10/2023
1.6.33 502 8/18/2023
1.6.32 938 3/23/2023
1.6.31 961 1/28/2023
1.6.30 1,062 8/10/2022
Loading failed

Refactor how to serialize objects. Target NET10. Update dependencies