Udx.IndexedDB.Blazor 2022.6.15

There is a newer version of this package available.
See the version list below for details.
dotnet add package Udx.IndexedDB.Blazor --version 2022.6.15                
NuGet\Install-Package Udx.IndexedDB.Blazor -Version 2022.6.15                
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="Udx.IndexedDB.Blazor" Version="2022.6.15" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Udx.IndexedDB.Blazor --version 2022.6.15                
#r "nuget: Udx.IndexedDB.Blazor, 2022.6.15"                
#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 Udx.IndexedDB.Blazor as a Cake Addin
#addin nuget:?package=Udx.IndexedDB.Blazor&version=2022.6.15

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

**UdxFramework

Udx.IndexedDB.Blazor

An easy way to interact with IndexedDB and make it feel like EF Core but async.

NuGet installation

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

Either install it from command line:

PM> Install-Package Udx.IndexedDB.Blazor

Features

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

How to use

  1. Add TG.Blazor.IndexedDB/indexedDb.Blazor.js to your index.html
<script src="_content/TG.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 : Udx.IndexedDB.Blazor.IndexedDb
    {
        public ExampleDb(IJSRuntime jSRuntime, string name, int version) : base(jSRuntime, name, version) { }

        public IndexedSet<Person> People { get; set; }
        public IndexedSet<Dog> Dog { get; set; }
    }
  • Your model (eg. Person) should contain an Id property or a property marked with the Key attribute.
 public class Person
    {
        [Key]
        public long Id { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        [ForeignKey(nameof(DogId))]
        [Browsable(false)]
        public Dog Dog { get; set; } = new Dog()
        {
            Id = 10,
            Name = "IndexedDB"
        };

        public long DogId { get; set; }
    }

    public class Dog
    {
        public long Id { get; set; }
        public string Name { 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>())
        {
            for (int i = 0; i < 10; i++)
            {
                var p = new Person()
                    {
                        FirstName = Guid.NewGuid().ToString(),
                        LastName = Guid.NewGuid().ToString(),
                        DogId = i,
                        Dog = new Dog
                        {
                            Id = i,
                            Name = $"dog {i}"
                        }

                    };
                db.People.Add(p);
                db.Dog.Add(p.Dog);

            }
            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();
}

Search records

  using (var db = await this.DbFactory.Create<ExampleDb>())
        {
            listPeople = from p in db.People
                         join d in db.Dog on p.DogId equals d.Id
                         where d.Name.Contains("dog")
                         select new Person
                             {
                                 Id = p.Id,
                                 FirstName = p.FirstName,
                                 LastName = p.LastName,
                                 DogId = p.DogId,
                                 Dog = d
                             };
        }

License

Original license. Original license.

Site

Udx Udx.com.cn

Licensed under the MIT license.

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 was computed.  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

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
2022.6.16 490 6/16/2022
2022.6.15 417 6/15/2022