Thanks to visit codestin.com
Credit goes to github.com

Skip to content

This is a simple wrapper of the idb-keyval.js library for Blazor applications. It is designed to be used similar to Entity Framework.

License

Notifications You must be signed in to change notification settings

Spylak/BlazorIDB

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BlazorIDB

This is a simple wrapper of the idb-keyval.js library for Blazor applications. It is designed to be used similar to Entity Framework Database. You can have a look at it here.

Create an entity Class

//A property called Id with the type of string is needed
public class MyEntity
{
    public string Id {get; set;}
    public string Property {get; set;}
    public int Property2 {get; set;}
    public List<int> Property3 {get; set;}
    public InnerClass InnerProperty {get; set;}

    public class InnerClass
    {
        public string Property {get; set;}
    }
}

Create Database Class

using BlazorIDB;

public class IndexedDb
{
    public IndexedDbTable<MyEntity> Entity;
    public IndexedDb(IJSRuntime jsRuntime)
    {
        //for each entity you have to provide different key, in this case we provide <<myentity>> as key
        Entity = new IndexedDbTable<MyEntity>(jsRuntime , "myentity");
    }
}

Add Database Class to Services in Program.cs

builder.Services.AddBlazorIDB<IndexedDb>();

or
    
builder.Services.AddSingleton<IndexedDb>();

Inject the database class to any Blazor page you want to use it

@inject IndexedDb _indexedDb

Use it as follows

//Get all
var allEntities = await _indexedDb.Entity.GetAllAsync();

//GetById, the id used is a string
var getEntity = await _indexedDb.Entity.GetByIdAsync(id);

//When adding either single entity or many they generate a new Guid that is then saved as string.
//AddAsync
var entity=new MyEntity()
{
    Props...
}
await _indexedDb.Entity.AddAsync(entity);
//AddRangeAsync
var entityList = new List<MyEntity>()
{
    new MyEntity()
        {
            Props...
        },
    new MyEntity()
        {
            Props...
        }
}
await _indexedDb.Entity.AddRangeAsync(entityList);

//UpdateAsync
entity.Prop = SomethingNew;
await _indexedDb.Entity.UpdateAsync(entity);

//UpdateRangeAsync
entityList.FirstOrDefault().Prop = SomethingNew;
await _indexedDb.Entity.UpdateRangeAsync(entityList);

//RemoveAsync
await _indexedDb.Entity.RemoveAsync(entity);

//RemoveRangeAsync
await _indexedDb.Entity.RemoveRangeAsync(entityList);

//DropTableAsync, this deletes the key provided to the entity and its values
await _indexedDb.Entity.DropTableAsync();

About

This is a simple wrapper of the idb-keyval.js library for Blazor applications. It is designed to be used similar to Entity Framework.

Resources

License

Stars

Watchers

Forks

Packages

No packages published