Udx.IndexedDB.Blazor
2022.6.16
dotnet add package Udx.IndexedDB.Blazor --version 2022.6.16
NuGet\Install-Package Udx.IndexedDB.Blazor -Version 2022.6.16
<PackageReference Include="Udx.IndexedDB.Blazor" Version="2022.6.16" />
paket add Udx.IndexedDB.Blazor --version 2022.6.16
#r "nuget: Udx.IndexedDB.Blazor, 2022.6.16"
// Install Udx.IndexedDB.Blazor as a Cake Addin #addin nuget:?package=Udx.IndexedDB.Blazor&version=2022.6.16 // Install Udx.IndexedDB.Blazor as a Cake Tool #tool nuget:?package=Udx.IndexedDB.Blazor&version=2022.6.16
**UdxFramework
Udx.IndexedDB.Blazor
An easy way to interact with IndexedDB and make it feel like EF Core but async
.
#GitHub repository
The GitHub repository is at: https://github.com/udxframework/Udx-IndexedDB-Blazor
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
- Add
TG.Blazor.IndexedDB/indexedDb.Blazor.js
to yourindex.html
<script src="_content/TG.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 : 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 anId
property or a property marked with theKey
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; }
}
- 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.
Site
Udx Udx.com.cn
Licensed under the MIT license.
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 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. |
-
net6.0
- TG.Blazor.IndexedDB (>= 1.5.0-preview)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.