Skip to content

Commit e83589c

Browse files
authored
Add documentation on helper methods (#13)
1 parent 93f1253 commit e83589c

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

README.md

+26-6
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,37 @@ const query = sql`SELECT * FROM books WHERE author_id IN (${nested})`;
3535
sql`SELECT * FROM books ${hasIds ? sql`WHERE ids IN (${join(ids)})` : empty}`;
3636
```
3737

38-
## Related
38+
### Join
39+
40+
Accepts an array of values and returns a SQL instance with the values joined by the separator. E.g.
41+
42+
```js
43+
const query = join([1, 2, 3]);
3944

40-
The main difference between this module and others is first-class TypeScript. There's also the `raw`, `join` and `empty` helpers. Specific differences are documented below:
45+
query.sql; //=> "?, ?, ?"
46+
query.values; //=> [1, 2, 3]
47+
```
48+
49+
### Raw
4150

42-
**[`sql-template-strings`](https://github.com/felixfbecker/node-sql-template-strings)**
51+
Accepts a string and returns a SQL instance, useful if you want some part of the SQL to be dynamic.
4352

44-
Promotes mutation via chained methods and lacks nesting SQL statements. The idea to support both `sql` and `text` for `mysql` and `pg` compatibility came from here.
53+
```js
54+
raw("SELECT"); // == sql`SELECT`
55+
```
56+
57+
**Do not** accept user input to `raw`, this will create a SQL injection vulnerability.
58+
59+
### Empty
60+
61+
Simple placeholder value for an empty SQL string. Equivalent to `raw("")`.
62+
63+
## Related
4564

46-
**[`pg-template-tag`](https://github.com/XeCycle/pg-template-tag)**
65+
Some other modules exist that do something similar:
4766

48-
Missing TypeScript and MySQL support. This is the API I envisioned before starting development and, by supporting `pg` only, it has the ability to [dedupe `values`](https://github.com/XeCycle/pg-template-tag/issues/5#issuecomment-386875336). Supporting MySQL makes deduping impossible, because of `?` placeholders instead of `$<num>`, so I decided that was a premature optimisation here and opted to keep `mysql` support here instead.
67+
- [`sql-template-strings`](https://github.com/felixfbecker/node-sql-template-strings): promotes mutation via chained methods and lacks nesting SQL statements. The idea to support `sql` and `text` properties for dual `mysql` and `pg` compatibility came from here.
68+
- [`pg-template-tag`](https://github.com/XeCycle/pg-template-tag): missing TypeScript and MySQL support. This is the API I envisioned before writing this library, and by supporting `pg` only it has the ability to [dedupe `values`](https://github.com/XeCycle/pg-template-tag/issues/5#issuecomment-386875336).
4969

5070
## License
5171

0 commit comments

Comments
 (0)