Feat: Allow Pre-save hooks on the create operations#27
Merged
Conversation
1- Add beforeCreate hook to CreateMany and CreateManyAndReturn wrappers to allow mutation on the struct before it's serialized to SQL rows
2- Add ToRowMap() on {Model}Create as the single exit point from the struct
representation, with per field nil checks, default injection, and CUID/UUID generation (if present)
3- establish a flow of create api takes the FieldAssignment spread and generates a struct via assignmentsTo{Model}Create, and the pre-save hook mutates that struct if preset, then, the base struct is converted to to a map via ToRowMap() that's used to generate the SQL rows in create operations
4- Remove RecordsToRowMaps generator function because ToRowMap() now owns the
struct to map conversion with proper defaults, eliminating the duplicated
default/CUID logic between RecordsToRowMaps and the single-create path
5- Remove Assignments() on {Model}Create because ToRowMap() replaces it as the single back-conversion path structs no longer round-trip through Field assignment
6- Remove validateFn, rowMapFn, and singleCreateFn params from executeCreateMany/AndReturn
since validation is handled at FieldAssignment level in the wrapper and map conversion is inline via ToRowMap(), the fallback per-row path uses mapToColsVals + executeInsert instead of re-entering singleCreateFn (which would double-fire hooks, and it did)
…ct specific which caused Postgres to fail in the workflow run
1- add the if {model}.afterCreate check to the CreateManyAndReturn in model_create template
2- update TestCreateMany_Hooks in create_many_tests to test the afterCreate hook on bulk inserts (CreateManyAndReturn for now)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
1- Add beforeCreate hook to CreateMany and CreateManyAndReturn wrappers to allow mutation on the struct before it's serialized to SQL rows
2- Add ToRowMap() on {Model}Create as the single exit point from the struct
representation, with per field nil checks, default injection, and CUID/UUID generation (if present)
3- establish a flow of create api takes the FieldAssignment spread and generates a struct via assignmentsTo{Model}Create, and the pre-save hook mutates that struct if preset, then, the base struct is converted to to a map via ToRowMap() that's used to generate the SQL rows in create operations
4- Remove RecordsToRowMaps generator function because ToRowMap() now owns the
struct to map conversion with proper defaults, eliminating the duplicated
default/CUID logic between RecordsToRowMaps and the single-create path
5- Remove Assignments() on {Model}Create because ToRowMap() replaces it as the single back-conversion path structs no longer round-trip through Field assignment
6- Remove validateFn, rowMapFn, and singleCreateFn params from executeCreateMany/AndReturn
since validation is handled at FieldAssignment level in the wrapper and map conversion is inline via ToRowMap(), the fallback per-row path uses mapToColsVals + executeInsert instead of re-entering singleCreateFn (which would double-fire hooks, and it did)