|
| 1 | +// This contains sample code which tests the typings. This code does not run, but it is type-checked. |
| 2 | +// See https://github.com/DefinitelyTyped/DefinitelyTyped |
| 3 | + |
| 4 | +import sql = require('@sequencework/sql') |
| 5 | + |
| 6 | +const yearRange = [1983, 1992] |
| 7 | + |
| 8 | +// basic usage |
| 9 | +// $ExpectType QueryConfig |
| 10 | +sql` |
| 11 | + select * from movies |
| 12 | + where |
| 13 | + year >= ${yearRange[0]} |
| 14 | + and year <= ${yearRange[1]} |
| 15 | +` |
| 16 | + |
| 17 | +// expressions returning undefined |
| 18 | +// $ExpectType QueryConfig |
| 19 | +sql` |
| 20 | + select * from books |
| 21 | + ${undefined} |
| 22 | +` |
| 23 | + |
| 24 | +// expressions returning booleans |
| 25 | +// $ExpectType QueryConfig |
| 26 | +sql`select * from books where read = ${false}` |
| 27 | + |
| 28 | +// expressions returning null |
| 29 | +// $ExpectType QueryConfig |
| 30 | +sql`select * from books where author = ${null}` |
| 31 | + |
| 32 | +// imbricated sql tags |
| 33 | +const author = 'steinbeck' |
| 34 | +// $ExpectType QueryConfig |
| 35 | +sql` |
| 36 | + select * from books |
| 37 | + ${author && sql`where author = ${author}`} |
| 38 | +` |
| 39 | + |
| 40 | +// imbricated sql tags (2 levels) |
| 41 | +const [expr0, expr1, expr2] = ['i', 'like', 'sql'] |
| 42 | +// $ExpectType QueryConfig |
| 43 | +sql` |
| 44 | + level 0 ${expr0} |
| 45 | + ${sql` |
| 46 | + level 1 ${expr1} |
| 47 | + ${sql` |
| 48 | + level 2 ${expr2} |
| 49 | + `} |
| 50 | + `} |
| 51 | +` |
| 52 | + |
| 53 | +// shorthand for node-postgres |
| 54 | +const sampleBooks = ['book1', 'book2'] |
| 55 | +const db = { |
| 56 | + query: async ({ text, values }: { text: string; values: any[] }) => { |
| 57 | + if (text === 'select * from books') { |
| 58 | + return { rows: sampleBooks } |
| 59 | + } |
| 60 | + return { rows: [] } |
| 61 | + } |
| 62 | +} |
| 63 | +const getBooks = async (): Promise<string[]> => { |
| 64 | + const rows = await sql(db)`select * from books` |
| 65 | + return rows as string[] |
| 66 | +} |
| 67 | +// $ExpectType Promise<string[]> |
| 68 | +getBooks() |
0 commit comments