Skip to content

Commit cba9967

Browse files
She's running, but not initiating. WIll finish soon
1 parent 72ff7fc commit cba9967

File tree

4 files changed

+68
-24
lines changed

4 files changed

+68
-24
lines changed

Magic.IndexedDb/Factories/MagicDbFactory.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,17 @@ private async ValueTask<IndexedDbManager> GetOrCreateDatabaseAsync(string dbName
8585
{
8686
if (_databases.TryGetValue(dbName, out var dbManager))
8787
return dbManager; // Return cached instance
88-
88+
/*
8989
var registeredStores = _serviceProvider.GetServices<DbStore>();
9090
var dbStore = registeredStores.FirstOrDefault(db => db.Name == dbName)
9191
?? throw new MagicException($"Database {dbName} is not registered.");
92-
92+
*/
9393
var jsModule = await GetJsModuleAsync(); // Ensure shared JS module is ready
9494

9595
// Create & Open the database (formerly in IndexedDbManager)
9696
var manager = new IndexedDbManager(jsModule);
97-
await new MagicJsInvoke(jsModule).CallJsAsync(Cache.MagicDbJsImportPath,
98-
IndexedDbFunctions.CREATE_DATABASES, cancellationToken, new TypedArgument<DbStore>(dbStore));
97+
/*await new MagicJsInvoke(jsModule).CallJsAsync(Cache.MagicDbJsImportPath,
98+
IndexedDbFunctions.CREATE_DATABASES, cancellationToken, new TypedArgument<DbStore>(dbStore));*/
9999

100100
_databases[dbName] = manager; // Cache the opened database
101101
return manager;

Magic.IndexedDb/Helpers/MagicValidator.cs

+24-5
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@ internal static class MagicValidator
1313
public static void ValidateTables()
1414
{
1515
var errors = new StringBuilder();
16-
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
17-
var magicTableClasses = assemblies
18-
.SelectMany(a => a.GetTypes())
19-
.Where(SchemaHelper.HasMagicTableInterface) // Uses the helper method
20-
.ToList();
16+
var magicTableClasses = SchemaHelper.GetAllMagicTables();
2117

2218

2319
foreach (var type in magicTableClasses)
@@ -61,6 +57,29 @@ public static void ValidateTables()
6157
errors.AppendLine($"Error: Property '{prop.Name}' in class '{type.Name}' has multiple Magic attributes ({string.Join(", ", appliedMagicAttributes.Select(a => a.Name))}). A property can have at most one Magic attribute.");
6258
}
6359
}
60+
61+
var instance = Activator.CreateInstance(type);
62+
var getCompoundKeyMethod = type.GetMethod("GetCompoundKey", BindingFlags.Public | BindingFlags.Instance);
63+
var getCompoundIndexesMethod = type.GetMethod("GetCompoundIndexes", BindingFlags.Public | BindingFlags.Instance);
64+
65+
if (getCompoundKeyMethod == null || getCompoundIndexesMethod == null)
66+
{
67+
errors.AppendLine($"Error: Class '{type.Name}' is missing required methods 'GetCompoundKey()' or 'GetCompoundIndexes()'.");
68+
}
69+
else
70+
{
71+
try
72+
{
73+
// Call both methods and force any errors to surface
74+
var compoundKey = getCompoundKeyMethod.Invoke(instance, null);
75+
var compoundIndexes = getCompoundIndexesMethod.Invoke(instance, null);
76+
}
77+
catch (TargetInvocationException tie) when (tie.InnerException != null)
78+
{
79+
// Extract and log the **actual** exception instead of the generic wrapper
80+
errors.AppendLine($"Error: Class '{type.Name}' encountered an issue when calling 'GetCompoundKey()' or 'GetCompoundIndexes()'. Exception: {tie.InnerException.Message}");
81+
}
82+
}
6483
}
6584

6685
if (errors.Length > 0)

Magic.IndexedDb/Helpers/SchemaHelper.cs

+38-13
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,35 @@ public static class SchemaHelper
1515
//private static readonly ConcurrentDictionary<string, List<StoreSchema>> _databaseSchemasCache = new();
1616
//private static bool _schemasScanned = false;
1717
//private static readonly object _lock = new();
18-
internal static readonly ConcurrentDictionary<string, Type> _schemaCache = new();
18+
internal static readonly ConcurrentDictionary<Type, IMagicTableBase?> _schemaCache = new();
1919

2020
internal static void EnsureSchemaIsCached(Type type)
2121
{
22-
string typeKey = type.FullName!;
23-
24-
_schemaCache.GetOrAdd(typeKey, _ =>
22+
_schemaCache.GetOrAdd(type, _ =>
2523
{
26-
if (!typeof(IMagicTableBase).IsAssignableFrom(type))
27-
throw new InvalidOperationException($"Type {type.Name} does not implement IMagicTableBase.");
28-
29-
return type; // Cache the type itself
24+
// Check if the type implements IMagicTableBase
25+
return typeof(IMagicTableBase).IsAssignableFrom(type)
26+
? (Activator.CreateInstance(type) as IMagicTableBase) // Store the instance if valid
27+
: null; // Store null if it doesn’t implement IMagicTableBase
3028
});
3129
}
3230

31+
32+
public static bool ImplementsIMagicTable(Type type)
33+
{
34+
return type.GetInterfaces().Any(i => i.IsGenericType
35+
&& i.GetGenericTypeDefinition() == typeof(IMagicTable<>));
36+
}
37+
38+
public static List<Type>? GetAllMagicTables()
39+
{
40+
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
41+
return assemblies
42+
.SelectMany(a => a.GetTypes())
43+
.Where(t => t.IsClass && !t.IsAbstract && SchemaHelper.ImplementsIMagicTable(t))
44+
.ToList();
45+
}
46+
3347
private static readonly ConcurrentDictionary<Type, string> _tableNameCache = new();
3448
private static readonly ConcurrentDictionary<Type, string> _databaseNameCache = new();
3549

@@ -143,16 +157,24 @@ public static StoreSchema GetStoreSchema(Type type)
143157
PropertyMappingCache.EnsureTypeIsCached(type);
144158
EnsureSchemaIsCached(type);
145159

146-
if (!_schemaCache.TryGetValue(type.FullName!, out var cachedType))
147-
throw new InvalidOperationException($"Type {type.Name} is not cached and does not implement IMagicTableBase.");
160+
// Retrieve the cached entry
161+
if (!_schemaCache.TryGetValue(type, out var instance))
162+
{
163+
throw new InvalidOperationException($"Type {type.Name} is not cached.");
164+
}
148165

149-
var instance = Activator.CreateInstance(cachedType) as IMagicTableBase;
166+
// Ensure the type actually implements IMagicTableBase
150167
if (instance == null)
151-
throw new InvalidOperationException($"Failed to create an instance of {cachedType.Name}.");
168+
{
169+
throw new InvalidOperationException($"Type {type.Name} does not implement IMagicTableBase and cannot be used as a Magic Table.");
170+
}
152171

172+
// Retrieve table name
153173
var tableName = instance.GetTableName();
154174
if (string.IsNullOrWhiteSpace(tableName))
155-
throw new InvalidOperationException($"Type {cachedType.Name} returned an invalid table name.");
175+
{
176+
throw new InvalidOperationException($"Type {type.Name} returned an invalid table name.");
177+
}
156178

157179
var schema = new StoreSchema
158180
{
@@ -165,7 +187,9 @@ public static StoreSchema GetStoreSchema(Type type)
165187
.FirstOrDefault(prop => PropertyMappingCache.GetPropertyEntry(prop, type).PrimaryKey);
166188

167189
if (primaryKeyProperty == null)
190+
{
168191
throw new InvalidOperationException($"The entity {type.Name} does not have a primary key attribute.");
192+
}
169193

170194
schema.PrimaryKey = PropertyMappingCache.GetJsPropertyName(primaryKeyProperty, type);
171195

@@ -183,5 +207,6 @@ public static StoreSchema GetStoreSchema(Type type)
183207

184208
return schema;
185209
}
210+
186211
}
187212
}

TestWasm/Models/Person.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class Nested
1212
}
1313

1414

15-
public class Person : MagicTableTool<Person>, IMagicTable<DbSets>
15+
public class Person : MagicTableTool<Person>, IMagicTable<Person.DbSets>
1616
{
1717
public List<IMagicCompoundIndex> GetCompoundIndexes() =>
1818
new List<IMagicCompoundIndex>() {
@@ -21,7 +21,7 @@ public List<IMagicCompoundIndex> GetCompoundIndexes() =>
2121

2222
//public IMagicCompoundIndex? GetCompoundKey() => null;
2323
public IMagicCompoundKey? GetCompoundKey() =>
24-
CreateCompoundKey(x => x._Id, x => x.GUIY);
24+
CreateCompoundKey(x => x.Name, x => x.GUIY);
2525

2626
public string GetTableName() => "Person";
2727
public IndexedDbSet GetDefaultDatabase() => IndexDbContext.Client;

0 commit comments

Comments
 (0)