UNPKG

2.19 kBTypeScriptView Raw
1declare module 'sql-template-strings' {
2 export class SQLStatement {
3 private strings: string[]
4
5 /**
6 * The SQL Statement for [node-postgres](https://www.npmjs.com/package/pg)
7 */
8 text: string
9
10 /**
11 * The SQL Statement for [Sequelize](https://www.npmjs.com/package/sequelize)
12 */
13 query: string
14
15 /**
16 * The SQL Statement for [mysql](https://www.npmjs.com/package/mysql)
17 */
18 sql: string
19
20 /**
21 * The values to be inserted for the placeholders
22 */
23 values: any[]
24
25 /**
26 * The name for postgres prepared statements, if set
27 */
28 name: string
29
30 /**
31 * Appends a string or another statement
32 *
33 * ```ts
34 * query.append(SQL`AND genre = ${genre}`).append(' ORDER BY rating')
35 * query.text // => 'SELECT author FROM books WHERE name = $1 AND author = $2 AND genre = $3 ORDER BY rating'
36 * query.sql // => 'SELECT author FROM books WHERE name = ? AND author = ? AND genre = ? ORDER BY rating'
37 * query.values // => ['harry potter', 'J. K. Rowling', 'Fantasy'] ORDER BY rating`
38 *
39 * const query = SQL`SELECT * FROM books`
40 * if (params.name) {
41 * query.append(SQL` WHERE name = ${params.name}`)
42 * }
43 * query.append(SQL` LIMIT 10 OFFSET ${params.offset || 0}`)
44 * ```
45 */
46 append (statement: SQLStatement | string | number): this
47
48 /**
49 * Sets the name property of this statement for prepared statements in postgres
50 *
51 * ```ts
52 * pg.query(SQL`SELECT author FROM books WHERE name = ${book}`.setName('my_query'))
53 * ```
54 */
55 setName (name: string): this
56
57 /**
58 * Use a prepared statement with Sequelize.
59 * Makes `query` return a query with `$n` syntax instead of `?` and switches the `values` key name to `bind`
60 * If omitted, `value` defaults to `true`.
61 */
62 useBind (value?: boolean): this
63 }
64
65 /**
66 * The template string tag
67 *
68 * ```ts
69 * import {SQL} from 'sql-template-strings';
70 *
71 * pg.query(SQL`SELECT author FROM books WHERE name = ${book} AND author = ${author}`)
72 * ```
73 */
74 export function SQL (strings: any, ...values: any[]): SQLStatement
75 export default SQL
76}
77
\No newline at end of file