mbdavid / LiteDB.WebAssembly

WebAssembly version of LiteDB to run on client (browser)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

LiteDB for Blazor - WebAssembly

This repository contains some cool tests about LiteDB and ASP.NET Blazor in client side (WebAssembly). This is not a final product! (yet 😄)

How will works

LiteDB.WebAssembly forks LiteDB v5.1 branch with master updates to start a smaller and focused version for wasm. Some LiteDB was removed in this version (maybe can back in future).

Blazor runs in browser using WebAssembly. There is no disk access in browsers so all your data will be sotored into IndexedDB and/or LocalStorage. Works with a single database per domain and async read/write operations only. All locks will be removed and there is no support for concurrency or transactions (exclusive lock only).

All methods are converted into async and has an Async sufix. Also, this version needs to run await OpenAsync() before use database.

Removed features

  • Encryption
  • Transactions
  • Shared Connection
  • FileStorage
  • SortDisk

How to use

Register ILiteDatabase service with a custom Stream. You can use MemoryStream, LocalStorageStream or IndexedDBStream (not yet).

// in memory database
builder.Services.AddScoped<ILiteDatabase>(sp => new LiteDatabase(new MemoryStream()));

// store data in local storage
builder.Services.AddScoped<ILiteDatabase>(sp => new LiteDatabase(new LocalStorageStream()));

You must call open database before use

@page "/db-demo"
@inject ILiteDatabase _db;

    private ILiteCollection<Person> _personCollection;

    protected override async Task OnInitializedAsync()
    {
        await _db.OpenAsync();
        
        _personCollection = _db.GetCollection<Person>();
    }

About

WebAssembly version of LiteDB to run on client (browser)

License:MIT License


Languages

Language:C# 98.6%Language:HTML 0.9%Language:CSS 0.3%Language:JavaScript 0.1%