Skip to content

Fix for enums. #32

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

Merged
merged 2 commits into from
Feb 14, 2025
Merged
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
13 changes: 11 additions & 2 deletions IndexDb.Example/Models/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,16 @@ public bool GetTest()
return true;
}

}
[Flags]
public enum Permissions
{
None = 0,
CanRead = 1,
CanWrite = 1 << 1,
CanDelete = 1 << 2,
CanCreate = 1 << 3
}


public Permissions Access { get; set; }
}
}
3 changes: 2 additions & 1 deletion IndexDb.Example/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<th>Age</th>
<th>DecryptedSecret</th>
<th>Encrypted Secret</th>
<th>Access</th>
</tr>
</thead>
<tbody>
Expand All @@ -69,7 +70,7 @@
@person.Secret
</div>
</td>

<td>@person.Access</td>
</tr>
}
</tbody>
Expand Down
115 changes: 50 additions & 65 deletions IndexDb.Example/Pages/Index.razor.cs
Original file line number Diff line number Diff line change
@@ -1,80 +1,65 @@
using Magic.IndexedDb.Models;
using System;
namespace IndexDb.Example.Pages;

namespace IndexDb.Example.Pages
public partial class Index
{
public partial class Index
private List<Person> allPeople { get; set; } = new List<Person>();
private IEnumerable<Person> WhereExample { get; set; } = Enumerable.Empty<Person>();
private double storageQuota { get; set; }
private double storageUsage { get; set; }
protected override async Task OnAfterRenderAsync(bool firstRender)
{
private List<Person> allPeople { get; set; } = new List<Person>();

private IEnumerable<Person> WhereExample { get; set; } = Enumerable.Empty<Person>();

private double storageQuota { get; set; }
private double storageUsage { get; set; }
protected override async Task OnAfterRenderAsync(bool firstRender)
if (firstRender)
{
if (firstRender)
try
{
var manager = await _MagicDb.GetDbManagerAsync(DbNames.Client);

try
{
var manager = await _MagicDb.GetDbManagerAsync(DbNames.Client);
await manager.ClearTableAsync<Person>();

await manager.ClearTableAsync<Person>();

var AllThePeeps = await manager.GetAllAsync<Person>();
if (AllThePeeps.Count() < 1)
{
Person[] persons = new Person[] {
new Person { Name = "Zack", TestInt = 9, _Age = 45, GUIY = Guid.NewGuid(), Secret = "I buried treasure behind my house"},
new Person { Name = "Luna", TestInt = 9, _Age = 35, GUIY = Guid.NewGuid(), Secret = "Jerry is my husband and I had an affair with Bob."},
new Person { Name = "Jerry", TestInt = 9, _Age = 35, GUIY = Guid.NewGuid(), Secret = "My wife is amazing"},
new Person { Name = "Jon", TestInt = 9, _Age = 37, GUIY = Guid.NewGuid(), Secret = "I black mail Luna for money because I know her secret"},
new Person { Name = "Jack", TestInt = 9, _Age = 37, GUIY = Guid.NewGuid(), Secret = "I have a drug problem"},
new Person { Name = "Cathy", TestInt = 9, _Age = 22, GUIY = Guid.NewGuid(), Secret = "I got away with reading Bobs diary."},
new Person { Name = "Bob", TestInt = 3 , _Age = 69, GUIY = Guid.NewGuid(), Secret = "I caught Cathy reading my diary, but I'm too shy to confront her." },
new Person { Name = "Alex", TestInt = 3 , _Age = 80, GUIY = Guid.NewGuid(), Secret = "I'm naked! But nobody can know!" }
if (!(await manager.GetAllAsync<Person>()).Any())
{
Person[] persons = new Person[] {
new Person { Name = "Zack", TestInt = 9, _Age = 45, GUIY = Guid.NewGuid(), Secret = "I buried treasure behind my house", Access=Person.Permissions.CanRead},
new Person { Name = "Luna", TestInt = 9, _Age = 35, GUIY = Guid.NewGuid(), Secret = "Jerry is my husband and I had an affair with Bob.", Access = Person.Permissions.CanRead|Person.Permissions.CanWrite},
new Person { Name = "Jerry", TestInt = 9, _Age = 35, GUIY = Guid.NewGuid(), Secret = "My wife is amazing", Access = Person.Permissions.CanRead|Person.Permissions.CanWrite|Person.Permissions.CanCreate},
new Person { Name = "Jon", TestInt = 9, _Age = 37, GUIY = Guid.NewGuid(), Secret = "I black mail Luna for money because I know her secret", Access = Person.Permissions.CanRead},
new Person { Name = "Jack", TestInt = 9, _Age = 37, GUIY = Guid.NewGuid(), Secret = "I have a drug problem", Access = Person.Permissions.CanRead|Person.Permissions.CanWrite},
new Person { Name = "Cathy", TestInt = 9, _Age = 22, GUIY = Guid.NewGuid(), Secret = "I got away with reading Bobs diary.", Access = Person.Permissions.CanRead | Person.Permissions.CanWrite},
new Person { Name = "Bob", TestInt = 3 , _Age = 69, GUIY = Guid.NewGuid(), Secret = "I caught Cathy reading my diary, but I'm too shy to confront her.", Access = Person.Permissions.CanRead },
new Person { Name = "Alex", TestInt = 3 , _Age = 80, GUIY = Guid.NewGuid(), Secret = "I'm naked! But nobody can know!" }
};

await manager.AddRangeAsync(persons);
}


//var StorageLimit = await manager.GetStorageEstimateAsync();
var storageInfo = await manager.GetStorageEstimateAsync();
storageQuota = storageInfo.QuotaInMegabytes;
storageUsage = storageInfo.UsageInMegabytes;

var allPeopleDecrypted = await manager.GetAllAsync<Person>();

foreach (Person person in allPeopleDecrypted)
{
person.SecretDecrypted = await manager.DecryptAsync(person.Secret);
allPeople.Add(person);
}

WhereExample = await manager.Where<Person>(x => x.Name.StartsWith("c", StringComparison.OrdinalIgnoreCase)
|| x.Name.StartsWith("l", StringComparison.OrdinalIgnoreCase)
|| x.Name.StartsWith("j", StringComparison.OrdinalIgnoreCase) && x._Age > 35
).OrderBy(x => x._Id).Skip(1).Execute();


/*
* Still working on allowing nested
*/
//// Should return "Zack"
//var NestedResult = await manager.Where<Person>(p => (p.Name == "Zack" || p.Name == "Luna") && (p._Age >= 35 && p._Age <= 45)).Execute();

//// should return "Luna", "Jerry" and "Jon"
//var NonNestedResult = await manager.Where<Person>(p => p.TestInt == 9 && p._Age >= 35 && p._Age <= 45).Execute();

StateHasChanged();
await manager.AddRangeAsync(persons);
}
catch (Exception ex)

var storageInfo = await manager.GetStorageEstimateAsync();
storageQuota = storageInfo.QuotaInMegabytes;
storageUsage = storageInfo.UsageInMegabytes;

var allPeopleDecrypted = await manager.GetAllAsync<Person>();
foreach (Person person in allPeopleDecrypted)
{
Console.WriteLine(ex.Message);
person.SecretDecrypted = await manager.DecryptAsync(person.Secret);
allPeople.Add(person);
}

WhereExample = await manager.Where<Person>(x => x.Name.StartsWith("c", StringComparison.OrdinalIgnoreCase)
|| x.Name.StartsWith("l", StringComparison.OrdinalIgnoreCase)
|| x.Name.StartsWith("j", StringComparison.OrdinalIgnoreCase) && x._Age > 35
).OrderBy(x => x._Id).Skip(1).Execute();

/*
* Still working on allowing nested
*/
//// Should return "Zack"
//var NestedResult = await manager.Where<Person>(p => (p.Name == "Zack" || p.Name == "Luna") && (p._Age >= 35 && p._Age <= 45)).Execute();

//// should return "Luna", "Jerry" and "Jon"
//var NonNestedResult = await manager.Where<Person>(p => p.TestInt == 9 && p._Age >= 35 && p._Age <= 45).Execute();
StateHasChanged();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
Expand Down
20 changes: 12 additions & 8 deletions Magic.IndexedDb/IndexDbManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal IndexedDbManager(DbStore dbStore, IJSRuntime jsRuntime)
{
this._dbStore = dbStore;
this._jsModule = jsRuntime.InvokeAsync<IJSObjectReference>(
"import",
"import",
"./_content/Magic.IndexedDb/magicDB.js").AsTask();
}

Expand Down Expand Up @@ -237,8 +237,8 @@ public async Task<string> DecryptAsync(
/// <param name="recordsToBulkAdd">An instance of StoreRecord that provides the store name and the data to add</param>
/// <returns></returns>
private Task BulkAddRecordAsync<T>(
string storeName,
IEnumerable<T> recordsToBulkAdd,
string storeName,
IEnumerable<T> recordsToBulkAdd,
CancellationToken cancellationToken = default)
{
// TODO: https://github.com/magiccodingman/Magic.IndexedDb/issues/9
Expand Down Expand Up @@ -362,7 +362,7 @@ public async Task<int> UpdateRangeAsync<T>(
}

public async Task<T?> GetByIdAsync<T>(
object key,
object key,
CancellationToken cancellationToken = default) where T : class
{
string schemaName = SchemaHelper.GetSchemaName<T>();
Expand Down Expand Up @@ -418,7 +418,7 @@ private Expression<Func<T, bool>> PreprocessPredicate<T>(Expression<Func<T, bool
}

internal async Task<IList<T>?> WhereV2Async<T>(
string storeName, List<string> jsonQuery, MagicQuery<T> query,
string storeName, List<string> jsonQuery, MagicQuery<T> query,
CancellationToken cancellationToken) where T : class
{
string? jsonQueryAdditions = null;
Expand All @@ -429,7 +429,7 @@ private Expression<Func<T, bool>> PreprocessPredicate<T>(Expression<Func<T, bool
var propertyMappings = ManagerHelper.GeneratePropertyMapping<T>();
IList<Dictionary<string, object>>? ListToConvert =
await CallJsAsync<IList<Dictionary<string, object>>>
(IndexedDbFunctions.WHERE, cancellationToken,
(IndexedDbFunctions.WHERE, cancellationToken,
[DbName, storeName, jsonQuery.ToArray(), jsonQueryAdditions!, query?.ResultsUnique!]);

var resultList = ConvertListToRecords<T>(ListToConvert, propertyMappings);
Expand Down Expand Up @@ -468,16 +468,20 @@ private object ConvertValueToType(object value, Type targetType)
{
return Guid.Parse(stringValue);
}
if (targetType.IsEnum)
{
return Enum.ToObject(targetType, Convert.ToInt64(value));
}

var nullableType = Nullable.GetUnderlyingType(targetType);
if (nullableType != null)
{
// It's nullable
if (value == null) return null;
if (value == null)
return null;

return Convert.ChangeType(value, nullableType);
}

return Convert.ChangeType(value, targetType);
}

Expand Down