Skip to content

Commit 6159dc9

Browse files
committed
Weired CompoundIndex issue
1 parent 38b3873 commit 6159dc9

File tree

7 files changed

+96
-37
lines changed

7 files changed

+96
-37
lines changed

E2eTest/OpenTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public async Task DirectOpenTest()
2222
var databases = await page.EvaluateAsync<DatabaseInfo[]>("indexedDB.databases()");
2323
// The actual version will be 10:
2424
// https://dexie.org/docs/Dexie/Dexie.version()
25-
Assert.IsTrue(databases!.Any(x => x.Name == "Person" && x.Version == 10));
25+
Assert.IsTrue(databases!.Any(x => x.Name == "Employee" && x.Version == 10));
2626
}
2727

2828

E2eTestWebApp/Models/IndexedDbContext.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.

E2eTestWebApp/Models/Person.cs

Lines changed: 69 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,90 @@
1-
using Magic.IndexedDb;
1+
using E2eTestWebApp.Repository;
2+
using Magic.IndexedDb;
23
using Magic.IndexedDb.SchemaAnnotations;
34

45
namespace E2eTestWebApp.Models;
56

7+
public class Nested
8+
{
9+
public string Value { get; set; } = "abc";
10+
}
11+
612
public class Person : MagicTableTool<Person>, IMagicTable<Person.DbSets>
713
{
8-
public int Id { get; set; }
14+
public List<IMagicCompoundIndex> GetCompoundIndexes() =>
15+
new List<IMagicCompoundIndex>() {
16+
CreateCompoundIndex(x => x.TestIntStable2, x => x.Name)
17+
};
918

19+
// Endobject when compund key contains two or more keys
1020
public IMagicCompoundKey GetKeys() =>
11-
CreatePrimaryKey(x => x.Id, true); // Auto-incrementing primary key
12-
13-
public List<IMagicCompoundIndex>? GetCompoundIndexes() => [];
21+
CreateCompoundKey(x => x.TestIntStable2, x => x.TestIntStable);
1422

1523
public string GetTableName() => "Person";
16-
public IndexedDbSet GetDefaultDatabase() => IndexedDbContext.Person;
17-
24+
public IndexedDbSet GetDefaultDatabase() => IndexDbContext.Client;
25+
1826
public DbSets Databases { get; } = new();
27+
1928
public sealed class DbSets
2029
{
21-
public readonly IndexedDbSet Person = IndexedDbContext.Person;
30+
public readonly IndexedDbSet Client = IndexDbContext.Client;
31+
public readonly IndexedDbSet Employee = IndexDbContext.Employee;
2232
}
2333

24-
[MagicIndex] // Creates an index on this field
34+
public int TestIntStable { get; set; }
35+
public int TestIntStable2 { get; set; } = 10;
36+
37+
public Nested Nested { get; set; } = new Nested();
38+
39+
[MagicName("_id")]
40+
public int _Id { get; set; }
41+
42+
[MagicName("guid1")]
43+
public Guid Guid1 { get; set; } = new Guid();
44+
45+
[MagicName("guid2")]
46+
public Guid Guid2 { get; set; } = new Guid();
47+
48+
[MagicIndex]
2549
public string Name { get; set; }
2650

27-
[MagicUniqueIndex("guid")] // Unique constraint
28-
public Guid UniqueGuid { get; set; } = Guid.NewGuid();
51+
[MagicName("Age")]
52+
public int _Age { get; set; }
53+
54+
[MagicIndex("TestInt")]
55+
public int TestInt { get; set; }
2956

30-
public int Age { get; set; }
57+
public DateTime? DateOfBirth { get; set; }
3158

32-
[MagicNotMapped] // Exclude from IndexedDB schema
59+
[MagicUniqueIndex("guid")]
60+
public Guid GUIY { get; set; } = Guid.NewGuid();
3361
public string Secret { get; set; }
62+
63+
[MagicNotMapped]
64+
public string DoNotMapTest { get; set; }
65+
66+
[MagicNotMapped]
67+
public static string DoNotMapTest2 { get; set; }
68+
69+
[MagicNotMapped]
70+
public string SecretDecrypted { get; set; }
71+
72+
private bool testPrivate { get; set; } = false;
73+
74+
public bool GetTest()
75+
{
76+
return true;
77+
}
78+
79+
[Flags]
80+
public enum Permissions
81+
{
82+
None = 0,
83+
CanRead = 1,
84+
CanWrite = 1 << 1,
85+
CanDelete = 1 << 2,
86+
CanCreate = 1 << 3
87+
}
88+
89+
public Permissions Access { get; set; }
3490
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Magic.IndexedDb;
2+
using Magic.IndexedDb.Interfaces;
3+
4+
namespace E2eTestWebApp.Repository;
5+
6+
public class IndexDbContext : IMagicRepository
7+
{
8+
public static readonly IndexedDbSet Client = new ("Client");
9+
public static readonly IndexedDbSet Employee = new ("Employee");
10+
public static readonly IndexedDbSet Animal = new ("Animal");
11+
}

E2eTestWebApp/TestPages/OpenTestPage.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using E2eTestWebApp.Models;
2+
using E2eTestWebApp.Repository;
23
using Magic.IndexedDb;
34
using Microsoft.AspNetCore.Components;
45

@@ -9,7 +10,7 @@ public class OpenTestPage(IMagicIndexedDb magic) : TestPageBase
910
{
1011
public async Task<string> DirectOpen()
1112
{
12-
var connection = await magic.Database(IndexedDbContext.Person);
13+
var connection = await magic.Database(IndexDbContext.Employee);
1314
await connection.OpenAsync();
1415
return "OK";
1516
}

E2eTestWebApp/TestPages/SingleRecordBasicTestPage.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class SingleRecordBasicTestPage(IMagicIndexedDb magic) : TestPageBase
1313
public async Task<string> Add()
1414
{
1515
var db = await magic.Query<Person>();
16-
await db.AddAsync(new Person { Age = 20, Name = "John" });
16+
await db.AddAsync(new Person { _Age = 20, Name = "John" });
1717
var results = await db.ToListAsync();
1818

1919
return results.Count == 1 ? "OK" : "Incorrect";
@@ -22,8 +22,8 @@ public async Task<string> Add()
2222
public async Task<string> Delete()
2323
{
2424
var db = await magic.Query<Person>();
25-
await db.AddAsync(new Person { Id = 1, Age = 20, Name = "John" });
26-
await db.DeleteAsync(new Person {Id = 1, Age = 20, Name = "John" });
25+
await db.AddAsync(new Person { _Id = 1, _Age = 20, Name = "John" });
26+
await db.DeleteAsync(new Person {_Id = 1, _Age = 20, Name = "John" });
2727
var results = await db.ToListAsync();
2828

2929
return results.Count == 0 ? "OK" : "Incorrect";
@@ -32,19 +32,19 @@ public async Task<string> Delete()
3232
public async Task<string> Update()
3333
{
3434
var db = await magic.Query<Person>();
35-
await db.AddAsync(new Person { Id = 1, Age = 20, Name = "John" });
36-
await db.UpdateAsync(new Person { Id = 1, Age = 25, Name = "John" });
35+
await db.AddAsync(new Person { _Id = 1, _Age = 20, Name = "John" });
36+
await db.UpdateAsync(new Person { _Id = 1, _Age = 25, Name = "John" });
3737
var results = await db.ToListAsync();
3838

39-
return results.First().Age == 25 ? "OK" : "Incorrect";
39+
return results.First()._Age == 25 ? "OK" : "Incorrect";
4040
}
4141

4242
public async Task<string> GetAll()
4343
{
4444
var db = await magic.Query<Person>();
45-
await db.AddAsync(new Person { Age = 20, Name = "John" });
46-
await db.AddAsync(new Person { Age = 25, Name = "Peter" });
47-
await db.AddAsync(new Person { Age = 35, Name = "Bert" });
45+
await db.AddAsync(new Person { _Age = 20, Name = "John" });
46+
await db.AddAsync(new Person { _Age = 25, Name = "Peter" });
47+
await db.AddAsync(new Person { _Age = 35, Name = "Bert" });
4848
var results = await db.ToListAsync();
4949

5050
return results.Count == 3 ? "OK" : "Incorrect";

E2eTestWebApp/TestPages/WhereTestPage.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ private class Record
2121
public async Task<string> Where1()
2222
{
2323
var db = await magic.Query<Person>();
24-
await db.AddAsync(new Person { Age = 20, Name = "John" });
25-
await db.AddAsync(new Person { Age = 25, Name = "Peter" });
26-
await db.AddAsync(new Person { Age = 35, Name = "Bert" });
24+
await db.AddAsync(new Person { _Age = 20, Name = "John" });
25+
await db.AddAsync(new Person { _Age = 25, Name = "Peter" });
26+
await db.AddAsync(new Person { _Age = 35, Name = "Bert" });
2727

28-
var results = await db.Where(p => p.Age < 30).ToListAsync();
28+
var results = await db.Where(p => p._Age < 30).ToListAsync();
2929

3030
return results.Count == 2 ? "OK" : "Incorrect";
3131
}

0 commit comments

Comments
 (0)