Skip to content

Feat: Allow Pre-save hooks on the create operations#27

Merged
VoidClancy merged 4 commits into
masterfrom
codegen
Jul 9, 2026
Merged

Feat: Allow Pre-save hooks on the create operations#27
VoidClancy merged 4 commits into
masterfrom
codegen

Conversation

@VoidClancy

Copy link
Copy Markdown
Owner

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)

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)
@VoidClancy VoidClancy self-assigned this Jul 9, 2026
…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)
@VoidClancy VoidClancy merged commit 2e44924 into master Jul 9, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant