An open API service indexing awesome lists of open source software.

https://github.com/nwestfall/blazordb

Use IndexedDB in Blazor WebAssembly (WASM)
https://github.com/nwestfall/blazordb

Last synced: about 1 month ago
JSON representation

Use IndexedDB in Blazor WebAssembly (WASM)

Awesome Lists containing this project

README

        

Forked from [https://github.com/Reshiru/Blazor.IndexedDB.Framework](https://github.com/Reshiru/Blazor.IndexedDB.Framework) as inspiration. A lot has changed since original fork and a lot is planned to get it closer.

# BlazorDB

An easy, fast way to use IndexedDB in a Blazor application.

### How to Use (WASM)
- Install `dotnet add package BlazorIndexedDB`
- Add `.js` files to `index.html`
```js


```
- Add `@using BlazorDB` to `_Imports.razor`
- Update `Program.cs` `ServiceCollection` with new databases (can add as many databases as you want)
```c#
builder.Services.AddBlazorDB(options =>
{
options.Name = "Test";
options.Version = 1;
options.StoreSchemas = new List()
{
new StoreSchema()
{
Name = "Person",
PrimaryKey = "id",
PrimaryKeyAuto = true,
UniqueIndexes = new List { "name" }
}
};
});
```
- Inject `IBlazorDbFactory` in your Razor pages
```c#
@inject IBlazorDbFactory _dbFactory
```
- Start using!
```c#
var manager = await _dbFactory.GetDbManager(dbName)
await manager.AddRecord(new StoreRecord()
{
StoreName = storeName,
Record = new { Name = "MyName", Age = 20 }
});
```

### How it works
You defined your databases and store schemes in the `ServicesCollection`. We add a helper called `AddBlazorDB` where you can build ` DbStore`. From there, we ensure that an instance of `IBlazorDbFactory` is available in your service provider and will automatically add all `DbStores` and `IndexedDbManagers` based on what is defined with `AddBlazorDb`. This allows you to have multiple databases if needed.

To access any database/store, all you need to do is inject the `IBlazorDbFactory` in your page and call `GetDbManager(string dbName)`. We will pull your `IndexedDbManager` from the factory and make sure it's created and ready.

Most calls will either be true `Async` or let you pass an `Action` in. If it lets you pass in an `Action`, it is optional. This `Action` will get called when the method is complete. This way, it isn't blocking on `WASMs` single thread. All of these calls return a `Guid` which is the "transaction" identifier to IndexeDB (it's not a true database transaction, just the call between C# and javascript that gets tracked).

If the call is flagged as `Async`, we will wait for the JS callback that it is complete and then return the data. The library takes care of this connection between javascript and C# for you.

### Available Calls
- `Task OpenDb(Action action)` - Open the IndexedDb and make sure it is created, with an optional callback when complete
- `Task DeleteDb(string dbName, Action action)` - Delete the database, with an optional callback when complete
- `Task DeleteDbAsync(string dbName)` - Delete the database and wait for it to complete
- `Task AddRecord(StoreRecord recordToAdd, Action action)` - Add a record to the store, with an optional callback when complete
- `Task AddRecordAsync(StoreRecord recordToAdd)` - Add a record to the store and wait for it to complete
- `Task BulkAddRecord(string storeName, IEnumerable recordsToBulkAdd, Action action)` - Adds records/objects to the specified store in bulk
- `Task BulkAddRecordAsync(string storeName, IEnumerable recordsToBulkAdd)` - Adds records/objects to the specified store in bulk and waits for it to complete
- `Task UpdateRecord(UpdateRecord recordToUpdate, Action action)` - Update a record in the store, with an optional callback when c complete
- `Task UpdateRecordAsync(UpdateRecord recordToUpdate)` - Update a record in the store and wait for it to complete
- `Task GetRecordByIdAsync(string storeName, TInput key)` - Get a record by the 'id' and wait for it to return
- `Task DeleteRecord(string storeName, TInput key, Action action)` - Delete a record in the store by 'id', with an optional callback when complete
- `Task DeleteRecordAsync(string storeName, TInput key)` - Delete a record in the store by 'id' and wait for it to complete
- `Task ClearTable(string storeName, Action action)` - Clears all data from a Table but keeps the table
- `Task ClearTableAsync(string storeName)` - Clears all data from a Table but keeps the table and wait for it to complete

### More to Come
- Real queries
- Automated testing
- Dynamically add/remove from `IBlazorDbFactory`