Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ export interface IPGQueryResult {
rows: any[]
}

export interface IPGQueryable {
readonly query: (query: IPGQueryConfig) => Promise<IPGQueryResult>
export interface IPGQueryable<T extends IPGQueryResult = IPGQueryResult> {
readonly query: (query: IPGQueryConfig) => Promise<T>
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"clean": "rimraf pg.js pg.d.ts sql.js sql.d.ts lib/**/*.js lib/**/*.d.ts coverage",
"test": "jest",
"prettier": "prettier --write '**/*.{ts,js,json,css,md}' --ignore-path .gitignore",
"lint": "tslint *.ts **/*.ts"
"lint": "tslint -c tslint.json -p tsconfig.json -t stylish"
},
"devDependencies": {
"@types/jest": "^23.3.2",
Expand Down
12 changes: 6 additions & 6 deletions pg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import {
} from './lib/utils'
import _sql = require('./sql')

type PGSqlHelper<T> = (db: IPGQueryable) => TemplateLiteralFunc<Promise<T>>

type PGSql = TemplateLiteralFunc<IPGQueryConfig> & {
query: PGSqlHelper<IPGQueryResult>
many: PGSqlHelper<any[]>
one: PGSqlHelper<any>
count: PGSqlHelper<number>
query: <T extends IPGQueryResult>(
db: IPGQueryable<T>
) => TemplateLiteralFunc<Promise<T>>
many: (db: IPGQueryable) => TemplateLiteralFunc<Promise<any[]>>
one: (db: IPGQueryable) => TemplateLiteralFunc<Promise<any>>
count: (db: IPGQueryable) => TemplateLiteralFunc<Promise<number>>
}

const sql = ((chains, ...expressions) => _sql(chains, ...expressions)) as PGSql
Expand Down
30 changes: 20 additions & 10 deletions test/pg.test.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,48 @@
import { IPGQueryable } from '../lib/utils'
import sql = require('../pg')

const sampleBooks = ['book1', 'book2']
const db: IPGQueryable = {
const db = {
query: async ({ text, values }) => {
if (text === 'select * from books') {
return { rows: sampleBooks, rowCount: sampleBooks.length }
if (text === 'select * from books where read = $1') {
return {
oid: 1,
rowCount: sampleBooks.length,
rows: sampleBooks
}
}
return {
oid: 0,
rowCount: 0,
rows: []
}
return { rows: [], rowCount: 0 }
}
}

test('sql should return the query config', async () => {
const queryConfig = sql`select * from books`
const queryConfig = sql`select * from books where read = ${false}`
expect(queryConfig._sql).toBeTruthy()
})

test("sql.query should return pg's query result", async () => {
const { rows, rowCount } = await sql.query(db)`select * from books`
const { rows, rowCount, oid } = await sql.query(
db
)`select * from books where read = ${false}`
expect(rows).toBe(sampleBooks)
expect(rowCount).toBe(sampleBooks.length)
expect(oid).toBe(1)
})

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

test('sql.many should return rows', async () => {
const books = await sql.many(db)`select * from books`
const books = await sql.many(db)`select * from books where read = ${false}`
expect(books).toBe(sampleBooks)
})

test('sql.count should return rowCount', async () => {
const nbBooks = await sql.count(db)`select * from books`
const nbBooks = await sql.count(db)`select * from books where read = ${false}`
expect(nbBooks).toBe(sampleBooks.length)
})
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"module": "commonjs",
"declaration": true
},
"files": ["sql.ts", "pg.ts"]
"files": ["sql.ts", "pg.ts", "lib/utils.ts"]
}