feat: add tagged template for executing raw SQL queries #10108
Closed
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.
I'm aware that this PR won't be accepted until I add the appropriate tests and documentation. I first want to make sure that this feature is acceptable to the core maintainers.
My apologies for not writing a separate issue for that. If you believe sql tags should not exist in TypeORM, I will understand. Although, it would be harder for other tools (such as SafeQL) to integrate with TypeORM.
Description of change
Adds a new tagged template to classes that execute raw SQL queries and return raw database results. The template takes a template string and an array of values as input and builds a SQL query using the appropriate parameter strategy based on
While SQL tags can be misleading to some people regarding SQL injections, they are, in fact, safer than splitting the query from the variables. With tagged template, you simply can't go wrong with accidently injecting unsafe values from variables due to the way of how tagged templates work.
As a plus, reading the query with the variables in place adds more clarity rather than trying to figure out what is the $23 parameter.
The new
sqltag will split the templates from the expressions, parameterize the statement, and then map the statement and the variables back to thequerymethod. (tag -> { query, variables } -> query method).Pull-Request Checklist
masterbranchnpm run formatto apply prettier formattingnpm run testpasses with this changeFixes #0000