Skip to content

Commit 0973f7b

Browse files
committed
fixed merge conflicts from recent PRs
1 parent 4000c5b commit 0973f7b

File tree

7 files changed

+114
-3
lines changed

7 files changed

+114
-3
lines changed

example/BlazorDB.Example/Pages/Index.razor

+33
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
<button @onclick="ToArray">ToArray</button>
2020
<button @onclick="AddWithCallback">Add With Callback</button>
2121
<button @onclick="DeleteDb">Delete DB</button>
22+
<button @onclick="WhereName">Where Name</button>
23+
<button @onclick="WhereId">Where Id</button>
24+
<button @onclick="WhereNameAndId">Where Id and Name</button>
2225

2326

2427
<h1>Item</h1>
@@ -38,6 +41,9 @@
3841
<button @onclick="ToArray">ToArray</button>
3942
<button @onclick="AddWithCallback">Add With Callback</button>
4043
<button @onclick="DeleteDb">Delete DB</button>
44+
<button @onclick="WhereName">Where Name</button>
45+
<button @onclick="WhereId">Where Id</button>
46+
<button @onclick="WhereNameAndId">Where Id and Name</button>
4147

4248
@code {
4349
IndexedDbManager manager;
@@ -188,4 +194,31 @@
188194
{
189195
await manager.DeleteDb(dbName);
190196
}
197+
198+
private async Task WhereName()
199+
{
200+
var items = await manager.Where<object>(storeName, "name", "MyName");
201+
foreach (var item in items) {
202+
Console.WriteLine(item);
203+
}
204+
}
205+
206+
private async void WhereId()
207+
{
208+
var items = await manager.Where<object>(storeName, "id", 1);
209+
foreach (var item in items) {
210+
Console.WriteLine(item);
211+
}
212+
}
213+
214+
private async void WhereNameAndId()
215+
{
216+
var filters = new List<IndexFilterValue>();
217+
filters.Add(new IndexFilterValue("id", 1));
218+
filters.Add(new IndexFilterValue("name", "MyName"));
219+
var items = await manager.Where<object>(storeName, filters);
220+
foreach (var item in items) {
221+
Console.WriteLine(item);
222+
}
223+
}
191224
}

example/BlazorDB.Example/wwwroot/index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
<a class="dismiss">🗙</a>
2020
</div>
2121
<script src="_framework/blazor.webassembly.js"></script>
22-
<script src="_content/BlazorDB/dexie.min.js"></script>
23-
<script src="_content/BlazorDB/blazorDB.js"></script>
22+
<script src="dexie.min.js"></script>
23+
<script src="blazorDB.js"></script>
2424
</body>
2525

2626
</html>

global.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"sdk": {
3+
"version": "3.1.x"
4+
}
5+
}

src/BlazorDB/IndexDbManager.cs

+36-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
43
using System.Threading.Tasks;
54
using Microsoft.JSInterop;
65

@@ -278,6 +277,42 @@ public async Task<TResult> GetRecordByIdAsync<TInput, TResult>(string storeName,
278277

279278
return default(TResult);
280279
}
280+
281+
/// <summary>
282+
/// Filter a store on an indexed value
283+
/// </summary>
284+
/// <param name="storeName">The name of the store to retrieve the records from</param>
285+
/// <param name="indexName">index field name to filter on</param>
286+
/// <param name="filterValue">filter's value</param>
287+
/// <returns></returns>
288+
public async Task<IList<TRecord>> Where<TRecord>(string storeName, string indexName, object filterValue)
289+
{
290+
var filters = new List<IndexFilterValue>() { new IndexFilterValue(indexName, filterValue) };
291+
return await Where<TRecord>(storeName, filters);
292+
}
293+
294+
/// <summary>
295+
/// Filter a store on indexed values
296+
/// </summary>
297+
/// <param name="storeName">The name of the store to retrieve the records from</param>
298+
/// <param name="filters">A collection of index names and filters conditions</param>
299+
/// <returns></returns>
300+
public async Task<IList<TRecord>> Where<TRecord>(string storeName, IEnumerable<IndexFilterValue> filters)
301+
{
302+
var trans = GenerateTransaction(null);
303+
304+
try
305+
{
306+
307+
return await CallJavascript<IList<TRecord>>(IndexedDbFunctions.WHERE, trans, DbName, storeName, filters);
308+
}
309+
catch (JSException jse)
310+
{
311+
RaiseEvent(trans, true, jse.Message);
312+
}
313+
314+
return default;
315+
}
281316

282317
/// <summary>
283318
/// Retrieve all the records in a store

src/BlazorDB/IndexFilterValue.cs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace BlazorDB
2+
{
3+
public class IndexFilterValue
4+
{
5+
public IndexFilterValue(string indexName, object filterValue)
6+
{
7+
IndexName = indexName;
8+
FilterValue = filterValue;
9+
}
10+
11+
public string IndexName { get; set; }
12+
public object FilterValue { get; set; }
13+
}
14+
}

src/BlazorDB/IndexedDbFunctions.cs

+1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ internal struct IndexedDbFunctions
1212
public const string CLEAR_TABLE = "clear";
1313
public const string FIND_ITEM = "findItem";
1414
public const string TOARRAY = "toArray";
15+
public const string WHERE = "where";
1516
}
1617
}

src/BlazorDB/wwwroot/blazorDB.js

+23
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,28 @@ window.blazorDB = {
175175
resolve(table);
176176
});
177177
});
178+
},
179+
createFilterObject: function (filters) {
180+
const jsonFilter = {};
181+
for (const filter in filters) {
182+
if (filters.hasOwnProperty(filter))
183+
jsonFilter[filters[filter].indexName] = filters[filter].filterValue;
184+
}
185+
return jsonFilter;
186+
},
187+
where: function(dotnetReference, transaction, dbName, storeName, filters) {
188+
const filterObject = this.createFilterObject(filters);
189+
return new Promise((resolve, reject) => {
190+
window.blazorDB.getTable(dbName, storeName).then(table => {
191+
table.where(filterObject).toArray(items => {
192+
dotnetReference.invokeMethodAsync('BlazorDBCallback', transaction, false, 'where succeeded');
193+
resolve(items);
194+
})
195+
}).catch(e => {
196+
console.error(e);
197+
dotnetReference.invokeMethodAsync('BlazorDBCallback', transaction, true, 'where failed');
198+
reject(e);
199+
});
200+
});
178201
}
179202
}

0 commit comments

Comments
 (0)