Skip to content

Commit

Permalink
perf: bulk insert to decrease db overhead
Browse files Browse the repository at this point in the history
  • Loading branch information
ayaanqui committed Apr 28, 2024
1 parent e9d5aae commit 7e9eb0f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
2 changes: 1 addition & 1 deletion graph/resolver/location.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 20 additions & 17 deletions services/location_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,33 +137,36 @@ func (service Service) BulkCreateLocationImages(
user *gmodel.User,
image_inputs []*gmodel.CreateLocationImage,
) ([]*gmodel.LocationImage, error) {
inserted_images := make([]*gmodel.LocationImage, len(image_inputs))
inserted_images := make([]model.LocationImage, len(image_inputs))
for i, image_input := range image_inputs {
// TODO: validate image content type
insert_qb := table.LocationImage.INSERT(
table.LocationImage.LocationID,
table.LocationImage.Default,
table.LocationImage.UploadID,
table.LocationImage.OriginalFilename,
table.LocationImage.Caption,
table.LocationImage.CreatedBy,
table.LocationImage.UpdatedBy,
).MODEL(model.LocationImage{
inserted_images[i] = model.LocationImage{
LocationID: location_id,
Default: image_input.Default,
UploadID: uuid.New().String(),
OriginalFilename: image_input.File.Filename,
Caption: image_input.Caption,
CreatedBy: &user.ID,
UpdatedBy: &user.ID,
}).RETURNING(table.LocationImage.AllColumns)
var image gmodel.LocationImage
if err := insert_qb.QueryContext(ctx, service.DbOrTxQueryable(), &image); err != nil {
return nil, err
}
inserted_images[i] = &image
}
return inserted_images, nil

insert_qb := table.LocationImage.
INSERT(
table.LocationImage.LocationID,
table.LocationImage.Default,
table.LocationImage.UploadID,
table.LocationImage.OriginalFilename,
table.LocationImage.Caption,
table.LocationImage.CreatedBy,
table.LocationImage.UpdatedBy,
).
MODELS(inserted_images).
RETURNING(table.LocationImage.AllColumns)
var location_images []*gmodel.LocationImage
if err := insert_qb.QueryContext(ctx, service.DbOrTxQueryable(), &location_images); err != nil {
return nil, err
}
return location_images, nil
}

func (service Service) CreateLocationSchedule(
Expand Down

0 comments on commit 7e9eb0f

Please sign in to comment.