Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getRecords with offset and limit, Add getRecordsCount(str) -> double #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions Blazor.IndexedDB.Test/Components/TestIndexedDb.razor
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
<div>
<p><b>status:</b> @Message</p>
<p><b>Db version:</b> @DbManager.CurrentVersion</p>
<p><b>stores:</b> @string.Join(", ", DbManager.Stores.Select(s => s.Name))</p>
<p><b>stores:</b> @StoreCounts</p>
</div>
<div class="d-flex flex-row align-items-start">
<div class="d-flex flex-column">
<button class="btn btn-outline-primary m-4" @onclick="OpenDatabase">Open Database</button>
<button class="btn btn-outline-primary m-4" @onclick="GetRecords">Get Records</button>
<button class="btn btn-outline-primary m-4" @onclick="GetRecordsPaginated">Get Records Paginated</button>
<button class="btn btn-outline-primary m-4" @onclick="UpdateStoreRecordCounts">Update Store Record Counts</button>
<button class="btn btn-outline-primary m-4" @onclick="ClearStore">Clear Store</button>
<button class="btn btn-outline-primary m-4" @onclick="DeleteDatabase">Delete Database</button>
<button class="btn btn-outline-danger m-4" @onclick="Add130Records">Pre-populate Store</button>
Expand Down Expand Up @@ -70,11 +72,13 @@

string NewStoreName { get; set; } = "";

string StoreCounts { get; set; } = "";


protected override void OnInitialized()
{
DbManager.ActionCompleted += OnIndexedDbNotification;

StoreCounts = string.Join(", ", DbManager.Stores.Select(s => s.Name + ": 0"));
}

public void Dispose()
Expand Down Expand Up @@ -111,6 +115,48 @@

StateHasChanged();
}

protected async void GetRecordsPaginated()
{
const uint pageSize = 15;
var results = new List<Person>();
var personCount = (ulong) await DbManager.GetRecordsCount(DbManager.Stores[0].Name);
for (var i = 0u; i < personCount; i += pageSize)
{
results.AddRange(await DbManager.GetRecords<Person>(DbManager.Stores[0].Name, pageSize, i));
}

if (results.Any())
{
People = results;
}
else
{
People.Clear();
Message = "No Records found";
}

StateHasChanged();
}

protected async Task UpdateStoreRecordCounts()
{
var first = true;
var sb = new StringBuilder();

foreach (var store in DbManager.Stores)
{
if (!first)
sb.Append(", ");

first = false;
sb.Append(store.Name);
sb.Append(": ");
sb.Append(await DbManager.GetRecordsCount(store.Name));
}

StoreCounts = sb.ToString();
}

protected async void Add130Records()
{
Expand Down
2 changes: 2 additions & 0 deletions TG.Blazor.IndexedDb/IndexedDB/DbFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public struct DbFunctions
public const string AddRecord = "addRecord";
public const string UpdateRecord = "updateRecord";
public const string GetRecords = "getRecords";
public const string GetRecordsCount = "getRecordsCount";
public const string GetRecordsOffset = "getRecordsOffset";
public const string OpenDb = "openDb";
public const string DeleteRecord = "deleteRecord";
public const string GetRecordById = "getRecordById";
Expand Down
51 changes: 51 additions & 0 deletions TG.Blazor.IndexedDb/IndexedDB/IndexedDBManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,57 @@ public async Task<List<TResult>> GetRecords<TResult>(string storeName)
}

}

/// <summary>
/// Gets all of the records in a given store, up to a limit, with an offset.
/// </summary>
/// <typeparam name="TResult"></typeparam>
/// <param name="storeName">The name of the store from which to retrieve the records</param>
/// <param name="limit">Maximum number of records to return (Max 2^32-1, aka UInt32.MaxValue)</param>
/// <param name="offset">Offset from start into records results by offset count</param>
/// <returns></returns>
public async Task<List<TResult>> GetRecords<TResult>(string storeName, uint limit, uint offset = 0)
{
await EnsureDbOpen();
try
{
var results = await CallJavascript<List<TResult>>(DbFunctions.GetRecordsOffset, storeName, limit, offset);

RaiseNotification(IndexDBActionOutCome.Successful, $"Retrieved {results.Count} records from {storeName}");

return results;
}
catch (JSException jse)
{
RaiseNotification(IndexDBActionOutCome.Failed, jse.Message);
return default;
}

}

/// <summary>
/// Gets count all of the records in a given store.
/// </summary>
/// <param name="storeName">The name of the store from which records will be counted.</param>
/// <returns></returns>
public async Task<double> GetRecordsCount(string storeName)
{
await EnsureDbOpen();
try
{
var results = await CallJavascript<double>(DbFunctions.GetRecordsCount, storeName);

RaiseNotification(IndexDBActionOutCome.Successful, $"Retrieved count of {results} records from {storeName}");

return results;
}
catch (JSException jse)
{
RaiseNotification(IndexDBActionOutCome.Failed, jse.Message);
return default;
}

}

/// <summary>
/// Retrieve a record by id
Expand Down
35 changes: 35 additions & 0 deletions TG.Blazor.IndexedDb/client/indexedDbBlazor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,41 @@ export class IndexedDbManager {

return results;
}

public getRecordsCount = async (storeName: string): Promise<number> => {
const tx = this.getTransaction(this.dbInstance, storeName, 'readonly');
const count: number = await tx.objectStore(storeName).count();
await tx.complete;
return count;
}

public getRecordsOffset = async (storeName: string, limit: bigint, offset: bigint): Promise<any> => {
const tx = this.getTransaction(this.dbInstance, storeName, 'readonly');
let i = 0;
let results: any[] = [];

tx.objectStore(storeName)
.iterateCursor(cursor => {
if (!cursor) {
return;
}

if (i < offset) {
i += 1;
cursor.continue();
return;
}
else if (results.length < limit) {
results.push(cursor.value);
}

cursor.continue();
});

await tx.complete;

return results;
}

public clearStore = async (storeName: string): Promise<string> => {

Expand Down
30 changes: 29 additions & 1 deletion TG.Blazor.IndexedDb/wwwroot/indexedDb.Blazor.js

Large diffs are not rendered by default.