diff --git a/IndexDb.Example/Models/Person.cs b/IndexDb.Example/Models/Person.cs index 1277ebd..b3f1a7a 100644 --- a/IndexDb.Example/Models/Person.cs +++ b/IndexDb.Example/Models/Person.cs @@ -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; } + } } diff --git a/IndexDb.Example/Pages/Index.razor b/IndexDb.Example/Pages/Index.razor index 779de8e..91d4125 100644 --- a/IndexDb.Example/Pages/Index.razor +++ b/IndexDb.Example/Pages/Index.razor @@ -50,6 +50,7 @@ Age DecryptedSecret Encrypted Secret + Access @@ -69,7 +70,7 @@ @person.Secret - + @person.Access } diff --git a/IndexDb.Example/Pages/Index.razor.cs b/IndexDb.Example/Pages/Index.razor.cs index f4850c2..0f6f47b 100644 --- a/IndexDb.Example/Pages/Index.razor.cs +++ b/IndexDb.Example/Pages/Index.razor.cs @@ -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 allPeople { get; set; } = new List(); + private IEnumerable WhereExample { get; set; } = Enumerable.Empty(); + private double storageQuota { get; set; } + private double storageUsage { get; set; } + protected override async Task OnAfterRenderAsync(bool firstRender) { - private List allPeople { get; set; } = new List(); - - private IEnumerable WhereExample { get; set; } = Enumerable.Empty(); - - 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(); - await manager.ClearTableAsync(); - - var AllThePeeps = await manager.GetAllAsync(); - 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()).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(); - - foreach (Person person in allPeopleDecrypted) - { - person.SecretDecrypted = await manager.DecryptAsync(person.Secret); - allPeople.Add(person); - } - - WhereExample = await manager.Where(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(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(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(); + foreach (Person person in allPeopleDecrypted) { - Console.WriteLine(ex.Message); + person.SecretDecrypted = await manager.DecryptAsync(person.Secret); + allPeople.Add(person); } + WhereExample = await manager.Where(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(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(p => p.TestInt == 9 && p._Age >= 35 && p._Age <= 45).Execute(); + StateHasChanged(); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); } } } diff --git a/Magic.IndexedDb/IndexDbManager.cs b/Magic.IndexedDb/IndexDbManager.cs index d43c0d2..dcd9bf5 100644 --- a/Magic.IndexedDb/IndexDbManager.cs +++ b/Magic.IndexedDb/IndexDbManager.cs @@ -35,7 +35,7 @@ internal IndexedDbManager(DbStore dbStore, IJSRuntime jsRuntime) { this._dbStore = dbStore; this._jsModule = jsRuntime.InvokeAsync( - "import", + "import", "./_content/Magic.IndexedDb/magicDB.js").AsTask(); } @@ -237,8 +237,8 @@ public async Task DecryptAsync( /// An instance of StoreRecord that provides the store name and the data to add /// private Task BulkAddRecordAsync( - string storeName, - IEnumerable recordsToBulkAdd, + string storeName, + IEnumerable recordsToBulkAdd, CancellationToken cancellationToken = default) { // TODO: https://github.com/magiccodingman/Magic.IndexedDb/issues/9 @@ -362,7 +362,7 @@ public async Task UpdateRangeAsync( } public async Task GetByIdAsync( - object key, + object key, CancellationToken cancellationToken = default) where T : class { string schemaName = SchemaHelper.GetSchemaName(); @@ -418,7 +418,7 @@ private Expression> PreprocessPredicate(Expression?> WhereV2Async( - string storeName, List jsonQuery, MagicQuery query, + string storeName, List jsonQuery, MagicQuery query, CancellationToken cancellationToken) where T : class { string? jsonQueryAdditions = null; @@ -429,7 +429,7 @@ private Expression> PreprocessPredicate(Expression(); IList>? ListToConvert = await CallJsAsync>> - (IndexedDbFunctions.WHERE, cancellationToken, + (IndexedDbFunctions.WHERE, cancellationToken, [DbName, storeName, jsonQuery.ToArray(), jsonQueryAdditions!, query?.ResultsUnique!]); var resultList = ConvertListToRecords(ListToConvert, propertyMappings); @@ -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); }