Skip to content

Commit f946198

Browse files
committed
docs: line code blocks in readme
1 parent f5c7a4d commit f946198

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ before_install:
1919
# Setup Node.js version-specific dependencies
2020
- "test $TRAVIS_NODE_VERSION != '0.6' || npm rm --save-dev nyc"
2121
- "test $TRAVIS_NODE_VERSION != '0.8' || npm rm --save-dev nyc"
22-
- "test $(echo $TRAVIS_NODE_VERSION | cut -d'.' -f1) -ge 4 || npm rm --save-dev eslint"
22+
- "test $(echo $TRAVIS_NODE_VERSION | cut -d'.' -f1) -ge 4 || npm rm --save-dev eslint eslint-plugin-markdown"
2323
# Update Node.js modules
2424
- "test ! -d node_modules || npm prune"
2525
- "test ! -d node_modules || npm rebuild"

README.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ $ npm install sqlstring
1616

1717
## Usage
1818

19+
<!-- eslint-disable no-unused-vars -->
20+
1921
```js
2022
var SqlString = require('sqlstring');
2123
```
@@ -29,21 +31,27 @@ provided data before using it inside a SQL query. You can do so using the
2931
```js
3032
var userId = 'some user provided value';
3133
var sql = 'SELECT * FROM users WHERE id = ' + SqlString.escape(userId);
34+
console.log(sql); // SELECT * FROM users WHERE id = 'some user provided value'
3235
```
3336

3437
Alternatively, you can use `?` characters as placeholders for values you would
3538
like to have escaped like this:
3639

3740
```js
38-
var sql = SqlString.format('SELECT * FROM users WHERE id = ?', [userId]);
41+
var userId = 1;
42+
var sql = SqlString.format('SELECT * FROM users WHERE id = ?', [userId]);
43+
console.log(sql); // SELECT * FROM users WHERE id = 1
3944
```
4045

4146
Multiple placeholders are mapped to values in the same order as passed. For example,
4247
in the following query `foo` equals `a`, `bar` equals `b`, `baz` equals `c`, and
4348
`id` will be `userId`:
4449

4550
```js
46-
var sql = SqlString.format('UPDATE users SET foo = ?, bar = ?, baz = ? WHERE id = ?', ['a', 'b', 'c', userId]);
51+
var userId = 1;
52+
var sql = SqlString.format('UPDATE users SET foo = ?, bar = ?, baz = ? WHERE id = ?',
53+
['a', 'b', 'c', userId]);
54+
console.log(sql); // UPDATE users SET foo = 'a', bar = 'b', baz = 'c' WHERE id = 1
4755
```
4856

4957
This looks similar to prepared statements in MySQL, however it really just uses
@@ -96,15 +104,15 @@ provided by a user, you should escape it with `SqlString.escapeId(identifier)` l
96104
```js
97105
var sorter = 'date';
98106
var sql = 'SELECT * FROM posts ORDER BY ' + SqlString.escapeId(sorter);
99-
// -> SELECT * FROM posts ORDER BY `date`
107+
console.log(sql); // SELECT * FROM posts ORDER BY `date`
100108
```
101109

102110
It also supports adding qualified identifiers. It will escape both parts.
103111

104112
```js
105113
var sorter = 'date';
106114
var sql = 'SELECT * FROM posts ORDER BY ' + SqlString.escapeId('posts.' + sorter);
107-
// -> SELECT * FROM posts ORDER BY `posts`.`date`
115+
console.log(sql); // SELECT * FROM posts ORDER BY `posts`.`date`
108116
```
109117

110118
If you do not want to treat `.` as qualified identifiers, you can set the second
@@ -113,7 +121,7 @@ argument to `true` in order to keep the string as a literal identifier:
113121
```js
114122
var sorter = 'date.2';
115123
var sql = 'SELECT * FROM posts ORDER BY ' + connection.escapeId(sorter, true);
116-
// -> SELECT * FROM posts ORDER BY `date.2`
124+
console.log(sql); // SELECT * FROM posts ORDER BY `date.2`
117125
```
118126

119127
Alternatively, you can use `??` characters as placeholders for identifiers you would
@@ -135,8 +143,10 @@ You can use `SqlString.format` to prepare a query with multiple insertion points
135143
utilizing the proper escaping for ids and values. A simple example of this follows:
136144

137145
```js
146+
var userId = 1;
138147
var inserts = ['users', 'id', userId];
139148
var sql = SqlString.format('SELECT * FROM ?? WHERE ?? = ?', inserts);
149+
console.log(sql); // SELECT * FROM `users` WHERE `id` = 1
140150
```
141151

142152
Following this you then have a valid, escaped query that you can then send to the database safely.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"repository": "mysqljs/sqlstring",
1919
"devDependencies": {
2020
"eslint": "3.18.0",
21+
"eslint-plugin-markdown": "1.0.0-beta.4",
2122
"nyc": "10.2.0",
2223
"urun": "0.0.8",
2324
"utest": "0.0.8"
@@ -33,7 +34,7 @@
3334
"node": ">= 0.6"
3435
},
3536
"scripts": {
36-
"lint": "eslint .",
37+
"lint": "eslint --plugin markdown --ext js,md .",
3738
"test": "node test/run.js",
3839
"test-ci": "nyc --reporter=text npm test",
3940
"test-cov": "nyc --reporter=html --reporter=text npm test"

0 commit comments

Comments
 (0)