Skip to content

Commit ffdda9e

Browse files
yannvgnlucleray
authored andcommitted
Add typescript declaration file (#1)
1 parent 1bc02ae commit ffdda9e

8 files changed

Lines changed: 250 additions & 11 deletions

File tree

package.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,22 @@
1111
"pg"
1212
],
1313
"files": [
14-
"sql.js"
14+
"sql.js",
15+
"types/index.d.ts"
1516
],
17+
"types": "types",
1618
"scripts": {
1719
"test": "jest",
1820
"prettier": "prettier --write '**/*.{js,json,css,md}' --ignore-path .gitignore",
19-
"lint": "eslint '**/*.js' --ignore-path .gitignore"
21+
"lint": "eslint '**/*.js' --ignore-path .gitignore",
22+
"dtslint": "dtslint types"
2023
},
2124
"devDependencies": {
2225
"@sequencework/eslint-config": "^0.0.7",
23-
"jest": "^23.5.0",
26+
"dtslint": "^0.3.0",
2427
"eslint": "5.4.0",
2528
"husky": "1.0.0-rc.13",
29+
"jest": "^23.5.0",
2630
"lint-staged": "7.2.2",
2731
"prettier": "1.14.2"
2832
},
@@ -50,6 +54,10 @@
5054
"*.{json,css,md}": [
5155
"prettier --write",
5256
"git add"
57+
],
58+
"*.ts": [
59+
"prettier --write",
60+
"git add"
5361
]
5462
}
5563
}

readme.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Transforms a template literal in an object that can be read by [node-postgres](h
99
- Nested sql tags
1010
- `undefined` expressions are ignored
1111
- Useful [shorthand for node-postgres](#shorthand-fo-postgres) : sql(db)\`...\`
12+
- [TypeScript-friendly](#usage-with-typescript)
1213

1314
### Installation
1415

@@ -179,6 +180,23 @@ const listMoviesByYear = async (db, yearRange) => sql(db)`
179180
`
180181
```
181182

183+
### Usage with TypeScript
184+
185+
`sql` comes with its TypeScript declaration file. You can directly use it within your TypeScript projects:
186+
187+
```ts
188+
import sql = require('@sequencework/sql')
189+
190+
const yearRange: ReadonlyArray<number> = [1983, 1992]
191+
192+
const query = sql`
193+
select * from movies
194+
where
195+
year >= ${yearRange[0]}
196+
and year <= ${yearRange[1]}
197+
`
198+
```
199+
182200
### More
183201

184202
This package is inspired by the great [sql-template-strings](https://github.com/felixfbecker/node-sql-template-strings). Some interesting features that we were missing :

test/sql.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ test('shorthand for node-postgres', async () => {
8484
if (text === 'select * from books') {
8585
return { rows: sampleBooks }
8686
}
87-
return { rows: false }
87+
return { rows: [] }
8888
}
8989
}
9090

types/index.d.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// TypeScript Version: 2.9
2+
3+
export = sql
4+
5+
declare function sql(chains: {
6+
readonly query: (
7+
queryExpression: sql.QueryConfig
8+
) => Promise<{
9+
rows: any[]
10+
}>
11+
}): (chains: ReadonlyArray<string>, ...expressions: any[]) => Promise<any[]>
12+
13+
declare function sql(
14+
chains: ReadonlyArray<string>,
15+
...expressions: any[]
16+
): sql.QueryConfig
17+
18+
declare namespace sql {
19+
interface QueryConfig {
20+
_sql?: { chains: ReadonlyArray<string>; expressions: any[] }
21+
text: string
22+
values: any[]
23+
}
24+
}

types/test.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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()

types/tsconfig.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"compilerOptions": {
3+
"module": "commonjs",
4+
"lib": ["es6"],
5+
"noImplicitAny": true,
6+
"noImplicitThis": true,
7+
"strictNullChecks": true,
8+
"strictFunctionTypes": true,
9+
"noEmit": true,
10+
11+
// If the library is an external module (uses `export`), this allows your test file to import "mylib" instead of "./index".
12+
// If the library is global (cannot be imported via `import` or `require`), leave this out.
13+
"baseUrl": ".",
14+
"paths": { "@sequencework/sql": ["."] }
15+
}
16+
}

types/tslint.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "dtslint/dtslint.json",
3+
"rules": {
4+
"semicolon": false,
5+
"indent": [true, "spaces", 2]
6+
}
7+
}

0 commit comments

Comments
 (0)