-
Notifications
You must be signed in to change notification settings - Fork 10
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
New Schema Refactor (Dev Migrations Required) #60
Comments
I believe for a static database whose schema won't change at runtime, there is no reason to reject these changes.
And are there any specific examples about the dynamic runtime tables? Will they use another attribute other than the |
Yes that's more the idea I was thinkin @yueyinqiu Also I was considering some more radical changes as well potentially. So consider this scenario: _MagicDb.Where<Person>(x => x.Name == "John").Where<Person>(x => x.Age <= 30 && x.Age >= 18).Skip(4); Now when I created this library, I wanted to more replicate repository LINQ to SQL because I like it more, plus the normal EF Core syntax is harder to replicate in a non relational database. But That syntax I showed you, it can happen, especially in deferred execution scenarios. So multiple where statements are not uncommon. But what if you wanted to start with a skip? Where do you put that? The answer is you can't. Additionally what if those where statements type of T weren't identical? What would happen? I am not sure and I'm sure that'd cause failure but you shouldn't be allowed to do that in the first place. Plus it's kind of annoying I had to write it like that over and over anyways. Especially for complex deferred execution scenarios. So I'm thinking of removing " As there should instead be something like, " _MagicDb.Repository<Person>().OrderBy(x => x.Id)
.Where(x => x.Name == "John")
.Where(x => x.Age <= 30 && x.Age >= 18).Skip(4); Or you could do: var personRepository = _MagicDb.Repository<Person>().OrderBy(x => x.Id);
personRepository.Where(x => x.Name == "John");
personRepository.Where(x => x.Age <= 30 && x.Age >= 18).Skip(4);
List<Person> personList = await personRepository.ToListAsync() Anyways hopefully this makes sense. But this shows more ways to break it apart by using repositories and more control as I could put an OrderBy, or Skip, or Take, or other attached to the repository instead of being forced to attach it to a Where statement first. I also think this is just a lot cleaner. Major down side is that this will require each project to refactor. Though not the hardest refactor in the world. But I could also obsolete the current Where, add the repository, and just let people migrate over if that's a concern and not just instantly remove the code. On the other hand, all of my ideas would require a refactor so why not just make everyone refactor all at once? Hmm. |
What about _MagicDb.Query<Person>().Where(...
// for dynamically created ones:
_MagicDb.Query<Person>("database", "store").Where(... |
@yueyinqiu That... I really like that. You're absolutely right. In our case, we don’t truly have a "repository" in the traditional sense. The idea of But I was actually just about to start implementing some of this, so this suggestion came at the perfect time. Great call! I'll go ahead and move forward with |
@yueyinqiu One approach I’m considering is introducing a specialized class ( Caching dynamic objects doesn’t make sense—what if their structure changes at runtime? The existing caching system is built for strictly-typed, immutable objects. I also realized that if the serializer encounters With this in mind, dynamic runtime tables in Magic.IndexDB would need new query initiators. Here’s how I see it working: Query Initiator Overloads1. Fully Dynamic Query (No Type)_MagicDb.Query("database", "store") // Returns type of object, relies on runtime definitions This allows full flexibility without requiring a predefined model. 2. Explicit Type, No MagicTable Attribute_MagicDb.Query<Person>("database", "store")
I could also introduce considerations as well to use expression array parameters for you to define multiple properties as indexed, or not mapped, or different property names in IndexDB, or more. Thus allowing strict classes that don't have attributes to be flexible in our system. 3. Standard Typed Query (MagicTable Enabled)_MagicDb.Query<Person>()
Key Takeaways
This approach would enable both dynamic flexibility and strict type safety while preventing common pitfalls. Thoughts? |
Oh I didn't even consider such complicated scenarios. And I'd like to suggest another case between the overload 2 and 3, that is, the type has no |
Yup haha, that's why when you said we should support dynamic object I broke out sweating a bit haha! But I think my proposed resolution resolves this perfectly. But I also had wayyy too much fun today building some pretty crazy stuff. I've been working too hard recently so I decided to chill and have fun with this project. the |
Overview
This proposal introduces a significant refactor to the Magic.IndexDB library, focusing on simplifying database schema management, improving ease of use, and removing unnecessary manual setup. The primary change involves eliminating the need for explicit database initialization by leveraging attribute-based metadata, thus reducing developer overhead and improving usability.
Current System
Currently, Magic.IndexDB requires developers to explicitly register and manage database connections using the
MagicDbFactory
. The following methods are used to open and interact with databases:Developers then use the
IndexedDbManager
to query and manipulate records. Example usage:This approach requires developers to explicitly open databases and associate models with the correct database manager. However, this information is already available in model attributes:
Where the
MagicTable
attribute includes:Since the system already knows which database a model belongs to, requiring manual database initialization is redundant and adds unnecessary complexity.
Proposed Changes
1. Automatic Database Initialization on First Use
Instead of requiring explicit database registration, databases will automatically open when a model is accessed. This eliminates the need for developers to manually manage database connections.
2. Removing Explicit Database Manager Assignment
Currently, multiple managers must be instantiated for different databases, and developers must remember which manager corresponds to which models. This proposal eliminates that requirement, as the system can infer database assignments from attributes.
3. Simplified Database Closing Mechanism
While databases will open automatically, developers should retain control over closing them. This proposal introduces new methods:
This provides a streamlined approach to resource management while removing unnecessary manual steps.
4. Refactoring Startup Configuration
Currently, database registration in
Program.cs
looks like this:The proposed refactor eliminates
options.Name
andoptions.StoreSchemas
, as this information is redundant given the attribute-based schema system. The new approach would be:The system will infer database names and schemas at runtime.
5. Removing
DbStore.StoreSchemas
Previously,
DbStore
required explicit schema definitions:This proposal removes
StoreSchemas
entirely, as Magic.IndexDB can determine schema details automatically.6. Introducing Dynamic Runtime Table & Database Creation
To allow for dynamic, runtime-created tables and databases, a special runtime model will be introduced. This model will allow developers to:
This ensures that even runtime-created databases maintain the benefits of strong typing and schema validation.
Summary of Benefits
Migration Guide
Changes to Database Initialization
Before:
After:
Changes to Closing Databases
Before:
After:
Changes to Startup Configuration
Before:
After:
Conclusion
This refactor simplifies database usage, eliminates redundant setup, and improves developer experience by making database management seamless. While a migration is required, it is a one-time change that provides long-term benefits by reducing boilerplate and enforcing best practices.
Feedback from the community is encouraged before implementation!
The text was updated successfully, but these errors were encountered: