Expose Preload's typeConverter #591
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hello there!
This PR exposes a
TypeConverterI found in the source code, currently only used by Preloaders to handleNULLresults returned by LEFT JOINS. I figured it can be very useful to reuse it for custom queries, so I propose to expose it as part of Bob's API.Let me know if you think this makes sense: I can help in writing tests o tweaking the PR to make it more likely to be merged.
Use case
Since a while I started using Bob in a few fairly complex projects, and I now find myself building overcomplicated queries that can't really be expressed with just the ORM functions, but need to be built with Bob's query builder to handle all the joins, and CTEs, and subqueries, etc.
The query builder itself works quite well for my use case, but more often than not I want to reuse structs and constants generated by Bob instead of using custom structs or hardcoding strings:
I usually do it with the following approach:
This approach works well but breaks down as soon as the LeftJoin in the example doesn't match any row, causing NULLs to be returned and scanned:
Using a
ThingSetteras a result target almost works but of course it loses all the generated fields... But it turns out the
ormpackage was hiding aTypeConverterapparently perfect for my use case, where I just wantNULLrelationships to be expressed by empty structs instead of breaking down:Which results in a
myRow{}struct with a zeroAThingfield, just like I wanted