-
Notifications
You must be signed in to change notification settings - Fork 82
Add Relationship Count Loading Support + MySQL Insert Loader Support #598
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
base: main
Are you sure you want to change the base?
Conversation
|
First of all, I must say a big thank you for such a well thought out and high quality contribution.
To do this, test templates should be included too, which will generate test code, and these tests are run when testing generation. |
Do you have an example of where this has been done? |
|
See |
Summary
This PR adds a new
countsplugin that generates code for efficiently loading relationship counts, and allows MySQL insert queries to properly execute loaders.Features
New Counts Plugin
Adds the ability to load counts of to-many relationships without fetching the full related data. This is useful for displaying "X has 5 posts" without loading all posts.
PreloadCount - Loads counts via correlated subquery in a single query:
ThenLoadCount - Loads counts via separate query after main query:
InsertThenLoadCount - Loads counts after insert:
The InsertThenLoadCount could easily be expanded to UpdateThenLoadCount as well if we want to. I didn't generate it since MySQL update queries doesn't support returning (.One() or All()). However I can make that change as well if want to.
Models now include a
Cstruct with*int64pointers for each to-many relationship count.MySQL Insert Loader Support
Added support
InsertThenLoadandInsertThenLoadCountfor MySQL. Previously the issue was thatbob.ExecininsertAllwhere running the loaders causing issues withnil. Now we save and clear loaders before executing the INSERT, then transfer them to the SELECT query that retrieves the inserted row.Changes
gen/plugins/counts.gogen/templates/counts/gen/templates.go,gen/plugins/plugins.go- Register counts plugingen/templates/models/table/001_types.go.tpl- AddCstruct blockdialect/mysql/table.go- Properly handle loaders in insert flowConfiguration
Breaking Changes
None. The
Cstruct is added to models but doesn't affect existing functionality.I had difficulties figuring out how the generated queries are tested against a DB? Can you point me in the right direction for this, since I would like to include some actual query tests.