Skip to content

Commit acc02c4

Browse files
committed
lint: sync rules with mysqljs/mysql
1 parent 14e4c05 commit acc02c4

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

.eslintrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@
3333
"no-regex-spaces": 2,
3434
"no-sparse-arrays": 2,
3535
"no-trailing-spaces": 2,
36+
"no-undef": 2,
3637
"no-unexpected-multiline": 2,
3738
"no-unreachable": 2,
3839
"no-unused-vars": 2,
40+
"one-var": ["error", { "initialized": "never" }],
3941
"quotes": [2, "single", { "avoidEscape": true, "allowTemplateLiterals": true }],
4042
"semi": [2, "always"],
4143
"semi-spacing": 2,

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ $ npm install sqlstring
1616

1717
## Usage
1818

19-
<!-- eslint-disable no-unused-vars -->
19+
<!-- eslint-disable no-undef, no-unused-vars -->
2020

2121
```js
2222
var SqlString = require('sqlstring');
@@ -32,6 +32,8 @@ In order to avoid SQL Injection attacks, you should always escape any user
3232
provided data before using it inside a SQL query. You can do so using the
3333
`SqlString.escape()` method:
3434

35+
<!-- eslint-disable no-undef -->
36+
3537
```js
3638
var userId = 'some user provided value';
3739
var sql = 'SELECT * FROM users WHERE id = ' + SqlString.escape(userId);
@@ -41,6 +43,8 @@ console.log(sql); // SELECT * FROM users WHERE id = 'some user provided value'
4143
Alternatively, you can use `?` characters as placeholders for values you would
4244
like to have escaped like this:
4345

46+
<!-- eslint-disable no-undef -->
47+
4448
```js
4549
var userId = 1;
4650
var sql = SqlString.format('SELECT * FROM users WHERE id = ?', [userId]);
@@ -51,6 +55,8 @@ Multiple placeholders are mapped to values in the same order as passed. For exam
5155
in the following query `foo` equals `a`, `bar` equals `b`, `baz` equals `c`, and
5256
`id` will be `userId`:
5357

58+
<!-- eslint-disable no-undef -->
59+
5460
```js
5561
var userId = 1;
5662
var sql = SqlString.format('UPDATE users SET foo = ?, bar = ?, baz = ? WHERE id = ?',
@@ -87,6 +93,8 @@ Different value types are escaped differently, here is how:
8793

8894
You may have noticed that this escaping allows you to do neat things like this:
8995

96+
<!-- eslint-disable no-undef -->
97+
9098
```js
9199
var post = {id: 1, title: 'Hello MySQL'};
92100
var sql = SqlString.format('INSERT INTO posts SET ?', post);
@@ -95,6 +103,8 @@ console.log(sql); // INSERT INTO posts SET `id` = 1, `title` = 'Hello MySQL'
95103

96104
And the `toSqlString` method allows you to form complex queries with functions:
97105

106+
<!-- eslint-disable no-undef -->
107+
98108
```js
99109
var CURRENT_TIMESTAMP = { toSqlString: function() { return 'CURRENT_TIMESTAMP()'; } };
100110
var sql = SqlString.format('UPDATE posts SET modified = ? WHERE id = ?', [CURRENT_TIMESTAMP, 42]);
@@ -108,6 +118,8 @@ placeholder, useful for using functions as dynamic values:
108118
**Caution** The string provided to `SqlString.raw()` will skip all escaping
109119
functions when used, so be careful when passing in unvalidated input.
110120

121+
<!-- eslint-disable no-undef -->
122+
111123
```js
112124
var CURRENT_TIMESTAMP = SqlString.raw('CURRENT_TIMESTAMP()');
113125
var sql = SqlString.format('UPDATE posts SET modified = ? WHERE id = ?', [CURRENT_TIMESTAMP, 42]);
@@ -117,6 +129,8 @@ console.log(sql); // UPDATE posts SET modified = CURRENT_TIMESTAMP() WHERE id =
117129
If you feel the need to escape queries by yourself, you can also use the escaping
118130
function directly:
119131

132+
<!-- eslint-disable no-undef -->
133+
120134
```js
121135
var sql = 'SELECT * FROM posts WHERE title=' + SqlString.escape('Hello MySQL');
122136
console.log(sql); // SELECT * FROM posts WHERE title='Hello MySQL'
@@ -127,6 +141,8 @@ console.log(sql); // SELECT * FROM posts WHERE title='Hello MySQL'
127141
If you can't trust an SQL identifier (database / table / column name) because it is
128142
provided by a user, you should escape it with `SqlString.escapeId(identifier)` like this:
129143

144+
<!-- eslint-disable no-undef -->
145+
130146
```js
131147
var sorter = 'date';
132148
var sql = 'SELECT * FROM posts ORDER BY ' + SqlString.escapeId(sorter);
@@ -135,6 +151,8 @@ console.log(sql); // SELECT * FROM posts ORDER BY `date`
135151

136152
It also supports adding qualified identifiers. It will escape both parts.
137153

154+
<!-- eslint-disable no-undef -->
155+
138156
```js
139157
var sorter = 'date';
140158
var sql = 'SELECT * FROM posts ORDER BY ' + SqlString.escapeId('posts.' + sorter);
@@ -144,6 +162,8 @@ console.log(sql); // SELECT * FROM posts ORDER BY `posts`.`date`
144162
If you do not want to treat `.` as qualified identifiers, you can set the second
145163
argument to `true` in order to keep the string as a literal identifier:
146164

165+
<!-- eslint-disable no-undef -->
166+
147167
```js
148168
var sorter = 'date.2';
149169
var sql = 'SELECT * FROM posts ORDER BY ' + SqlString.escapeId(sorter, true);
@@ -153,6 +173,8 @@ console.log(sql); // SELECT * FROM posts ORDER BY `date.2`
153173
Alternatively, you can use `??` characters as placeholders for identifiers you would
154174
like to have escaped like this:
155175

176+
<!-- eslint-disable no-undef -->
177+
156178
```js
157179
var userId = 1;
158180
var columns = ['username', 'email'];
@@ -168,6 +190,8 @@ When you pass an Object to `.escape()` or `.format()`, `.escapeId()` is used to
168190
You can use `SqlString.format` to prepare a query with multiple insertion points,
169191
utilizing the proper escaping for ids and values. A simple example of this follows:
170192

193+
<!-- eslint-disable no-undef -->
194+
171195
```js
172196
var userId = 1;
173197
var inserts = ['users', 'id', userId];
@@ -184,6 +208,8 @@ location-specific/timezone-aware `Date`.
184208
This can be further combined with the `SqlString.raw()` helper to generate SQL
185209
that includes MySQL functions as dynamic vales:
186210

211+
<!-- eslint-disable no-undef -->
212+
187213
```js
188214
var userId = 1;
189215
var data = { email: '[email protected]', modified: SqlString.raw('NOW()') };

0 commit comments

Comments
 (0)