Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add ability to use SqlString.raw() within the SqlString.escapeId() method #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
unreleased
==========

* Add `.toSqlString()` escapeId overriding

2.3.3 / 2022-03-06
==================

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ console.log(sql); // SELECT `username`, `email` FROM `users` WHERE id = 1
```
**Please note that this last character sequence is experimental and syntax might change**

To skip escaping one or more of the columns names that you pass to `SqlString.escapeId()`
you may use `SqlString.raw()` similarly to how it is used with `SqlString.escape()`.
See above for more details.

When you pass an Object to `.escape()` or `.format()`, `.escapeId()` is used to avoid SQL injection in object keys.

### Formatting queries
Expand Down
2 changes: 2 additions & 0 deletions lib/SqlString.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ SqlString.escapeId = function escapeId(val, forbidQualified) {
}

return sql;
} else if (typeof val.toSqlString === 'function') {
return String(val.toSqlString());
} else if (forbidQualified) {
return '`' + String(val).replace(ID_GLOBAL_REGEXP, '``') + '`';
} else {
Expand Down
8 changes: 8 additions & 0 deletions test/unit/test-SqlString.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ test('SqlString.escapeId', {

'nested arrays are flattened': function() {
assert.equal(SqlString.escapeId(['a', ['b', ['t.c']]]), '`a`, `b`, `t`.`c`');
},

'raw not escaped': function () {
assert.equal(SqlString.escapeId(SqlString.raw('*')), '*');
},

'raw within array not escaped': function () {
assert.equal(SqlString.escapeId(['a', SqlString.raw('*'), 'b']), '`a`, *, `b`');
}
});

Expand Down