Skip to content

Commit dff16eb

Browse files
committed
Chore: change AfterCreate hook signature to expose a slice of model instead of a single model, so it fires once per batch, not per creation, in the bulk inserts
1- change afterCreate signature from func(ctx, *Model) to func(ctx, []*Model) so single create wraps the result in a 1-element slice and CreateManyAndReturn passes the full results array, firing the hook once per batch instead of per-record
1 parent 255ad20 commit dff16eb

11 files changed

Lines changed: 51 additions & 60 deletions

File tree

‎generator/templates/model_create.gotpl‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ func (q *Queries) execute{{ .Model.Name }}Create(ctx context.Context, assignment
316316
}
317317

318318
if q.{{ .Model.Name }}.afterCreate != nil {
319-
if err := q.{{ .Model.Name }}.afterCreate(ctx, res); err != nil {
319+
if err := q.{{ .Model.Name }}.afterCreate(ctx, []*{{ .Model.Name }}{res}); err != nil {
320320
return nil, err
321321
}
322322
}
@@ -394,10 +394,8 @@ func (q *Queries) execute{{ .Model.Name }}CreateManyAndReturn(ctx context.Contex
394394
return nil, err
395395
}
396396
if q.{{ .Model.Name }}.afterCreate != nil {
397-
for _, r := range results {
398-
if err := q.{{ .Model.Name }}.afterCreate(ctx, r); err != nil {
399-
return nil, err
400-
}
397+
if err := q.{{ .Model.Name }}.afterCreate(ctx, results); err != nil {
398+
return nil, err
401399
}
402400
}
403401
return results, nil

‎generator/templates/model_structs.gotpl‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ type {{ .Model.Name }}Omit struct {
3838
type {{ .Model.Name }}Delegate struct {
3939
client *Queries
4040
beforeCreate func(context.Context, *{{ .Model.Name }}Create) error
41-
afterCreate func(context.Context, *{{ .Model.Name }}) error
41+
afterCreate func(context.Context, []*{{ .Model.Name }}) error
4242
afterCreateMany func(context.Context, []{{ .Model.Name }}Create, int64) error
4343
}
4444

4545
func (d *{{ .Model.Name }}Delegate) BeforeCreate(hook func(context.Context, *{{ .Model.Name }}Create) error) {
4646
d.beforeCreate = hook
4747
}
4848

49-
func (d *{{ .Model.Name }}Delegate) AfterCreate(hook func(context.Context, *{{ .Model.Name }}) error) {
49+
func (d *{{ .Model.Name }}Delegate) AfterCreate(hook func(context.Context, []*{{ .Model.Name }}) error) {
5050
d.afterCreate = hook
5151
}
5252

‎integration/create_many_test.go‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,11 @@ func TestCreateMany_Hooks(t *testing.T) {
109109

110110
var afterCalled bool
111111
var gotRole valk.UserRoleType
112-
client.User.AfterCreate(func(ctx context.Context, user *valk.User) error {
112+
client.User.AfterCreate(func(ctx context.Context, users []*valk.User) error {
113113
afterCalled = true
114-
gotRole = user.Role
114+
if len(users) > 0 {
115+
gotRole = users[0].Role
116+
}
115117
return nil
116118
})
117119

@@ -137,7 +139,7 @@ func TestCreateMany_Hooks(t *testing.T) {
137139
client, cleanup := setupTestDB(t)
138140
defer cleanup()
139141

140-
client.User.AfterCreate(func(ctx context.Context, user *valk.User) error {
142+
client.User.AfterCreate(func(ctx context.Context, users []*valk.User) error {
141143
return fmt.Errorf("after hook rejected")
142144
})
143145

‎integration/main.go‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,18 @@ func seed(db *valk.DB, ctx context.Context) *SeedData {
3232
return nil
3333
})
3434

35-
db.User.AfterCreateMany(func(ctx context.Context, users []valk.UserCreate, count int64) error {
35+
db.User.AfterCreateMany(func(ctx context.Context, usersInput []valk.UserCreate, count int64) error {
3636
var emails []string
37-
for _, u := range users {
37+
for _, u := range usersInput {
3838
emails = append(emails, u.Email)
3939
}
4040
fmt.Printf("AfterCreateMany: %d users created (emails: %v)\n", count, emails)
4141
return nil
4242
})
43-
44-
db.User.AfterCreate(func(ctx context.Context, user *valk.User) error {
45-
fmt.Printf("AfterCreate: user %s (role=%s)\n", user.Email, user.Role)
43+
db.User.AfterCreate(func(ctx context.Context, users []*valk.User) error {
44+
for _, u := range users {
45+
fmt.Printf("AfterCreate: user %s (role=%s)\n", u.Email, u.Role)
46+
}
4647
return nil
4748
})
4849

‎integration/validation_test.go‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,9 +459,11 @@ func TestCreate_Hooks(t *testing.T) {
459459
})
460460

461461
var afterCalled bool
462-
db.User.AfterCreate(func(ctx context.Context, u *valk.User) error {
463-
if u.Email == "hook@example.com" {
464-
afterCalled = true
462+
db.User.AfterCreate(func(ctx context.Context, users []*valk.User) error {
463+
for _, u := range users {
464+
if u.Email == "hook@example.com" {
465+
afterCalled = true
466+
}
465467
}
466468
return nil
467469
})

‎integration/valk/category.go‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ type CategoryOmit struct {
3838
type CategoryDelegate struct {
3939
client *Queries
4040
beforeCreate func(context.Context, *CategoryCreate) error
41-
afterCreate func(context.Context, *Category) error
41+
afterCreate func(context.Context, []*Category) error
4242
afterCreateMany func(context.Context, []CategoryCreate, int64) error
4343
}
4444

4545
func (d *CategoryDelegate) BeforeCreate(hook func(context.Context, *CategoryCreate) error) {
4646
d.beforeCreate = hook
4747
}
4848

49-
func (d *CategoryDelegate) AfterCreate(hook func(context.Context, *Category) error) {
49+
func (d *CategoryDelegate) AfterCreate(hook func(context.Context, []*Category) error) {
5050
d.afterCreate = hook
5151
}
5252

@@ -224,7 +224,7 @@ func (q *Queries) executeCategoryCreate(ctx context.Context, assignments []Field
224224
}
225225

226226
if q.Category.afterCreate != nil {
227-
if err := q.Category.afterCreate(ctx, res); err != nil {
227+
if err := q.Category.afterCreate(ctx, []*Category{res}); err != nil {
228228
return nil, err
229229
}
230230
}
@@ -302,10 +302,8 @@ func (q *Queries) executeCategoryCreateManyAndReturn(ctx context.Context, record
302302
return nil, err
303303
}
304304
if q.Category.afterCreate != nil {
305-
for _, r := range results {
306-
if err := q.Category.afterCreate(ctx, r); err != nil {
307-
return nil, err
308-
}
305+
if err := q.Category.afterCreate(ctx, results); err != nil {
306+
return nil, err
309307
}
310308
}
311309
return results, nil

‎integration/valk/categoryToPost.go‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ type CategoryToPostOmit struct {
4141
type CategoryToPostDelegate struct {
4242
client *Queries
4343
beforeCreate func(context.Context, *CategoryToPostCreate) error
44-
afterCreate func(context.Context, *CategoryToPost) error
44+
afterCreate func(context.Context, []*CategoryToPost) error
4545
afterCreateMany func(context.Context, []CategoryToPostCreate, int64) error
4646
}
4747

4848
func (d *CategoryToPostDelegate) BeforeCreate(hook func(context.Context, *CategoryToPostCreate) error) {
4949
d.beforeCreate = hook
5050
}
5151

52-
func (d *CategoryToPostDelegate) AfterCreate(hook func(context.Context, *CategoryToPost) error) {
52+
func (d *CategoryToPostDelegate) AfterCreate(hook func(context.Context, []*CategoryToPost) error) {
5353
d.afterCreate = hook
5454
}
5555

@@ -223,7 +223,7 @@ func (q *Queries) executeCategoryToPostCreate(ctx context.Context, assignments [
223223
}
224224

225225
if q.CategoryToPost.afterCreate != nil {
226-
if err := q.CategoryToPost.afterCreate(ctx, res); err != nil {
226+
if err := q.CategoryToPost.afterCreate(ctx, []*CategoryToPost{res}); err != nil {
227227
return nil, err
228228
}
229229
}
@@ -301,10 +301,8 @@ func (q *Queries) executeCategoryToPostCreateManyAndReturn(ctx context.Context,
301301
return nil, err
302302
}
303303
if q.CategoryToPost.afterCreate != nil {
304-
for _, r := range results {
305-
if err := q.CategoryToPost.afterCreate(ctx, r); err != nil {
306-
return nil, err
307-
}
304+
if err := q.CategoryToPost.afterCreate(ctx, results); err != nil {
305+
return nil, err
308306
}
309307
}
310308
return results, nil

‎integration/valk/comment.go‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ type CommentOmit struct {
6666
type CommentDelegate struct {
6767
client *Queries
6868
beforeCreate func(context.Context, *CommentCreate) error
69-
afterCreate func(context.Context, *Comment) error
69+
afterCreate func(context.Context, []*Comment) error
7070
afterCreateMany func(context.Context, []CommentCreate, int64) error
7171
}
7272

7373
func (d *CommentDelegate) BeforeCreate(hook func(context.Context, *CommentCreate) error) {
7474
d.beforeCreate = hook
7575
}
7676

77-
func (d *CommentDelegate) AfterCreate(hook func(context.Context, *Comment) error) {
77+
func (d *CommentDelegate) AfterCreate(hook func(context.Context, []*Comment) error) {
7878
d.afterCreate = hook
7979
}
8080

@@ -389,7 +389,7 @@ func (q *Queries) executeCommentCreate(ctx context.Context, assignments []FieldA
389389
}
390390

391391
if q.Comment.afterCreate != nil {
392-
if err := q.Comment.afterCreate(ctx, res); err != nil {
392+
if err := q.Comment.afterCreate(ctx, []*Comment{res}); err != nil {
393393
return nil, err
394394
}
395395
}
@@ -467,10 +467,8 @@ func (q *Queries) executeCommentCreateManyAndReturn(ctx context.Context, records
467467
return nil, err
468468
}
469469
if q.Comment.afterCreate != nil {
470-
for _, r := range results {
471-
if err := q.Comment.afterCreate(ctx, r); err != nil {
472-
return nil, err
473-
}
470+
if err := q.Comment.afterCreate(ctx, results); err != nil {
471+
return nil, err
474472
}
475473
}
476474
return results, nil

‎integration/valk/post.go‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ type PostOmit struct {
5656
type PostDelegate struct {
5757
client *Queries
5858
beforeCreate func(context.Context, *PostCreate) error
59-
afterCreate func(context.Context, *Post) error
59+
afterCreate func(context.Context, []*Post) error
6060
afterCreateMany func(context.Context, []PostCreate, int64) error
6161
}
6262

6363
func (d *PostDelegate) BeforeCreate(hook func(context.Context, *PostCreate) error) {
6464
d.beforeCreate = hook
6565
}
6666

67-
func (d *PostDelegate) AfterCreate(hook func(context.Context, *Post) error) {
67+
func (d *PostDelegate) AfterCreate(hook func(context.Context, []*Post) error) {
6868
d.afterCreate = hook
6969
}
7070

@@ -316,7 +316,7 @@ func (q *Queries) executePostCreate(ctx context.Context, assignments []FieldAssi
316316
}
317317

318318
if q.Post.afterCreate != nil {
319-
if err := q.Post.afterCreate(ctx, res); err != nil {
319+
if err := q.Post.afterCreate(ctx, []*Post{res}); err != nil {
320320
return nil, err
321321
}
322322
}
@@ -394,10 +394,8 @@ func (q *Queries) executePostCreateManyAndReturn(ctx context.Context, records []
394394
return nil, err
395395
}
396396
if q.Post.afterCreate != nil {
397-
for _, r := range results {
398-
if err := q.Post.afterCreate(ctx, r); err != nil {
399-
return nil, err
400-
}
397+
if err := q.Post.afterCreate(ctx, results); err != nil {
398+
return nil, err
401399
}
402400
}
403401
return results, nil

‎integration/valk/profile.go‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ type ProfileOmit struct {
4242
type ProfileDelegate struct {
4343
client *Queries
4444
beforeCreate func(context.Context, *ProfileCreate) error
45-
afterCreate func(context.Context, *Profile) error
45+
afterCreate func(context.Context, []*Profile) error
4646
afterCreateMany func(context.Context, []ProfileCreate, int64) error
4747
}
4848

4949
func (d *ProfileDelegate) BeforeCreate(hook func(context.Context, *ProfileCreate) error) {
5050
d.beforeCreate = hook
5151
}
5252

53-
func (d *ProfileDelegate) AfterCreate(hook func(context.Context, *Profile) error) {
53+
func (d *ProfileDelegate) AfterCreate(hook func(context.Context, []*Profile) error) {
5454
d.afterCreate = hook
5555
}
5656

@@ -258,7 +258,7 @@ func (q *Queries) executeProfileCreate(ctx context.Context, assignments []FieldA
258258
}
259259

260260
if q.Profile.afterCreate != nil {
261-
if err := q.Profile.afterCreate(ctx, res); err != nil {
261+
if err := q.Profile.afterCreate(ctx, []*Profile{res}); err != nil {
262262
return nil, err
263263
}
264264
}
@@ -336,10 +336,8 @@ func (q *Queries) executeProfileCreateManyAndReturn(ctx context.Context, records
336336
return nil, err
337337
}
338338
if q.Profile.afterCreate != nil {
339-
for _, r := range results {
340-
if err := q.Profile.afterCreate(ctx, r); err != nil {
341-
return nil, err
342-
}
339+
if err := q.Profile.afterCreate(ctx, results); err != nil {
340+
return nil, err
343341
}
344342
}
345343
return results, nil

0 commit comments

Comments
 (0)