Skip to content

feat: use bind params in bulkInsertQuery - #17131

Closed
Americas wants to merge 0 commit into
sequelize:mainfrom
Americas:main
Closed

Americas wants to merge 0 commit into
sequelize:mainfrom
Americas:main

Conversation

@Americas

@Americas Americas commented Feb 28, 2024 •

Copy link
Copy Markdown
Contributor

Description of Changes

When using bulk create, the generated SQL does not use bindings. This creates an issue, since the data is logged along with the running query.

With this PR, the default generated SQL for a bulk insert will use bindings by default, unless bindParams is false.

This is a followup on #17128, I still hope we can get that one on the v6 branch, since it will still be a while for v7 to be usable. @ephys

List of Breaking Changes

  • internal breaking change, abstract QueryGenerator.bulkInsertQuery() now returns an object with 2 properties: query (always returned) and bind (only if bind params are used). This follows the same pattern that is used in other methods from this class, like InsertQuery.

@Americas
Americas force-pushed the main branch 7 times, most recently from a98e83c to 75c9cd0 Compare February 28, 2024 10:57
@Americas
Americas marked this pull request as ready for review February 28, 2024 11:14
@Americas
Americas requested a review from a team as a code owner February 28, 2024 11:14
@Americas
Americas requested review from WikiRik and ephys February 28, 2024 11:14
@Americas
Americas force-pushed the main branch 7 times, most recently from 3771ada to 3c5194c Compare March 5, 2024 08:25
@Americas
Americas force-pushed the main branch 5 times, most recently from a2dc36d to 5c09897 Compare March 12, 2024 14:31

@ephys ephys left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small change request but otherwise this looks good :)

Thank you for implementing this in v7


type BulkInsertOptions = ParameterOptions & {
hasTrigger?: boolean;
bindParam?: false | ((value: unknown) => string);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bindParam is a legacy option. Could you add support for parameterStyle instead?

enum ParameterStyle {
  bind = 'bind',
  replacement = 'replacement',
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Judging from the comment on my previous PR, I hope I did it right, but please explain if I did not get it right.

columnDefinitions?: { [columnName: string]: NormalizedAttributeOptions },
options?: InsertOptions,
): { query: string; bind?: unknown[] };
): { query: string; bind?: Record<string, unknown> };

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you move { query, bind } to its own reusable type?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@Americas
Americas force-pushed the main branch 6 times, most recently from 7693d58 to c951e6e Compare March 19, 2024 09:56
@ephys
ephys self-requested a review March 19, 2024 10:43
@Americas
Americas force-pushed the main branch 5 times, most recently from b580d53 to 1b7781d Compare March 25, 2024 11:54
@lohart13

Copy link
Copy Markdown
Contributor

I have a question on this, all the dialects have varying numbers of allowed parameters with MSSQL having the least at 2100.
Are we just relying on the user to split up their calls to bulkInsert so that they are within the dialects supported number of allowed parameters?

The only reason for asking is that I initially was going to include this change in PR #16988 however I opted not to due to this potential issue.

@Americas

Copy link
Copy Markdown
Contributor Author

While I do understand this problem has a higher chance of happening in a bulk insert, the same is true in any other query that uses bind params. It certainly seems important to deal with this, or at least make sure the error is understandable.

To note, this PR does not add bind params to mssql, both mysql and postgres seem to handle around 65k, not sure how likely it is to hit that.

This PR also still allows to use replacement instead of bind. If this is truly a problem, I can make the default behavior be replacement instead of bind.

@ephys

ephys commented Mar 26, 2024

Copy link
Copy Markdown
Member

Note that there will eventually be a global option to configure the defaults, but the exact API is still TBD, so even if we pick replacements by default, you could configure your Sequelize instance to use bind for this query

@Americas
Americas force-pushed the main branch 2 times, most recently from 7ecdf3e to 312b49f Compare March 27, 2024 08:09
@sequelize-bot sequelize-bot Bot added the conflicted This PR has merge conflicts and will not be present in the list of PRs to review label Apr 10, 2024
@lohart13 lohart13 mentioned this pull request Oct 17, 2024
4 of 5 tasks
@Americas Americas closed this Nov 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

conflicted This PR has merge conflicts and will not be present in the list of PRs to review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants