Skip to content

Commit 516aa99

Browse files
yannvgnlucleray
authored andcommitted
change return type of sql.query to reflect the one of db.query (#10)
* return type of sql.query is now the same as db.query * fix linting
1 parent 672701b commit 516aa99

File tree

5 files changed

+30
-20
lines changed

5 files changed

+30
-20
lines changed

lib/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ export interface IPGQueryResult {
2929
rows: any[]
3030
}
3131

32-
export interface IPGQueryable {
33-
readonly query: (query: IPGQueryConfig) => Promise<IPGQueryResult>
32+
export interface IPGQueryable<T extends IPGQueryResult = IPGQueryResult> {
33+
readonly query: (query: IPGQueryConfig) => Promise<T>
3434
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"clean": "rimraf pg.js pg.d.ts sql.js sql.d.ts lib/**/*.js lib/**/*.d.ts coverage",
2525
"test": "jest",
2626
"prettier": "prettier --write '**/*.{ts,js,json,css,md}' --ignore-path .gitignore",
27-
"lint": "tslint *.ts **/*.ts"
27+
"lint": "tslint -c tslint.json -p tsconfig.json -t stylish"
2828
},
2929
"devDependencies": {
3030
"@types/jest": "^23.3.2",

pg.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import {
66
} from './lib/utils'
77
import _sql = require('./sql')
88

9-
type PGSqlHelper<T> = (db: IPGQueryable) => TemplateLiteralFunc<Promise<T>>
10-
119
type PGSql = TemplateLiteralFunc<IPGQueryConfig> & {
12-
query: PGSqlHelper<IPGQueryResult>
13-
many: PGSqlHelper<any[]>
14-
one: PGSqlHelper<any>
15-
count: PGSqlHelper<number>
10+
query: <T extends IPGQueryResult>(
11+
db: IPGQueryable<T>
12+
) => TemplateLiteralFunc<Promise<T>>
13+
many: (db: IPGQueryable) => TemplateLiteralFunc<Promise<any[]>>
14+
one: (db: IPGQueryable) => TemplateLiteralFunc<Promise<any>>
15+
count: (db: IPGQueryable) => TemplateLiteralFunc<Promise<number>>
1616
}
1717

1818
const sql = ((chains, ...expressions) => _sql(chains, ...expressions)) as PGSql

test/pg.test.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,48 @@
1-
import { IPGQueryable } from '../lib/utils'
21
import sql = require('../pg')
32

43
const sampleBooks = ['book1', 'book2']
5-
const db: IPGQueryable = {
4+
const db = {
65
query: async ({ text, values }) => {
7-
if (text === 'select * from books') {
8-
return { rows: sampleBooks, rowCount: sampleBooks.length }
6+
if (text === 'select * from books where read = $1') {
7+
return {
8+
oid: 1,
9+
rowCount: sampleBooks.length,
10+
rows: sampleBooks
11+
}
12+
}
13+
return {
14+
oid: 0,
15+
rowCount: 0,
16+
rows: []
917
}
10-
return { rows: [], rowCount: 0 }
1118
}
1219
}
1320

1421
test('sql should return the query config', async () => {
15-
const queryConfig = sql`select * from books`
22+
const queryConfig = sql`select * from books where read = ${false}`
1623
expect(queryConfig._sql).toBeTruthy()
1724
})
1825

1926
test("sql.query should return pg's query result", async () => {
20-
const { rows, rowCount } = await sql.query(db)`select * from books`
27+
const { rows, rowCount, oid } = await sql.query(
28+
db
29+
)`select * from books where read = ${false}`
2130
expect(rows).toBe(sampleBooks)
2231
expect(rowCount).toBe(sampleBooks.length)
32+
expect(oid).toBe(1)
2333
})
2434

2535
test('sql.one should return the first row', async () => {
26-
const book = await sql.one(db)`select * from books`
36+
const book = await sql.one(db)`select * from books where read = ${false}`
2737
expect(book).toBe(sampleBooks[0])
2838
})
2939

3040
test('sql.many should return rows', async () => {
31-
const books = await sql.many(db)`select * from books`
41+
const books = await sql.many(db)`select * from books where read = ${false}`
3242
expect(books).toBe(sampleBooks)
3343
})
3444

3545
test('sql.count should return rowCount', async () => {
36-
const nbBooks = await sql.count(db)`select * from books`
46+
const nbBooks = await sql.count(db)`select * from books where read = ${false}`
3747
expect(nbBooks).toBe(sampleBooks.length)
3848
})

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
"module": "commonjs",
55
"declaration": true
66
},
7-
"files": ["sql.ts", "pg.ts"]
7+
"files": ["sql.ts", "pg.ts", "lib/utils.ts"]
88
}

0 commit comments

Comments
 (0)